2

I'm struggling with creating a simple rewrite url on nginx.
My configuration looks like this:

location /foo/bar {
    rewrite ^/(.+)$ /index.php?p=$1 break;
}

E.g. /foo/bar/baz should become /foo/bar/index.php?p=baz (internally of course)

However, everything accessed through /foo/bar/ triggers a download of an index.php located at the root. How do I get this to work ?

I've also tried using try_files but can't figure out how to exclude the /foo/bar/ path from $uri.

4

2 回答 2

10

改变

rewrite ^/(.+)$ /index.php?p=$1 break;

rewrite ^/(.+)$ /index.php?p=$1 last;

基于http://wiki.nginx.org/HttpRewriteModule#rewrite

last - 完成当前重写指令的处理并重新启动该过程(包括重写),并从所有可用位置搜索 URI 上的匹配项。

[编辑] 使用 break 将留在同一个位置块内。在您的情况下,该位置块内没有其他规则。因此,默认情况下,nginx 会将其作为文件请求提供服务。

于 2013-03-14T21:52:51.613 回答
0

PHP 文件不会被解释和视为常规文件。您将需要设置一个像 FPM 这样的 CGI 服务器并将连接传递给这些进程。

有关详细信息,请参阅http://wiki.nginx.org/Configuration

更新

我不确定你的例子为什么会中断。但我的猜测是你需要将 更改rewritetry_files.

location /foo/bar {
    try_files $uri $uri/ /index.php?p=$uri;
}
于 2013-03-14T21:05:52.657 回答