URL重写规则不适用于nginx

时间:2013-04-05 19:45:49

标签: nginx rewrite

我想在nginx中的apache中重写一个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中。到目前为止,我还不知道一个真正的方法来调试正在发生的事情。

1 个答案:

答案 0 :(得分:0)

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

# 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 (...)

我知道nginx世界中的if is evil,但是当它在location {}块中使用时,它的问题也类似于我的虚拟服务器上的一个魅力。