skip to Main Content

I am trying to get my nginx reverse proxy to pass to 1 of 2 apache webservers. But there is a problem with my setup. When I want to connect to ‘localhost/central/api’ it only works if I go to ‘localhost/central/api/’ (the trailing slash).
Otherwise the url is changed to ‘localhost/central/’ again.

It seems weird that this is needed/wanted, I don’t know how to fix this.
It does not seem to be caused by the apache apps.
I don’t want the last trailing slash to be needed and I also don’t want nginx to add this to the end of the url when passing to my apache services.

This is the relevant part of my config file for nginx.

server {
    listen 80;
    server_name localhost;

    location ^~ /central {
        proxy_pass http://central/;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }

    location ^~ /microservices {
        proxy_pass http://microservices/;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }

}

Have tried using rewrite rules instead but this also didn’t fix it.

2

Answers


  1. Chosen as BEST ANSWER

    So apparently I should expect apache to send this redirect, but my browser was not handling it properly because it was doing some sort of internal caching.

    Also the redirects did not have the 'central' or 'microservices' subpath. The redirect would give a uri of localhost/api.

    I fixed this using a proxy redirect rule:

     proxy_redirect http://central http://localhost/central;
    

  2. If you configure no URI to the proxy_pass directive, NGINX will simply pass the same URI as the requested to the proxied service.

    Reference: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

    So basically remove the trailing slash. Also, I would add a trailing slash to the location, otherwise things like /central-park/api will also match:

    server {
        listen 80;
        server_name localhost;
    
        location ^~ /central/ {
            proxy_pass http://central;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    
        location ^~ /microservices/ {
            proxy_pass http://microservices;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search