Nginx – Impossible to fix nginx redirecting to port 8080 when accessing url without slash

nginxPROXYredirect

I've been trying to fix this so annoying error, but didn't work.
I followed and tried many ways on this post Nginx redirects to port 8080 when accessing url without slash but its still there.

I'm using nginx ad reverse proxy for apache.
Here is my nginx configure file:

server {

        listen   80; ## listen for ipv4
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        server_name  domain.com *.domain.com;

        #access_log  /var/log/nginx/localhost.access.log;

        location / {
                proxy_pass http://127.0.0.1:8080/;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ~* \.(jpg|jpeg|png|gif|png|css|js|swf)$ {
        root /var/www/html/domain/;
        }
}

and http

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  /var/log/nginx/access.log  main;
    access_log off;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;

    keepalive_timeout  15;
    client_max_body_size 5M;

    gzip  on;
    gzip_min_length 1100;
    gzip_buffers 4 32k;
    gzip_types text/plain application/x-javascript text/xml text/css;

    proxy_cache_path  /var/cached levels=2:2 keys_zone=cache:1024m inactive=1d max_size=3900m;
    server_name_in_redirect off;
    server_names_hash_max_size 2048;
    server_names_hash_bucket_size 256;
    server_tokens off;
    ignore_invalid_headers on;
    client_header_timeout  3m;
    client_body_timeout 3m;
    send_timeout     3m;
    reset_timedout_connection on;
    connection_pool_size  256;
    client_header_buffer_size 256k;
    large_client_header_buffers 4 256k;
    request_pool_size  32k;
    output_buffers   4 32k;
    postpone_output  1460;
    fastcgi_hide_header X-Powered-By;

    include /etc/nginx/conf.d/*.conf;
}

Best Answer

This may be related to your backend software, that writes port 8080 into it's output. If there is no option to stop doing that, you can solve the problem by running backend on port 80 as well. Just make sure you explicitly specify in nginx.conf to listen on external IPs only, while backend(s) should listen only on local IPs.

In nginx.conf, find every listen directive without any IP or 0.0.0.0 as IP and change it to have one:

listen 80; ## listen for ipv4

change to:

listen <your external IP>:80; ## listen for ipv4

In apache configuration find every Listen & VirtualHost directive & make sure they listen on port 80 of a local IP:

Listen 8080

change to

Listen 127.0.0.1:80

and

<VirtualHost 0.0.0.0:8080>

to

<VirtualHost 127.0.0.1:80>

Finally, change proxy_pass directive in nginx.conf to the new backend address:

proxy_pass http://127.0.0.1:80/;

If you want to have several backends, just place them on any other local IP (127.0.0.0/8) like this:

<VirtualHost 127.0.0.2:80>
<VirtualHost 127.0.0.3:80>
<VirtualHost 127.0.0.4:80>

nginx.conf's proxy_pass would be:

proxy_pass http://127.0.0.2:80/;
proxy_pass http://127.0.0.3:80/;
proxy_pass http://127.0.0.4:80/;

etc.