Nginx重写触发器下载 [英] Nginx rewrite triggers download

查看:72
本文介绍了Nginx重写触发器下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

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

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

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

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

我也尝试过使用try_files,但无法弄清楚如何从$uri中排除/foo/bar/路径.

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

推荐答案

更改

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

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

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

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

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将保留在同一位置块内.在您的情况下,该位置块内没有其他规则.因此,默认情况下,nginx会将其作为文件请求.

Using break will stay inside the same location block. In your case, there is no other rules inside that location block. So by default, nginx will serve that as a file request.

这篇关于Nginx重写触发器下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