我有一个运行在80端口上的Nginx服务器,作为Apache 2.2的代理工作,它正在监听127.0.0.1:8080
当我访问http://hostname/subfolder/它很好。
当我访问http://hostname/subfolder它redirect到http://hostname:8080/subfolder/这是错误的。
据我看到错误的redirect是由Apache返回,但UseCanonicalName和UseCanonicalPhysicalProxy都设置为Off
任何想法如何解决这个问题?
我也遇到了这个问题,我可以在我的nginxconfiguration文件的proxy_pass指令后面用一个proxy_redirect指令修复它:
proxy_redirect http://example.com:8080/ http://example.com/
这是我完整的nginxconfiguration(在我的情况下,Apache是在端口81上,并托pipe两个网站。我添加了两个特定于站点的proxy_redirect行,因为我不知道如何添加一个通用的。
server { listen 80; access_log /var/log/nginx/apache-proxy.access.log; location / { proxy_pass http://localhost:81; #fix for apache redirects that include the port number proxy_redirect http://nfriedly.com:81/ http://nfriedly.com/; proxy_redirect http://misticflame.com:81/ http://misticflame.com/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 6000; proxy_send_timeout 6000; proxy_read_timeout 6000; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; send_timeout 6000; proxy_buffering off; proxy_next_upstream error; } }
注意:这是针对5年前的nginx 1.0之前的版本。 以下是当前版本的proxy_redirect文档: http ://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
如果Apache上的ServerName指令设置为“hostname:8080”,则删除“:8080”或将其更改为“hostname:80”。 你也可以添加“proxy_set_header Host $ host:80”
我很久以前就有这个问题了。我记得它和HTTP RFC有关,最后的斜杠表示一个目录(/ test /),在文件末尾没有斜线(/ test)
长话短说,添加一个重写规则,将添加一个尾随斜线的请求,如果没有。
看看解决:Nginx服务器的尾随斜线问题
HTP
我最近遇到了这个确切的问题。 虽然build议的解决scheme将工作,Nginx提供了一个内置的解决scheme:
proxy_redirect default;
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect上的文档
以下是如何使用它的完整示例:
server { listen 80; location / { proxy_pass http://localhost:8080 proxy_redirect default } }
也许nginx没有设置代理头告诉Apache什么原始请求看起来像。
在nginx中:
proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;