0

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).

10
  • The config that you posted listen on port 8080 but your example URL (https://localhost/var) is using the default port (443). There is no way that the configuration you posted is powering that URL on the default port. Commented May 13, 2022 at 15:39
  • Sorry, already corrected. When not using port 8080 i am using 443. Do you have any idea how to fix that issue with two locations? Commented May 13, 2022 at 18:38
  • You edited https to http in your question, but that doesn't change my concern. HTTP uses port 80 by default so you still wouldn't be hitting the service on port 8080 with that URL. Commented May 13, 2022 at 19:00
  • Well, i am using a container backend listening on port 8080 if that answer your question that is why i use proxy pass and port 8080. My question is more related to the locations Commented May 13, 2022 at 20:36
  • What do you have listening on port 80? It doesn't look like the problem is with the configuration you have posted. Commented May 13, 2022 at 21:34

1 Answer 1

0

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/;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Leaving that rewrite I am using on my original code i posted above and port redirect off too?
@XT you don't need to do anything, remove port_in_redirect, rewrite and anything else you've tried to beat this problem, just add / symbol after your port number in proxy_pass and use your normal config.
it is not working the trailing slash in proxy_pass. Getting the same error. redirected to "localhost:8080/var/" and impossible to show the page.
@XT please attach your current nginx configuration
i modified the first post with the current config just the block that is giving issues and server part.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.