0

I'm not sure if the behavior I want is actually possible natively with nginx but here goes.

I have a server running on port 81 with the following nginx config:

CONFIGURATION OF SERVER1 NGINX
server {
       listen 81;
       server_name SERVER_DNS_NAME;

       location /server1 {
                proxy_pass http://127.0.0.1:8084/;
                proxy_set_header Host $host;
       }

       location / {
                    proxy_pass http://127.0.0.1:8084;
                    proxy_set_header Host $host:$server_port;
                    }

       }

I have another server running on port 82 with similar configuration. Now what'd i'd like to do is be able to visit them both from port 80 with just different uris.

For example: URL/server1 would take me to the first server, and URL/server2 would take me to the second.

CONFIGURATION OF NGINX LISTENING ON PORT 80
server {
       listen SERVER_IP:80;
       location /server1{
                    proxy_set_header Host $host;
                    http://SERVER_IP:81;


                    }
        location /server2 {
                 proxy_pass http://SERVER_IP:82;
                 proxy_set_header Host $host;
        }

This works fine when I go to URL/server1. I am successfully routed to the main page on server1. However as soon as I click any of the links present on the page on server1 I get a 404. This is because the site tries to go to URL/some_subdir_of_server1 (for which there is no mapping) rather than doing URL/server1/some_subdir_of_server1. Is this behavior doable? If so how?

Thanks!

Mohammad Ahmad
  • 133
  • 1
  • 1
  • 8
  • 1
    This question is asked several times a day. Remove trailing slash from your `proxy_pass` directive parameter. See more info [here](https://stackoverflow.com/questions/53649885/a-little-confused-about-trailing-slash-behavior-in-nginx). – Ivan Shatsky Dec 19 '18 at 22:02
  • Thanks. Also an update for @armsultan. So when I remove the trailing slash in the nginx configuration for server1 (ie "proxy_pass http://127.0.0.1:8084") but leave it in the main configuration I am able to access server1 successfully. However the url presented to the user is URL:81/.... Is there any way that this can be hidden from the user such that they only see URL/server1/... – Mohammad Ahmad Dec 19 '18 at 22:36
  • 1
    It seems that my first comment was completely incorrect. You need to rewrite a response body replacing all absolute links generated by your backend with a new ones. If using a [ngx_http_sub_module](http://nginx.org/en/docs/http/ngx_http_sub_module.html) is a suitable option for you, I can give you several recipes how to do it. Please note that this module is not built by default and should be enabled with the `--with-http_sub_module` configuration parameter while building nginx from source. – Ivan Shatsky Dec 20 '18 at 21:34

1 Answers1

0

Be careful with trailing slashes: in your example, you have proxy_pass http://SERVER_IP:81/; which will set the proxy URL to root /

armsultan
  • 11
  • 2
  • So without that trailing slash I just get 404s. – Mohammad Ahmad Dec 19 '18 at 22:08
  • 1
    Did you enable error logs, any interesting there? do you see where that 404 is going to? – armsultan Dec 19 '18 at 22:10
  • I see: "/usr/share/nginx/html/assets/illustrations/error-404-039d7e84bde2e9d55acc7d6fc759b856983f925e8d52181c55cf22d2435cb8b3.svg" failed (2: No such file or directory).. Not sure what thats even referring to. Why is it trying to load that file? Interestingly when I remove the trailing / the 404 page that i see is actually the one served by server1 – Mohammad Ahmad Dec 19 '18 at 22:13
  • Ok interesting. When I remove the trailing slashes the url that it redirects to is: URL:81/server1 – Mohammad Ahmad Dec 19 '18 at 22:14
  • 1
    cant determine why from your config snippet. there must be a `root` directive pointing to `/usr/share/nginx/html...` where nginx is trying to serve virtual host's assets – armsultan Dec 19 '18 at 22:19
  • No root directive is present in any of the configs. hmm that is odd. So just to be clear. After removing all of the trailing slashes: When I navigate to: URL/server1. I get taken to URL:81 and see the server1 home page (ideally I don't want the user to see the port). Then when I click on a link of server 1, it tries navigating to URL:81/server1 for which there is no mapping – Mohammad Ahmad Dec 19 '18 at 22:24
  • 1
    Posting your complete config might help determine this – armsultan Dec 20 '18 at 14:49
  • Just posted an update. The configs are now the full configs for server1 virtual host and the main server listening on port 80. Thanks for the help! – Mohammad Ahmad Dec 20 '18 at 15:05