Search code examples
dockernginxdocker-machinenginx-locationnginx-reverse-proxy

How to configure NGINX Location for Multiple Service


I want to configure NGINX to work as a reverse proxy to other Microservices.

I am able to forward the request from NGINX to one of the microservice

if I do curl http://xx.xx.xx.xx:8080/ call did landed on consumer-portal But its using default location configuration /

when I comment the 1st block and configure the same code for location /consumer-portal and do curl http://xx.xx.xx.xx:8080/consumer-portal

I get :

Cannot GET /consumer-portal

I have more than 10 microservice which I want to call using NGINX.

Below is my nginx.conf file

worker_processes 4;
events { 
        worker_connections 1024; 
    }

http {
    sendfile on;
    upstream consumer-portal {

         server xx.xx.xx.xx:9006;

    }
    upstream publisher-portal {

         server xx.xx.xx.xx:9001;
    }
    server {
        listen 8080;

        #1st Block
        #location / {
        #    proxy_pass         http://consumer-portal;
        #    proxy_redirect     off;
        #    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;
        #}

        #2nd Block
        location  /consumer-portal {
            proxy_pass         http://consumer-portal;
            proxy_redirect     off;
            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;
        }

        #3rd Block
        location /publisher-portal/ {
             proxy_pass         http://publisher-portal;
             proxy_redirect     off;
             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;
         }
}
}

Also, Please let me know If I can forward the request using docker container name.

e.g instead of server xx.xx.xx.xx:9006 i want to use server consumer-portal:9006

Please suggest what changes I need to do into .conf file.


Solution

  •     location  /consumer-portal {
            proxy_pass         http://consumer-portal;
    

    If your proxy_pass URL is just a domain/IP/hostname and has no URI set then Nginx will pass the full client request URL to the proxy for requests matching the location block. So here your request to

    http://xx.xx.xx.xx:8080/consumer-portal will be proxied by Nginx to

    http://consumer-portal/consumer-portal

    If your proxy_pass URL is a domain/IP/hostname which also has a URI appended then Nginx will replace the matching part of your location block from the original client request with the URI in your directive and then proxy the resulting URL to the upstream server. So if you had proxy_pass http://consumer-portal/new/location; then a request to

    http://xx.xx.xx.xx:8080/consumer-portal/account would be proxied by Nginx to

    http://consumer-portal/new/location/account

    As you want to remove /consumer-portal from the request to the upstream proxy the solution is as simple as adding a trailing slash to your proxy_pass directive, like this:

    proxy_pass http://consumer-portal/;