Nginx 重写触发器下载

标签 nginx rewrite

我正在努力在 nginx 上创建一个简单的重写 url。
我的配置如下所示:

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

例如/foo/bar/baz 应该变成 /foo/bar/index.php?p=baz(当然是在内部)

但是,通过 /foo/bar/ 访问的所有内容都会触发下载位于根目录的 index.php。我如何让它工作?

我也尝试过使用 try_files 但不知道如何从 $uri 中排除 /foo/bar/ 路径.

最佳答案

改变

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

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

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

last - completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.

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

关于Nginx 重写触发器下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15420018/

相关文章:

nginx - 使用 IP 访问 nginx 服务器时给出 404 状态,但不使用域

node.js - 无法从 nodemailer 发送电子邮件

nginx - 为pushState-URL重写Nginx

php - 如何配置 nginx 重写规则以使 CakePHP 在 CentOS 上运行?

php - Opencart SEO 打破分类直接

ssl - 如何使用服务器端配置(Nginx)强制浏览器获取新的 SSL 证书而不是旧证书?

node.js - 502 错误网关,docker 内有 nginx 和 Node

django - Docker django nginx gunicorn URL 丢弃端口

.htaccess - Htaccess 使用参数在子文件夹中重写

apache - 为什么我的重写规则中不能有斜杠?