Nginx redirects to port 8080 when accessing url without slash [closed]

I just ran into the same problem and port_in_redirect off; actually worked for me, just make sure you use it inside the server {} block.

server {
  listen 8080;
  server_name example.com;

  port_in_redirect off;
  autoindex on;

  location / {
    root /var/www/example.com;
    index index.html;
  }
}

This should fix problem. Add proxy_redirect directive right after proxy_pass directive

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


If someone is still experiencing this problem while having apache behind nginx reverse proxy setup, you could try the ff:

  location / {
    proxy_redirect off;
    proxy_set_header Host $host:$server_port; # <-- this one solved mine
    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;
  }
  

My setup is that I let Apache listen to 127.0.0.1:8083 and let nginx proxy requests to it.


I had the same problem with my nginx + Apache setup. Apache seems to be redirecting to it's own port (running on 8080), while nginx is on port 80.

In my setup, this made infinite redirect loop for normal urls:

proxy_set_header Host $host:80; # Force port 80

Instead bind the returning data to port 80, like this:

proxy_bind $host:80; # Bind to port 80

Here is my nginx server block:

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;
    }
}

With this wildcard setup, all requests nginx does not have a server block for is passed on to Apache.