【问题标题】:URL Rewrite rule not working on nginxURL重写规则不适用于nginx
【发布时间】:2013-04-05 19:45:49
【问题描述】:

我想在 nginx 中像在 apache 中一样重写 url。基本上是为了给基于位置的请求一个更友好的 url 外观。我一直找不到 404 页面。

server {

  # site definitions (...)

  # fastcgi defitions (...)

  location / {

    # Code to make wp super cache work (...)

    rewrite ^/ads/category/(.*)/(.*)$ /ads/category/$1?location=$2 permanent;
    rewrite ^/category/(.*)/(.*)$ /ads/category/$1?location=$2 last;
    rewrite ^/category/(.*)$ /ads/category/$1 last;

  }
}

我将上面的代码放在虚拟主机定义 /etc/nginx/sites-enable/mysite 中。到目前为止,我不知道调试正在发生的事情的真正方法。

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    而不是在单个位置/{}块中定义所有重写规则;试试这样的:

    # site definitions (...)
    
    location / {
        # Code to make wp super cache work (...)
    
        # remove following line if you already have an index at site definitions..
        index index.php;
    
        try_files $uri $uri/ /index.php?$args;
      }
    
      if (!-f $request_filename) {
           rewrite ^/ads/category/(.*)/(.*)$ /ads/category/$1?location=$2 permanent;
           rewrite ^/category/(.*)/(.*)$ /ads/category/$1?location=$2 break;
           rewrite ^/category/(.*)$ /ads/category/$1 break;
      }
    
     # fastcgi defitions (...)
    

    我知道 if is evil 在 nginx 世界中,但它在 location {} 块中使用时会出现问题,类似的配置在我的虚拟服务器上就像一个魅力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-28
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 2015-07-03
      • 2017-03-13
      相关资源
      最近更新 更多