$c->req->base するとhttp://127.0.0.1/が返ってくる


どうも、着々とデブになりつつ有るたかとしです。

そろそろランニングしないとマジでまずい。豚になる。

本題。

apache

ServerName www.example.com
ProxyPass /peropero/m http://127.0.0.1:3000/m
ProxyPassReverse /peropero/m http://127.0.0.1:3000/m

とやっていて、

さらにCatalystのRoot.pmで

my $base = $c->req->base;

とやると、$baseに「http://127.0.0.1/」が返ってくる。。
あれ、「http://www.example.com/」じゃないの?
どうしたらいいの?

と思ってマニュアルをチェック。

% perldoc Catalyst
/PROXY
PROXY SUPPORT
Many production servers operate using the common double-server
approach, with a lightweight frontend web server passing requests to a
larger backend server. An application running on the backend server
must deal with two problems: the remote user always appears to be
127.0.0.1 and the server's hostname will appear to be "localhost"
regardless of the virtual host that the user connected through.

Catalyst will automatically detect this situation when you are running
the frontend and backend servers on the same machine. The following
changes are made to the request.

$c->req->address is set to the user's real IP address, as read from
the HTTP X-Forwarded-For header.

The host value for $c->req->base and $c->req->uri is set to the real
host, as read from the HTTP X-Forwarded-Host header.

Additionally, you may be running your backend application on an
insecure connection (port 80) while your frontend proxy is running
under SSL. If there is a discrepancy in the ports, use the HTTP header
"X-Forwarded-Port" to tell Catalyst what port the frontend listens on.
This will allow all URIs to be created properly.

In the case of passing in:

X-Forwarded-Port: 443

All calls to "uri_for" will result in an https link, as is expected.

Obviously, your web server must support these headers for this to work.

In a more complex server farm environment where you may have your
frontend proxy server(s) on different machines, you will need to set a
configuration option to tell Catalyst to read the proxied data from the
headers.

MyApp->config(using_frontend_proxy => 1);

If you do not wish to use the proxy support at all, you may set:

MyApp->config(ignore_frontend_proxy => 0);

あれ、The host value for $c->req->base and $c->req->uri is set to the real host, as read from the HTTP X-Forwarded-Host header.って書いてんじゃん…?

いみふ。



追記:
結局、c->req->baseを上書きして対応した。
謎は深まるばかり…
いみふすぎてウンコもれるかと思った

FreeBSDにPerlのフレームワーク「Catalyst」をインストール

どうも、さいきんめっきり寒くなってきて朝が辛いたかとしです。


どうも一度目覚めて居間で二度寝してしまう…



本題。

条件

FreeBSD
perl 5.10
portsが最新である

手順

1.% sudo -s でrootになる
 インストールするときは必ずrootで。
 じゃないとめんどくさそう


2.# perl -MCPAN -e shell を実行。
 実行したら普通のプロンプトから変わる。
 最初の実行だと設定どうするか聞かれるけど、とりあえず全部デフォルトでOK


3.cpan > o conf urllist push ftp://ftp.u-aizu.ac.jp/pub/CPAN
 これを実行すると指定したURLから優先的にダウンロードするように。
 必須じゃないけど念のため。これ以外に追加したい場合はここを参考に


4.cpan > o conf prerequisites_policy follow
 Catalystのインストールで一番めんどくさい、対話形式の質問を自動でdefaultにしてくれるおまじない。
 これないと地獄。カスタマイズしたいなら無用


5.cpan > o conf commit
 3と4の設定をconfigファイルに書き込むコマンド。
 これやらないと設定反映されない


6.cpan > install Catalyst::Runtime を実行
 特に問題なく終了(するはず)。


7.cpan > install Catalyst::Devel を実行
 実行すると途中でハングアップする。(2012/10現在)
 詳しい解決方法はこの記事で →たかとの日記


8.cpan > install Task::Catalyst を実行
 これも問題なく終了。


9.その他、必要なモジュールをインストール。


まぁcpan使わなくてもぶっちゃけpotsで入れられるんだが、
これやったのがお引越し案件で、お引越し元がcpanで入れてたからこっちでやってみた。

これでとりあえずインストールは完了。

apache2.2にmod_auth_mysqlを追加。


どうも、最近そわそわしているたかとしです。


なんか会社に不穏な空気(?)が…



本題。


先日jail内にapache2.2をインストールしましたが、それにBASIC認証のモジュールを追加しました。

手順

1.公式サイトからダウンロード。
http://sourceforge.net/projects/modauthmysql/

2.コンパイルエラーが発生するらしいので、パッチファイルをダウンロード。

      • mod_auth_mysql.c.orig 2005-06-23 01:17:45.000000000 +0900
      1. mod_auth_mysql.c 2006-12-19 19:33:25.106795000 +0900

@@ -206,7 +206,7 @@
#define SNPRINTF apr_snprintf
#define PSTRDUP apr_pstrdup
#define PSTRNDUP apr_pstrndup

  • #define STRCAT ap_pstrcat
  1. #define STRCAT apr_pstrcat

#define POOL apr_pool_t
#include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
#include "ap_compat.h"
@@ -237,7 +237,7 @@
#define SNPRINTF ap_snprintf
#define PSTRDUP ap_pstrdup
#define PSTRNDUP ap_pstrndup

  • #define STRCAT ap_pstrcat
  1. #define STRCAT apr_pstrcat

