转换重写规则

    像这样:

    1. server {
    2. listen 80;
    3. server_name www.example.org example.org;
    4. if ($http_host = example.org) {
    5. rewrite (.*) http://www.example.org$1;
    6. }
    7. ...
    8. }

    这是一个错误、麻烦而无效的做法。正确的方式是为 example.org 定义一个单独的服务器:

    1. server {
    2. listen 80;
    3. server_name example.org;
    4. return 301 http://www.example.org$request_uri;
    5. }
    6. server {
    7. listen 80;
    8. server_name www.example.org;
    9. }

    另一个例子是使用了颠倒逻辑,即 所有不是 example.com 和 的

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

    应该简单地定义 example.comwww.example.com其他一切

    1. server {
    2. listen 80;
    3. server_name example.com www.example.com;
    4. ...
    5. }
    6. server {
    7. listen 80 default_server;
    8. server_name _;
    9. return 301 http://example.com$request_uri;
    10. }

    典型的 Mongrel 规则:

    1. DocumentRoot /var/www/myapp.com/current/public
    2. RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
    3. RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    4. RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]
    5. RewriteCond %{REQUEST_FILENAME} -f
    6. RewriteRule ^(.*)$ $1 [QSA,L]
    7. RewriteCond %{REQUEST_FILENAME}/index.html -f
    8. RewriteRule ^(.*)$ $1/index.html [QSA,L]
    9. RewriteCond %{REQUEST_FILENAME}.html -f
    10. RewriteRule ^(.*)$ $1.html [QSA,L]
    11. RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

    应该转换为:

    1. location / {
    2. root /var/www/myapp.com/current/public;
    3. try_files /system/maintenance.html
    4. $uri $uri/index.html $uri.html
    5. @mongrel;
    6. }
    7. location @mongrel {
    8. proxy_pass http://mongrel;