2

apache 到nginx的rewrite规则转换

 1 year ago
source link: https://blog.p2hp.com/archives/9857
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

apache 到nginx的rewrite规则转换

apachenginx 的rewrite规则转换

重定向到主站点

在他们的共享主机生活中,曾经使用 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.comwww.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?;

转换怪物规则

典型的怪物规则:

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK