書き換えルールの変換

Mongrel ルールの変換

メインサイトへのリダイレクト

共有ホスティング期間中、Apache の .htaccess ファイルののみを使用してすべてを構成していた人たちは、通常、以下のルールを変換します

RewriteCond  %{HTTP_HOST}  example.org
RewriteRule  (.*)          http://www.example.org$1

このような記述にしたの

server {
    listen       80;
    server_name  www.example.org  example.org;
    if ($http_host = example.org) {
        rewrite  (.*)  http://www.example.org$1;
    }
    ...
}

これは間違った、面倒で、非効率な方法です。正しい方法は、example.org に対して個別のサーバーを定義することです

server {
    listen       80;
    server_name  example.org;
    return       301 http://www.example.org$request_uri;
}

server {
    listen       80;
    server_name  www.example.org;
    ...
}

0.9.1 より前のバージョンでは、リダイレクトは次のように行えます
    rewrite      ^ http://www.example.org$request_uri?;

もう一つの例。「example.com でも www.example.com でもないすべてのもの」という「逆さ」ロジックの代わりに

RewriteCond  %{HTTP_HOST}  !example.com
RewriteCond  %{HTTP_HOST}  !www.example.com
RewriteRule  (.*)          http://www.example.com$1

example.comwww.example.com、および「その他のすべて」を定義する必要があります

server {
    listen       80;
    server_name  example.com www.example.com;
    ...
}

server {
    listen       80 default_server;
    server_name  _;
    return       301 http://example.com$request_uri;
}

0.9.1 より前のバージョンでは、リダイレクトは次のように行えます
    rewrite      ^ http://example.com$request_uri?;

Mongrel ルールの変換

一般的な Mongrel ルール

DocumentRoot /var/www/myapp.com/current/public

RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ $1 [QSA,L]

RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*)$ $1/index.html [QSA,L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [QSA,L]

RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

は次のように変換する必要があります

location / {
    root       /var/www/myapp.com/current/public;

    try_files  /system/maintenance.html
               $uri  $uri/index.html $uri.html
               @mongrel;
}

location @mongrel {
    proxy_pass  http://mongrel;
}