Converting rewrite rules转换重写规则

Converting Mongrel rules转换混合规则

A redirect to a main site重定向到主站点

People who during their shared hosting life used to configure everything using only Apache’s .htaccess files, usually translate the following rules:那些在共享托管期间只使用Apache的.htaccess文件配置所有内容的人通常会转换以下规则:

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

to something like this:对这样的事情:

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

This is a wrong, cumbersome, and ineffective way. 这是一种错误、繁琐且无效的方法。The right way is to define a separate server for example.org:正确的方法是为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;
    ... }
On versions prior to 0.9.1, redirects can be made with:在0.9.1之前的版本上,可以通过以下方式进行重定向:
rewrite      ^ http://www.example.org$request_uri?;

Another example. Instead of the “upside-down” logic “all that is not example.com and is not www.example.com”:另一个例子。而不是“颠倒”逻辑“所有不是example.com也不是www.example.com”:

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

one should simply define example.com, www.example.com, and “everything else”:我们应该简单地定义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;
}
On versions prior to 0.9.1, redirects can be made with:在0.9.1之前的版本上,可以通过以下方式进行重定向:
rewrite      ^ http://example.com$request_uri?;

Converting Mongrel rules转换混合规则

Typical Mongrel rules:典型的混血儿规则:

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]

should be converted to应转换为

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;
}