将 apache 重写规则转换为 nginx

将 apache 重写规则转换为 nginx

我想将 Apache 设置迁移到 Nginx,但无法使重写规则在 Nginx 中工作。我查看了官方的 nginx 文档,但转换时仍然有些麻烦。http://nginx.org/en/docs/http/converting_rewrite_rules.html

我用过http://winginx.com/en/htaccess转换我的规则,但这只是部分起作用。/ 部分看起来还行,/library 部分也行,但 /public 部分根本不起作用。

Apache 部分:



    ServerAdmin webmaster@localhost
    DocumentRoot /srv/www/Web
        
          Order allow,deny
          Allow from all
          RewriteEngine On
          RewriteRule  ^$    public/      [L]
          RewriteRule  (.*)  public/$1    [L]
        

        
          Order Deny,Allow
          Deny from all
        

        
          RewriteEngine On
          RewriteCond %{QUERY_STRING} ^pid=([0-9]*)$
          RewriteRule ^places(.*)$ index.php?url=places/view/%1 [PT,L]
          # Extract search query in /search?q={query}&l={location}
          RewriteCond %{QUERY_STRING} ^q=(.*)&l=(.*)$
          RewriteRule ^(.*)$ index.php?url=search/index/%1/%2 [PT,L]
          # Extract search query in /search?q={query}
          RewriteCond %{QUERY_STRING} ^q=(.*)$
          RewriteRule ^(.*)$ index.php?url=search/index/%1 [PT,L]
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          # Rewrite all other URLs to index.php/URL
          RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
        

        
          Order deny,allow
          deny from all
        
    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
        
          AddHandler php5-fcgi .php
          Action php5-fcgi /php5-fcgi
          Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
          FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
        
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Nginx 配置:


server {
  #listen   80; ## listen for ipv4; this line is default and implied
  root /srv/www/Web;
  index index.html index.php;
  server_name localhost;
  location / {
    rewrite ^/$ /public/ break;
    rewrite ^(.*)$ /public/$1 break;
  }

  location /library {
    deny all;
  }

  location /public {
    if ($query_string ~ "^pid=([0-9]*)$"){
      rewrite ^/places(.*)$ /index.php?url=places/view/%1 break;
    }
    if ($query_string ~ "^q=(.*)&l=(.*)$"){
      rewrite ^(.*)$ /index.php?url=search/index/%1/%2 break;
    }
    if ($query_string ~ "^q=(.*)$"){
      rewrite ^(.*)$ /index.php?url=search/index/%1 break;
    }
    if (!-e $request_filename){
      rewrite ^(.*)$ /index.php?url=$1 break;
    }
  }

  location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}

我还没有写过原始规则集,所以很难转换它。你能给我一点提示,告诉我如何轻松地完成转换吗?或者你能帮我转换一下吗?

我真的很想切换到 php5-fpm 和 nginx :)

谢谢

答案1

尝试将此块用于公共位置:

location /public {
    if ($args ~ "^pid=(?<placepid>[0-9]+)$") {
        rewrite ^/places /index.php?url=places/view/$placepid last;
    }
    if ($args ~ "^q=(?<getq>.+)&l=(?<getl>.+)$") {
        rewrite ^ /index.php?url=search/index/$getq/$getl last;
    }
    if ($args ~ "^q=(?<getq>.+)$") {
        rewrite ^ /index.php?url=search/index/$getq last;
    }
    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php?url=$1 last;
    }
}

在 nginx 中,查询部分中对正则表达式捕获的引用与 Apache 中的工作方式不同。在这里,我们?<variablename>在与查询字符串匹配的部分中使用命名捕获$args。然后我们在重写部分使用变量。

配置可以进一步优化,因为这个版本有不必要的复杂性(一开始将 / 重写为 /public)。

相关内容