'Not able to see URL without trailing slash in NGINX

I know that this is a common issue in NGINX and there are many threads about that.

Issue:

When accessing the URL http://localhost/var without trailing slash is working with my current config. However, I need to add 2 locations (one with trailing slash and second one without trailing slash).

If i try accessing the URL with trailing slash, it is being redirected correctly and page is displayed correctly.

The issue comes when i try accessing the second URL:

http://localhost/var/api/app/v2

I did the same, adding two locations (one with trailing slash and second one without trailing slash). However, looks like the rewrite that i included inside server block is making a conflict as the url has some stuff behind "var". I am getting an 404 error.

This is my current config file:

server {
        listen       8080;
        listen  [::]:8080;
        server_name  localhost;
        rewrite ^/(.*)/$ /$1 permanent;
        absolute_redirect off;
        access_log  /var/log/nginx/access.log  main;


        ##location 1##

        location /##var##/ {
            set $dash local.##var##.svc;
            rewrite ^/##var##(.*)$ $1 break;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://$dash:8080;
            auth_basic_user_file /etc/apache2/dash_auth;
        }


        location /##var## {
            set $dash local.##var##.svc;
            rewrite ^/##var##/(.*)$ $1 break;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://$dash:8080/;
            auth_basic_user_file /etc/apache2/dash_auth;
        }

         ##location 2##

        location /##var##/api/app/v2/ {
            set $nu local2.##var##.svc;
            rewrite ^/##var##/api/v2/app/(.*)$ /app/$1 break;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://$nu:6000;
            auth_basic_user_file /etc/apache2/dash_auth;
        }


        location /##var##/api/app/v2 {
            set $nu local2.##var##.svc;
            rewrite ^/##var##/api/app/v2(.*)$ /app/$1 break;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://$nu:6000/;
            auth_basic_user_file /etc/apache2/dash_auth;
        }


}

This is working when accessing the URL:

http://localhost/var

but not when accessing the URL:

http://localhost/var/api/app/v2

i think the conflict is regarding the rewrite in the server but not sure how can i fix that.

I tried to add the rewrite inside the location for /var/ but not working as expected. I was thinking about include a specifyc rewrite for every location but not sure if this is gonna work.

Also I trid to add these two rewite rules in server block, but not working:

rewrite ^/api/app/(.*)/$ /api/app/$1 permanent;
rewrite ^/(.*)/$ /$1 permanent;

Regarding the ports, that is something currently working. i mean without including any modification in the nginx config it is working (just with URL with trailing slash although).



Solution 1:[1]

Change

proxy_pass http://$dash:8080;

To

proxy_pass http://$dash:8080/;

Also your default nginx location should looke like this

location / {
    auth_basic_user_file /etc/apache2/dash_auth;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://$dash:8080/;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1