Nginx sees one of the containers but not the others

Hello,
I have three Node.js containers running on ports 3000, 3001, and 3002:

# docker compose ps
NAME         IMAGE             COMMAND                  SERVICE      CREATED          STATUS          PORTS
Admin        yaml-admin        "docker-entrypoint.s…"   admin        20 minutes ago   Up 20 minutes   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp
Contractor   yaml-contractor   "docker-entrypoint.s…"   contractor   20 minutes ago   Up 20 minutes   0.0.0.0:3001->3000/tcp, :::3001->3000/tcp
Nginx        yaml-nginx        "/docker-entrypoint.…"   nginx        20 minutes ago   Up 20 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp
User         yaml-user         "docker-entrypoint.s…"   user         20 minutes ago   Up 20 minutes   0.0.0.0:3002->3000/tcp, :::3002->3000/tcp

Nginx configuration is as follows:

server {
    server_name admin.myhost.comm;
    error_log  /var/log/nginx/error.admin.log;
    access_log /var/log/nginx/access.admin.log;
                      
    location / {
        proxy_pass         http://admin:3000;
        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;
​
    }
}
                      
server {
    server_name contractor.myhost.comm;
    error_log  /var/log/nginx/error.contractor.log;
    access_log /var/log/nginx/access.contractor.log;
                  
    location / {
        proxy_pass         http://contractor:3001;
        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;
    }
}
                      
server {
    server_name user.myhost.comm;
    error_log  /var/log/nginx/error.user.log;
    access_log /var/log/nginx/access.user.log;
                      
    location / {
        proxy_pass         http://user:3002;
        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;
    }
}

In the /etc/hosts file I added the following lines:

127.0.0.1       contractor.myhost.comm
127.0.0.1       admin.myhost.comm
127.0.0.1       user.myhost.comm

It works when I use the curl admin.myhost.comm command, but curl contractor.myhost.comm shows me the following error:

[error] 29#29: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, server: contractor.myhost.comm, request: "GET / HTTP/1.1", upstream: "http://172.22.0.5:3001/", host: "contractor.myhost.comm"

I can see all containers via the curl 127.0.0.1:PORT command.

Why won’t only two containers work?

Thank you.

A common mistake I do, is to not check if the port is already used by another process. So the service is occupied by another service. Find out if your service is really running on this port. Or use another port that you have confirmed is free.

Hello,
Thank you so much for your reply.
All services are running:

# ss -tupln
Netid       State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port      Process                                       
tcp         LISTEN       0            4096                     0.0.0.0:3000                   0.0.0.0:*          users:(("docker-proxy",pid=90412,fd=4))      
tcp         LISTEN       0            4096                     0.0.0.0:3001                   0.0.0.0:*          users:(("docker-proxy",pid=90472,fd=4))      
tcp         LISTEN       0            4096                     0.0.0.0:3002                   0.0.0.0:*          users:(("docker-proxy",pid=90514,fd=4))      
tcp         LISTEN       0            4096                   127.0.0.1:35171                  0.0.0.0:*          users:(("obfs4proxy",pid=18716,fd=3))        
tcp         LISTEN       0            4096                     0.0.0.0:443                    0.0.0.0:*          users:(("docker-proxy",pid=90789,fd=4))      
tcp         LISTEN       0            4096                     0.0.0.0:80                     0.0.0.0:*          users:(("docker-proxy",pid=90824,fd=4))      
tcp         LISTEN       0            4096                        [::]:3000                      [::]:*          users:(("docker-proxy",pid=90427,fd=4))      
tcp         LISTEN       0            4096                        [::]:3001                      [::]:*          users:(("docker-proxy",pid=90478,fd=4))      
tcp         LISTEN       0            4096                        [::]:3002                      [::]:*          users:(("docker-proxy",pid=90528,fd=4))      
tcp         LISTEN       0            4096                        [::]:443                       [::]:*          users:(("docker-proxy",pid=90800,fd=4))      
tcp         LISTEN       0            4096                        [::]:80                        [::]:*          users:(("docker-proxy",pid=90830,fd=4))  

Hi again,
I had forgotten that all services in containers are running on port 3000. I made the following changes and the problem went away:

 proxy_pass         http://contractor:3000;
 ...
 proxy_pass         http://user:3000;

Hello,
These sections are related to microservices. Now what part should be added to this configuration for the main website? For example, What section should be added to this configuration if the main website name is myhost.com?

Hello,
I changed the Nginx configuration as below:

server {
        listen 80;
        server_name localhost.comm;
        error_log  /var/log/nginx/error.system-default.log;
        access_log /var/log/nginx/access.system-default.log;        

        location node1.localhost.comm {
            proxy_pass http://Node1:3000;
            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;
        }

        location node2.localhost.comm {
            proxy_pass http://Node2:3000;
            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;
        }
    }

The /etc/hosts file is as follows:

127.0.0.1       node1.localhost.comm
127.0.0.1       node2.localhost.comm

But I can’t see any of the containers:

# curl node1.localhost.comm
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.25.4</center>
</body>
</html>

The Nginx log is as follows:

2024/05/08 14:14:58 [error] 28#28: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 192.168.32.1, server: localhost.comm, request: "GET / HTTP/1.1", host: "node1.localhost.comm"

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.