Home > Blockchain >  Reverse proxy of multiple container
Reverse proxy of multiple container

Time:11-22

I have 2 API containers (docker) running on port 10000 and 10003. I want to reverse proxy both of them so the API can be called from a single port which is port 80. I am trying to use NGINX to do that and this is my nginx configuration file:

worker_processes 1;
 
events { worker_connections 1024; }
 
http {
    server {
        listen          80;
        server_name     container1;
        location / {
            proxy_pass http://10.10.10.50:10003;
        }
    }

    server {
      listen        80;
      server_name   container2;

      location / {
        proxy_pass http://10.10.10.50:10000;
      }
    }
}

I found that it is only working on the container 1 and if there is a request for container 2, it will generate 404 not found warning because the request go to the container 1 instead of container 2.

CodePudding user response:

Finally, I found a solution using NGINX. All I need to do is to create a new NGINX container then reconfigure the url of my 2 API container. The configuration file that I wrote looks like this:

worker_processes auto;
 
events { worker_connections 1024; }

http {
    upstream container1 {
        server 10.10.10.50:10003;
        }
    upstream container2 {
        server 10.10.10.50:10000;
        }
    server {
        listen 80;

        location /container1/ {
            proxy_pass http://container1/;
        }

        location /container2/ {
            proxy_pass http://container2/;
        }
    }
}

Now, I can make requests for both API containers by using port 80 as it will be re-routed from the port into the designated port (reverse-proxy).

  • Related