Jür*_*aul 5 redirect rewrite nginx
我正在尝试做的是将所有请求路由/rdr/extern_url到extern_url通过我的Web服务器重定向,而不是通过PHP进行.
location /rdr {
rewrite ^/rdr/(.*)$ $1 permanent;
}
Run Code Online (Sandbox Code Playgroud)
这有什么问题,如果我访问http://localhost/rdr/http://google.com我的浏览器告诉我:
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
Run Code Online (Sandbox Code Playgroud)
如何正确重定向?
琐碎的检查:
$ curl -si 'http://localhost/rdr/http://www.google.com' | head -8
HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.0
Date: Sun, 05 Aug 2012 09:33:14 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http:/www.google.com
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,在计划之后只有一个斜线Location.
将以下指令添加到server:
merge_slashes off;
Run Code Online (Sandbox Code Playgroud)
我们会得到正确的答复:
$ curl -si 'http://localhost/rdr/http://www.google.com' | head -8
HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.0
Date: Sun, 05 Aug 2012 09:36:56 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.google.com
Run Code Online (Sandbox Code Playgroud)
从注释中可以清楚地看出,您可能希望在没有架构的情况下将主机名传递给重定向服务.要解决此问题,您需要定义两个位置以分别处理两个案例:
server {
listen 80;
server_name localhost;
merge_slashes off;
location /rdr {
location /rdr/http:// {
rewrite ^/rdr/(.*)$ $1 permanent;
}
rewrite ^/rdr/(.*)$ http://$1 permanent;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我将其定义/rdr/http://为/rdr仅将重定向器服务保留在一个块中的子位置- 在server-level 创建两个位置是完全有效的.