#define POOL pool
#include
#include "ap_sha1.h"
@@ -589,87 +589,87 @@
static
command_rec mysql_auth_cmds[] = {
AP_INIT_TAKE1("AuthMySQLHost", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlhost),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlhost),

OR_AUTHCFG, "mysql server host name"),

AP_INIT_TAKE1("AuthMySQLPort", ap_set_int_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlport),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlport),

OR_AUTHCFG, "mysql server port number"),

AP_INIT_TAKE1("AuthMySQLSocket", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlsocket),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlsocket),

OR_AUTHCFG, "mysql server socket path"),

AP_INIT_TAKE1("AuthMySQLUser", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqluser),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqluser),

OR_AUTHCFG, "mysql server user name"),

AP_INIT_TAKE1("AuthMySQLPassword", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlpasswd),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlpasswd),

OR_AUTHCFG, "mysql server user password"),

AP_INIT_TAKE1("AuthMySQLDB", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlDB),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlDB),

OR_AUTHCFG, "mysql database name"),

AP_INIT_TAKE1("AuthMySQLUserTable", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlpwtable),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlpwtable),

OR_AUTHCFG, "mysql user table name"),

AP_INIT_TAKE1("AuthMySQLGroupTable", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlgrptable),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlgrptable),

OR_AUTHCFG, "mysql group table name"),

AP_INIT_TAKE1("AuthMySQLNameField", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlNameField),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlNameField),

OR_AUTHCFG, "mysql User ID field name within User table"),

AP_INIT_TAKE1("AuthMySQLGroupField", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlGroupField),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlGroupField),

OR_AUTHCFG, "mysql Group field name within table"),

AP_INIT_TAKE1("AuthMySQLGroupUserNameField", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlGroupUserNameField),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlGroupUserNameField),

OR_AUTHCFG, "mysql User ID field name within Group table"),

AP_INIT_TAKE1("AuthMySQLPasswordField", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlPasswordField),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlPasswordField),

OR_AUTHCFG, "mysql Password field name within table"),

AP_INIT_TAKE1("AuthMySQLPwEncryption", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlEncryptionField),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlEncryptionField),

OR_AUTHCFG, "mysql password encryption method"),

AP_INIT_TAKE1("AuthMySQLSaltField", ap_set_string_slot,

  • (void*) APR_XtOffsetOf(mysql_auth_config_rec, mysqlSaltField),
  1. (void*) APR_OFFSETOF(mysql_auth_config_rec, mysqlSaltField),

OR_AUTHCFG, "mysql salfe field name within table"),

/* AP_INIT_FLAG("AuthMySQLKeepAlive", ap_set_flag_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlKeepAlive),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlKeepAlive),

OR_AUTHCFG, "mysql connection kept open across requests if On"),
*/
AP_INIT_FLAG("AuthMySQLAuthoritative", ap_set_flag_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlAuthoritative),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlAuthoritative),

OR_AUTHCFG, "mysql lookup is authoritative if On"),

AP_INIT_FLAG("AuthMySQLNoPasswd", ap_set_flag_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlNoPasswd),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlNoPasswd),

OR_AUTHCFG, "If On, only check if user exists; ignore password"),

AP_INIT_FLAG("AuthMySQLEnable", ap_set_flag_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlEnable),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlEnable),

OR_AUTHCFG, "enable mysql authorization"),

AP_INIT_TAKE1("AuthMySQLUserCondition", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlUserCondition),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlUserCondition),

OR_AUTHCFG, "condition to add to user where-clause"),

AP_INIT_TAKE1("AuthMySQLGroupCondition", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlGroupCondition),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlGroupCondition),

OR_AUTHCFG, "condition to add to group where-clause"),

AP_INIT_TAKE1("AuthMySQLCharacterSet", ap_set_string_slot,

  • (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlCharacterSet),
  1. (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlCharacterSet),

OR_AUTHCFG, "mysql character set to be used"),

{ NULL }

3.以下コマンド実行。

% tar zxvf mod_auth_mysql-3.0.0.tar.gz
% cd mod_auth_mysql-3.0.0
% patch -p0 < ../mod_auth_mysql.c.patch
% /usr/local/sbin/apxs -c -L/usr/local/lib/mysql -I/usr/local/include/mysql lmysqlclient -lm -lz mod_auth_mysql.c
% apxs -i mod_auth_mysql.la

4.httpd.confに以下を追加。

LoadModule mysql_auth_module modules/mod_auth_mysql.so
※mod_auth_basic.soの前に記述しなきゃだめみたい。

5.httpd.confに以下を追加。(というかvhost/に01.mysql.authを作成して記述)

AuthMySQLEnable On
AuthMySQLSocket /var/lib/mysql/mysql.sock
AuthMySQLHost host_name
AuthMySQLUser auth_user_name
AuthMySQLPassword *********
AuthMySQLDB auth
AuthMySQLUserTable user
AuthMySQLNameField name
AuthMySQLPasswordField password
AuthMySQLPwEncryption sha1
AuthMySQLNoPasswd Off
AuthGroupFile /dev/null
AuthName "Enter your ID and password"
AuthType Basic
require valid-user



追記。

上記ですが、動かなかったので結局portsからmod_auth_mysql_anotherをインストール。
んで上記設定をすると動きました。