Redirect Nginx重定向到外部URL
我试图做的是将所有请求路由到Redirect Nginx重定向到外部URL,redirect,nginx,rewrite,Redirect,Nginx,Rewrite,我试图做的是将所有请求路由到/rdr/extern\u url,通过我的web服务器重定向到extern\u url,而不是通过PHP location /rdr { rewrite ^/rdr/(.*)$ $1 permanent; } 如果我访问http://localhost/rdr/http://google.com我的浏览器告诉我: Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
/rdr/extern\u url
,通过我的web服务器重定向到extern\u url
,而不是通过PHP
location /rdr {
rewrite ^/rdr/(.*)$ $1 permanent;
}
如果我访问http://localhost/rdr/http://google.com
我的浏览器告诉我:
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
如何正确重定向?琐碎检查:
$ 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后面只有一个斜杠
将以下指令添加到服务器后
:
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
的一个子位置,只是为了将重定向器服务保留在一个块中——在服务器
级别创建这两个位置是完全有效的。我尝试在服务器上下文中添加合并
斜线,但仍然得到一个斜线(在重新启动nginx之后)。顺便说一句,使用curl的好主意,我只在我的浏览器上测试它们。你可以看到它在我的设置中无缝地工作。我建议你把问题定位。只使用服务器
重新设置相同的Nginx版本,看看会发生什么。检查“错误”日志,打开调试日志。如果开始时没有http://
,该怎么办<代码>http://localhost/rdr/google.com
如果开始时您没有http://
,那么它将是301
和位置:www.google.com
,这意味着您的浏览器将请求http://localhost/www.google.com
(请不要删除您的评论:这让读者很难理解讨论的流程)您对如何解决这个问题有何想法?