ardhicz
(Ardhicz)
July 21, 2023, 1:15pm
1
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/"
rimelek
(Ákos Takács)
July 22, 2023, 8:45am
2
The error message shows only the favicon. Dou you have one? Did the front page work?
ardhicz
(Ardhicz)
July 22, 2023, 9:29am
3
Sorry if my question is not clear. this is not for Frontend but Nodejs Api.
rimelek
(Ákos Takács)
July 22, 2023, 4:01pm
4
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?
ardhicz
(Ardhicz)
July 31, 2023, 8:34am
5
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/"
rimelek
(Ákos Takács)
July 31, 2023, 6:16pm
6
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
ardhicz
(Ardhicz)
August 1, 2023, 1:33am
7
[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"
});