琐碎检查:
$ 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
如您所见,在.scheme 中只有一个斜杠Location
。
将以下指令添加到后server
:
merge_slashes off;
我们会得到正确的答复:
$ 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
从评论中可以清楚地看出,您可能希望将不带架构的主机名传递给重定向服务。要解决此问题,您需要定义两个位置来分别处理这两种情况:
server {
listen 80;
server_name localhost;
merge_slashes off;
location /rdr {
location /rdr/http:// {
rewrite ^/rdr/(.*)$ $1 permanent;
}
rewrite ^/rdr/(.*)$ http://$1 permanent;
}
}
在这里,我定义/rdr/http://
了一个子位置,只是为了将重定向器服务保持在一个块中——在 -级别/rdr
创建两个位置是完全有效的。server