只使用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.com, www.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规则:
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; }