在不使用斜线访问url时,Nginxredirect到端口8080

访问时: http : //example.com/somefolder – > http://example.com:8080/somefolder

我试过这个:

http { port_in_redirect off; 

有任何想法吗?

我只是遇到了同样的问题和port_in_redirect off; 实际上为我工作,只要确保您在server {}块内使用它。

 server { listen 8080; server_name example.com; port_in_redirect off; autoindex on; location / { root /var/www/example.com; index index.html; } } 

这应该解决问题。 在proxy_pass指令后面添加proxy_redirect指令

proxy_redirect http://example.com:8080/ http://example.com/;

如果有人在使用nginx反向代理设置的时候遇到了这个问题,你可以试试ff:

  位置 / {
     proxy_redirectclosures;
     proxy_set_header主机$主机:$服务器端口;  #< - 这个解决了我的问题
     proxy_set_header X-Forwarded-Host $ http_host;
     proxy_set_header X-Real-IP $ remote_addr;
     proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
     proxy_pass http://127.0.0.1:8083 ;
   }
  

我的设置是让Apache监听127.0.0.1:8083,并让nginx代理请求它。

我和我的nginx + Apache安装程序有同样的问题。 Apache似乎将redirect到它自己的端口(在8080上运行),而nginx在端口80上。

在我的设置中,这为正常的 url创build了无限的redirect循环

 proxy_set_header Host $host:80; # Force port 80 

而是将返回的数据绑定到端口80,如下所示:

 proxy_bind $host:80; # Bind to port 80 

这是我的nginx服务器块:

 server { listen 80; listen [::]:80 ipv6only=on; server_name _; # Wildcard server location / { proxy_bind $host:80; # Bind to port 80 << THIS IS THE MAGIC proxy_pass http://localhost:8080; proxy_set_header Host $host; # Pass host header proxy_set_header X-Real-IP $remote_addr; # Preserve client IP proxy_set_header X-Forwarded-For $remote_addr; } } 

通过这个通配符设置,所有的请求nginx没有一个服务器块被传递给Apache。

http://wiki.codemongers.com/NginxHttpCoreModule#port_in_redirect

 syntax: port_in_redirect [ on|off ] default: port_in_redirect on context: http, server, location 

希望这是你在找什么。

我有一个类似的问题。

也许你应该使用伪指令proxy_set_header来设置头文件的主机和位置。 在我的configuration中,我添加了proxy_set_header Location $host:80 ; 所有的请求现在返回端口80。

这个问题是由Nginx自动将相对path转换为绝对path引起的,对吗?
有一种方法可以告诉Nginx不需要翻译,你可以在斜杠前加一个空格: ngx.redirect(" /foo")

以防其他人有这个问题 – 这里的文章: http : //www.linuxquestions.org/questions/linux-server-73/strange-nginx-redirects-without-trailing-slash-930876/解决了我的问题。

我不得不在代理中添加一个额外的头文件。

确保你的主机设置为$http_host ,并且你已经将X-Forwarded-Host$http_host如下所示:

proxy_redirect off; proxy_set_header Host $http_host; # <-- make sure this is $http_host proxy_set_header X-Forwarded-Host $http_host; # <-- make sure you set this proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 16k; proxy_buffers 32 8k; proxy_busy_buffers_size 64k;

我会build议,而不是添加到您的服务器块:

 rewrite /wp-admin$ $scheme://$host$uri/ permanent; 

这是当你去example.com/wp-admin它将去example.com/wp-admin,而不是去example.com:8080/wp-admin/,如果你在CloudFlare的DNS例如它会说该网站已closures。

我使用的是Ubuntu,Nginx和Varnish,现在完全可以工作,而不是禁用port_in_redirect。

要添加,请确保在以下情况下重新启动nginx:

 sudo service nginx restart