Home > Mobile >  How to config nginx so that it listens to one port and proxies requests to other port?
How to config nginx so that it listens to one port and proxies requests to other port?

Time:10-24

I have two docker containers which are running on local Ubuntu machine.

The first one is nodejs service that listen to port 3010, the second one is nginx server on port 2010.

I need to handle all the requests come to port 2010 (matched '/login') and pass them to the first container.

I have nginx.conf as below:

server {
   listen 2010;
   server_name 127.0.0.1; 
   root /usr/share/nginx/html;
   
   location ^~ /login {
      proxy_pass http://127.0.0.1:3010$request_uri;
   }
}

I try to do request from Postman, and get an error:

[error] 29#29: *1 connect() failed (111: Connection refused) while connecting to
 upstream, client: 172.17.0.1, server: 127.0.0.1, request: "GET /login HTTP/1.1",
 upstream: "http://127.0.0.1:3010/login", host: "127.0.0.1:2010"

Where am I wrong and what am I doing not properly?

CodePudding user response:

127.0.0.1 refers to the nginx server/container itself, not any external services/containers.

Doubtful you're running Nodejs processes within the nginx container, so you need to refer to the other container with service names - https://docs.docker.com/network/bridge/

  • Related