How to Fix Nginx Redirecting to Port 8080 When Accessing URLs Without Trailing Slash


1 views

Recently I encountered a peculiar issue where Nginx was adding port 8080 to URLs when accessing directories without trailing slashes. For example:

Original request: http://example.com/somefolder
Redirected to:   http://example.com:8080/somefolder

This behavior occurred despite having port_in_redirect off; in my http block configuration.

After digging through Nginx documentation and testing various configurations, I discovered this typically happens when:

  1. The backend server is actually listening on port 8080
  2. Nginx is configured to proxy to port 8080
  3. There's no explicit configuration for handling directory access

Here are several working solutions I've tested:

1. Force trailing slashes

location ~* ^[^.]*$ {
    if (-d $request_filename) {
        rewrite [^/]$ $uri/ permanent;
    }
}

2. Explicit proxy configuration

location /somefolder/ {
    proxy_pass http://backend:8080/somefolder/;
    proxy_redirect http://$host:8080/ http://$host/;
}

3. Full proxy redirect control

location / {
    proxy_pass http://backend:8080;
    proxy_redirect http://$host:8080/ http://$host/;
    port_in_redirect off;
}
  1. Always test with curl -v to see redirects
  2. Browser cache can sometimes show old redirects - clear it
  3. The proxy_redirect directive is crucial for proper port handling

Here's a complete working configuration I used for a Node.js application:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect http://$host:8080/ http://$host/;
    }
}

This configuration ensures all redirects maintain the correct port (80) regardless of the backend server's port (8080).


When working with Nginx configurations, you might encounter a peculiar redirection behavior where accessing a URL like http://example.com/somefolder automatically redirects to http://example.com:8080/somefolder. This port appending behavior occurs specifically when:

  • The URL lacks a trailing slash
  • The destination is a directory (not a file)
  • The server has multiple port configurations

While port_in_redirect off; is indeed the correct directive to prevent port numbers in redirects, several factors can override this setting:

server {
    listen 80;
    server_name example.com;
    
    # This should theoretically work but might be overridden
    port_in_redirect off;
    
    location /somefolder/ {
        proxy_pass http://backend:8080/;
    }
}

Solution 1: Force Trailing Slash

server {
    listen 80;
    server_name example.com;

    location ~* ^(/somefolder[^/])$ {
        return 301 $1/;
    }

    location /somefolder/ {
        proxy_pass http://backend:8080/;
    }
}

Solution 2: Absolute URL Rewrites

location /somefolder {
    rewrite ^/somefolder(.*)$ http://$host/somefolder/$1 permanent;
}

Solution 3: Proxy Redirection Control

location /somefolder {
    proxy_redirect http://$host:8080/ http://$host/;
    proxy_pass http://backend:8080;
}

To diagnose the exact redirection flow:

  1. Enable debug logging in Nginx: error_log /var/log/nginx/error.log debug;
  2. Check response headers with curl -v http://example.com/somefolder
  3. Verify all server blocks that might handle the request
  • Multiple server blocks listening on different ports
  • Proxy modules altering the Host header
  • Missing trailing slash in proxy_pass destination
  • Conflicts with try_files directive