Why does nginx want to access another container, access is denied

I have docker compose like below.

version: '1'
services:
  nginx:
    restart: always
    build: 
      dockerfile: Dockerfile
      context: ./nginx
    container_name: nginx
    hostname: nginx
    ports:
      - "80:80"
    depends_on:
      - server
    networks:
      - musichub_public
  server:
    build: 
      dockerfile: Dockerfile
      context: "./server"
    container_name: nodeserverjs
    hostname: nodeserverjs
    volumes:
      - /app/node_modules
    ports:
      - "5001:5001"
    networks:
      - musichub_net
      - musichub_public
networks:
  musichub_net:
    external: true
  musichub_public:
    driver: bridge

and nginx configuration as below

upstream nodejs_api {
    server nodeserverjs:5001;
}

server {
  listen 80;
  server_name lvh.me; 

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

When it builds successfully, why when nginx wants to access the nodejs api connection is refused? is there something wrong with my configuration? Does this only happen on macOS or on other OS too.?

nginx         | 2023/07/21 12:56:25 [error] 29#29: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: lvh.me, request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.21.0.2:5001/favicon.ico", host: "localhost", referrer: "http://localhost/"

The error message shows only the favicon. Dou you have one? Did the front page work?

Sorry if my question is not clear. this is not for Frontend but Nodejs Api.

At this point it doesn’t matter, because the log is complaining about a favicon which would be tried to load by the browser in an HTML website. Do you have more error messages?

2023/07/31 08:34:18 [error] 30#30: *33 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: lvh.me, request: "GET / HTTP/1.1", upstream: "http://172.20.0.3:5001/", host: "localhost"
musicnginx       | 172.20.0.1 - - [31/Jul/2023:08:34:18 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
musicnginx       | 172.20.0.1 - - [31/Jul/2023:08:34:18 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
musicnginx       | 2023/07/31 08:34:18 [error] 30#30: *33 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: lvh.me, request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.20.0.3:5001/favicon.ico", host: "localhost", referrer: "http://localhost/"

Thanks for the logs. I had to ask for it so I can be sure I work on the real problem.

Can you access the nodejs app directly using the forwarded port?

http://localhost:5001

[SOLVED]
Turns out the problem was in my node js app. before the host in my node js application was localhost and I changed the host to 0.0.0.0 it worked.

// before
 await server.listen({
      port: 5001, // default host is localhost
 });

// after and it worked
 await server.listen({
      port: 5001,
      host: "0.0.0.0"
});