UPDATE
I have found out that it works if I restart nginx on the container with docker exec -it backend_gateway_1 nginx -s reload
. I have found some information from this question. It seems that connecting to 127.0.0.1 rather than localhost would work, however I am using docker networks so I'm not sure what my situation would be.
ORIGINAL QUESTION
I am trying to use nginx as a reverse proxy with docker. I cannot quite work out what's wrong as sometimes it works when but then if I use docker-compose down && docker-compose up
to restart it all, i will then usually get this 504 error
I have a feeling this is to do with the nginx or docker configuration. I am able to make a request to / fine but any request to /user is causing the intermittent issue.
When I am not receiving a 504, the node app is working well and creates/returns a user. I am also able to connect to the database fine with mongo-express so that should help narrow something down.
docker-compose.yml
version: '3.5'
services:
gateway:
image: nginx:latest
restart: always
ports:
- 8080:80
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/nginx-error.log:/var/log/nginx/error.log
networks:
- poker_network
poker_mongo:
image: mongo:latest
container_name: poker_mongo
restart: unless-stopped
volumes:
- db-data:/data/db
expose:
- "27017"
environment:
MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: 123
networks:
- poker_network
depends_on:
- gateway
poker_user:
container_name: poker_user
build: ./user/
image: poker/user:latest
volumes:
- ./user/:/usr/src/app
#- /usr/src/app/node_modules
expose:
- "8080"
depends_on:
- gateway
- poker_mongo
networks:
- poker_network
environment:
WAIT_HOSTS: poker_mongo:27017
mongo-express:
container_name: mongo-express
image: mongo-express
restart: always
ports:
- 8085:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: test
ME_CONFIG_MONGODB_ADMINPASSWORD: 123
ME_CONFIG_MONGODB_SERVER: poker_mongo
depends_on:
- gateway
- poker_mongo
networks:
- poker_network
networks:
poker_network:
volumes:
db-data:
nginx.conf
events { worker_connections 1024; }
http {
server {
listen 80;
location /user {
rewrite /user/(.*) /$1 break;
proxy_pass http://poker_user:8080/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
location / {
return 200 'hello';
}
}
}
Any help is appreciated, thanks.