504 response from ngingx reverse proxy with nodejs + dockerDocker Network Nginx ResolverHow do I return the response from an asynchronous call?How is Docker different from a virtual machine?How to get a Docker container's IP address from the host?Copying files from Docker container to hostCopying files from host to Docker containerHow to copy Docker images from one host to another without using a repositoryFrom inside of a Docker container, how do I connect to the localhost of the machine?Why entrypoint is set to 'bin/true' in docker-compose.ymlCan't connect to MongoDB container from other Docker containernginx docker compose redirect delay

How can I make dummy text (like lipsum) grey?

Why would company (decision makers) wait for someone to retire, rather than lay them off, when their role is no longer needed?

Do high-wing aircraft represent more difficult engineering challenges than low-wing aircraft?

Is Precocious Apprentice enough for Mystic Theurge?

When did Britain learn about American independence?

How to find the radius of this smaller circle?

Do we see some Unsullied doing this in S08E05?

Canadian citizen who is presently in litigation with a US-based company

Divisor Rich and Poor Numbers

Why can't I share a one use code with anyone else?

How long do Aarakocra live?

Is it standard to have the first week's pay indefinitely withheld?

Why is vowel phonology represented in a trapezoid instead of a square?

Why were the bells ignored in S8E5?

Could a space colony 1g from the sun work?

A person lacking money who shows off a lot

Is it possible to pass a pointer to an operator as an argument like a pointer to a function?

Physically unpleasant work environment

Solenoid fastest possible release - for how long should reversed polarity be applied?

Why didn't Daenerys' advisers suggest assassinating Cersei?

Why is so much ransomware breakable?

Why are lawsuits between the President and Congress not automatically sent to the Supreme Court

Can I pay my credit card?

Capital gains on stocks sold to take initial investment off the table



504 response from ngingx reverse proxy with nodejs + docker


Docker Network Nginx ResolverHow do I return the response from an asynchronous call?How is Docker different from a virtual machine?How to get a Docker container's IP address from the host?Copying files from Docker container to hostCopying files from host to Docker containerHow to copy Docker images from one host to another without using a repositoryFrom inside of a Docker container, how do I connect to the localhost of the machine?Why entrypoint is set to 'bin/true' in docker-compose.ymlCan't connect to MongoDB container from other Docker containernginx docker compose redirect delay






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















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.










share|improve this question
























  • Are you sure your node app doesn’t sporadically fails to handle request and doesn’t return any response? You could try removing all logic from /user handler and return 200 status code and see if you can reproduce the error. If you could add handler code that would also help.

    – Igor Nikolaev
    Mar 23 at 17:20












  • yes sorry I meant to mention I have already tried this and It gets 504. It doesn't start out working, it either does work or doesn't and I get no output in the logs of the node app failing. I will try create a simple example to add

    – Emobe
    Mar 23 at 17:27











  • @IgorNikolaev I've added some updates with what I've found out

    – Emobe
    Mar 24 at 11:43












  • You could try to change the order of dependencies - gateway should depend on poker_user

    – Igor Nikolaev
    Mar 24 at 11:50











  • @IgorNikolaev Thank you, this worked. I seemed to have the idea about depends_on backwards when it came to NGINX. Could you please add this as an answer?

    – Emobe
    Mar 24 at 12:58

















0















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.










share|improve this question
























  • Are you sure your node app doesn’t sporadically fails to handle request and doesn’t return any response? You could try removing all logic from /user handler and return 200 status code and see if you can reproduce the error. If you could add handler code that would also help.

    – Igor Nikolaev
    Mar 23 at 17:20












  • yes sorry I meant to mention I have already tried this and It gets 504. It doesn't start out working, it either does work or doesn't and I get no output in the logs of the node app failing. I will try create a simple example to add

    – Emobe
    Mar 23 at 17:27











  • @IgorNikolaev I've added some updates with what I've found out

    – Emobe
    Mar 24 at 11:43












  • You could try to change the order of dependencies - gateway should depend on poker_user

    – Igor Nikolaev
    Mar 24 at 11:50











  • @IgorNikolaev Thank you, this worked. I seemed to have the idea about depends_on backwards when it came to NGINX. Could you please add this as an answer?

    – Emobe
    Mar 24 at 12:58













0












0








0








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.










share|improve this question
















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.







javascript node.js docker nginx docker-compose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 11:48







Emobe

















asked Mar 23 at 16:36









EmobeEmobe

308420




308420












  • Are you sure your node app doesn’t sporadically fails to handle request and doesn’t return any response? You could try removing all logic from /user handler and return 200 status code and see if you can reproduce the error. If you could add handler code that would also help.

    – Igor Nikolaev
    Mar 23 at 17:20












  • yes sorry I meant to mention I have already tried this and It gets 504. It doesn't start out working, it either does work or doesn't and I get no output in the logs of the node app failing. I will try create a simple example to add

    – Emobe
    Mar 23 at 17:27











  • @IgorNikolaev I've added some updates with what I've found out

    – Emobe
    Mar 24 at 11:43












  • You could try to change the order of dependencies - gateway should depend on poker_user

    – Igor Nikolaev
    Mar 24 at 11:50











  • @IgorNikolaev Thank you, this worked. I seemed to have the idea about depends_on backwards when it came to NGINX. Could you please add this as an answer?

    – Emobe
    Mar 24 at 12:58

















  • Are you sure your node app doesn’t sporadically fails to handle request and doesn’t return any response? You could try removing all logic from /user handler and return 200 status code and see if you can reproduce the error. If you could add handler code that would also help.

    – Igor Nikolaev
    Mar 23 at 17:20












  • yes sorry I meant to mention I have already tried this and It gets 504. It doesn't start out working, it either does work or doesn't and I get no output in the logs of the node app failing. I will try create a simple example to add

    – Emobe
    Mar 23 at 17:27











  • @IgorNikolaev I've added some updates with what I've found out

    – Emobe
    Mar 24 at 11:43












  • You could try to change the order of dependencies - gateway should depend on poker_user

    – Igor Nikolaev
    Mar 24 at 11:50











  • @IgorNikolaev Thank you, this worked. I seemed to have the idea about depends_on backwards when it came to NGINX. Could you please add this as an answer?

    – Emobe
    Mar 24 at 12:58
















Are you sure your node app doesn’t sporadically fails to handle request and doesn’t return any response? You could try removing all logic from /user handler and return 200 status code and see if you can reproduce the error. If you could add handler code that would also help.

– Igor Nikolaev
Mar 23 at 17:20






Are you sure your node app doesn’t sporadically fails to handle request and doesn’t return any response? You could try removing all logic from /user handler and return 200 status code and see if you can reproduce the error. If you could add handler code that would also help.

– Igor Nikolaev
Mar 23 at 17:20














yes sorry I meant to mention I have already tried this and It gets 504. It doesn't start out working, it either does work or doesn't and I get no output in the logs of the node app failing. I will try create a simple example to add

– Emobe
Mar 23 at 17:27





yes sorry I meant to mention I have already tried this and It gets 504. It doesn't start out working, it either does work or doesn't and I get no output in the logs of the node app failing. I will try create a simple example to add

– Emobe
Mar 23 at 17:27













@IgorNikolaev I've added some updates with what I've found out

– Emobe
Mar 24 at 11:43






@IgorNikolaev I've added some updates with what I've found out

– Emobe
Mar 24 at 11:43














You could try to change the order of dependencies - gateway should depend on poker_user

– Igor Nikolaev
Mar 24 at 11:50





You could try to change the order of dependencies - gateway should depend on poker_user

– Igor Nikolaev
Mar 24 at 11:50













@IgorNikolaev Thank you, this worked. I seemed to have the idea about depends_on backwards when it came to NGINX. Could you please add this as an answer?

– Emobe
Mar 24 at 12:58





@IgorNikolaev Thank you, this worked. I seemed to have the idea about depends_on backwards when it came to NGINX. Could you please add this as an answer?

– Emobe
Mar 24 at 12:58












1 Answer
1






active

oldest

votes


















1














I found several similar related questions and will try to compile two possible solutions out of those:



  • https://serverfault.com/questions/240476/how-to-force-nginx-to-resolve-dns-of-a-dynamic-hostname-everytime-when-doing-p

  • Docker Network Nginx Resolver

First solution



First solution is to reverse dependency between services: instead of poker_user depend on gateway you can may gateway depend on user. This way when gateway starts if will be able to resolve poker_user hostname.



The Dockerfile would look this way in this case:



version: '3.5'
services:
gateway:
image: nginx:latest
restart: always
ports:
- 8080:80
depends_on: poker_user
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
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:
- 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
networks:
- poker_network

networks:
poker_network:
volumes:
db-data:


I also removed some other dependencies as I think they are redundant.



Second solution



This solution is based on changing nginx configuration. It looks like nginx tries to resolve hostname specified in proxy_pass upfront and since it's not available yet when nginx starts it will keep responding with 504 because it doesn't try to resolve it again.



There seems to be a workaround with using variables inside proxy_pass directive which are treated differently and force nginx to perform DNS resolution again. This requires resolver to be configured in nginx and luckily there's one built into Docker when using user-defined networks.



Here's an example nginx configuration (though I haven't tested it myself):



events worker_connections 1024; 

http

server
listen 80;
location /user
# Docker DNS server in user-defined networks
# https://docs.docker.com/v17.09/engine/userguide/networking/configure-dns/
resolver 127.0.0.11 ipv6=off;

rewrite /user/(.*) /$1 break;

# Using a variable will force nginx to re-resolve DNS name
# https://serverfault.com/a/593003
set $backend "http://poker_user:8080/";
proxy_pass $backend;

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';






Hope this help!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315958%2f504-response-from-ngingx-reverse-proxy-with-nodejs-docker%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I found several similar related questions and will try to compile two possible solutions out of those:



    • https://serverfault.com/questions/240476/how-to-force-nginx-to-resolve-dns-of-a-dynamic-hostname-everytime-when-doing-p

    • Docker Network Nginx Resolver

    First solution



    First solution is to reverse dependency between services: instead of poker_user depend on gateway you can may gateway depend on user. This way when gateway starts if will be able to resolve poker_user hostname.



    The Dockerfile would look this way in this case:



    version: '3.5'
    services:
    gateway:
    image: nginx:latest
    restart: always
    ports:
    - 8080:80
    depends_on: poker_user
    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
    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:
    - 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
    networks:
    - poker_network

    networks:
    poker_network:
    volumes:
    db-data:


    I also removed some other dependencies as I think they are redundant.



    Second solution



    This solution is based on changing nginx configuration. It looks like nginx tries to resolve hostname specified in proxy_pass upfront and since it's not available yet when nginx starts it will keep responding with 504 because it doesn't try to resolve it again.



    There seems to be a workaround with using variables inside proxy_pass directive which are treated differently and force nginx to perform DNS resolution again. This requires resolver to be configured in nginx and luckily there's one built into Docker when using user-defined networks.



    Here's an example nginx configuration (though I haven't tested it myself):



    events worker_connections 1024; 

    http

    server
    listen 80;
    location /user
    # Docker DNS server in user-defined networks
    # https://docs.docker.com/v17.09/engine/userguide/networking/configure-dns/
    resolver 127.0.0.11 ipv6=off;

    rewrite /user/(.*) /$1 break;

    # Using a variable will force nginx to re-resolve DNS name
    # https://serverfault.com/a/593003
    set $backend "http://poker_user:8080/";
    proxy_pass $backend;

    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';






    Hope this help!






    share|improve this answer



























      1














      I found several similar related questions and will try to compile two possible solutions out of those:



      • https://serverfault.com/questions/240476/how-to-force-nginx-to-resolve-dns-of-a-dynamic-hostname-everytime-when-doing-p

      • Docker Network Nginx Resolver

      First solution



      First solution is to reverse dependency between services: instead of poker_user depend on gateway you can may gateway depend on user. This way when gateway starts if will be able to resolve poker_user hostname.



      The Dockerfile would look this way in this case:



      version: '3.5'
      services:
      gateway:
      image: nginx:latest
      restart: always
      ports:
      - 8080:80
      depends_on: poker_user
      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
      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:
      - 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
      networks:
      - poker_network

      networks:
      poker_network:
      volumes:
      db-data:


      I also removed some other dependencies as I think they are redundant.



      Second solution



      This solution is based on changing nginx configuration. It looks like nginx tries to resolve hostname specified in proxy_pass upfront and since it's not available yet when nginx starts it will keep responding with 504 because it doesn't try to resolve it again.



      There seems to be a workaround with using variables inside proxy_pass directive which are treated differently and force nginx to perform DNS resolution again. This requires resolver to be configured in nginx and luckily there's one built into Docker when using user-defined networks.



      Here's an example nginx configuration (though I haven't tested it myself):



      events worker_connections 1024; 

      http

      server
      listen 80;
      location /user
      # Docker DNS server in user-defined networks
      # https://docs.docker.com/v17.09/engine/userguide/networking/configure-dns/
      resolver 127.0.0.11 ipv6=off;

      rewrite /user/(.*) /$1 break;

      # Using a variable will force nginx to re-resolve DNS name
      # https://serverfault.com/a/593003
      set $backend "http://poker_user:8080/";
      proxy_pass $backend;

      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';






      Hope this help!






      share|improve this answer

























        1












        1








        1







        I found several similar related questions and will try to compile two possible solutions out of those:



        • https://serverfault.com/questions/240476/how-to-force-nginx-to-resolve-dns-of-a-dynamic-hostname-everytime-when-doing-p

        • Docker Network Nginx Resolver

        First solution



        First solution is to reverse dependency between services: instead of poker_user depend on gateway you can may gateway depend on user. This way when gateway starts if will be able to resolve poker_user hostname.



        The Dockerfile would look this way in this case:



        version: '3.5'
        services:
        gateway:
        image: nginx:latest
        restart: always
        ports:
        - 8080:80
        depends_on: poker_user
        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
        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:
        - 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
        networks:
        - poker_network

        networks:
        poker_network:
        volumes:
        db-data:


        I also removed some other dependencies as I think they are redundant.



        Second solution



        This solution is based on changing nginx configuration. It looks like nginx tries to resolve hostname specified in proxy_pass upfront and since it's not available yet when nginx starts it will keep responding with 504 because it doesn't try to resolve it again.



        There seems to be a workaround with using variables inside proxy_pass directive which are treated differently and force nginx to perform DNS resolution again. This requires resolver to be configured in nginx and luckily there's one built into Docker when using user-defined networks.



        Here's an example nginx configuration (though I haven't tested it myself):



        events worker_connections 1024; 

        http

        server
        listen 80;
        location /user
        # Docker DNS server in user-defined networks
        # https://docs.docker.com/v17.09/engine/userguide/networking/configure-dns/
        resolver 127.0.0.11 ipv6=off;

        rewrite /user/(.*) /$1 break;

        # Using a variable will force nginx to re-resolve DNS name
        # https://serverfault.com/a/593003
        set $backend "http://poker_user:8080/";
        proxy_pass $backend;

        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';






        Hope this help!






        share|improve this answer













        I found several similar related questions and will try to compile two possible solutions out of those:



        • https://serverfault.com/questions/240476/how-to-force-nginx-to-resolve-dns-of-a-dynamic-hostname-everytime-when-doing-p

        • Docker Network Nginx Resolver

        First solution



        First solution is to reverse dependency between services: instead of poker_user depend on gateway you can may gateway depend on user. This way when gateway starts if will be able to resolve poker_user hostname.



        The Dockerfile would look this way in this case:



        version: '3.5'
        services:
        gateway:
        image: nginx:latest
        restart: always
        ports:
        - 8080:80
        depends_on: poker_user
        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
        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:
        - 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
        networks:
        - poker_network

        networks:
        poker_network:
        volumes:
        db-data:


        I also removed some other dependencies as I think they are redundant.



        Second solution



        This solution is based on changing nginx configuration. It looks like nginx tries to resolve hostname specified in proxy_pass upfront and since it's not available yet when nginx starts it will keep responding with 504 because it doesn't try to resolve it again.



        There seems to be a workaround with using variables inside proxy_pass directive which are treated differently and force nginx to perform DNS resolution again. This requires resolver to be configured in nginx and luckily there's one built into Docker when using user-defined networks.



        Here's an example nginx configuration (though I haven't tested it myself):



        events worker_connections 1024; 

        http

        server
        listen 80;
        location /user
        # Docker DNS server in user-defined networks
        # https://docs.docker.com/v17.09/engine/userguide/networking/configure-dns/
        resolver 127.0.0.11 ipv6=off;

        rewrite /user/(.*) /$1 break;

        # Using a variable will force nginx to re-resolve DNS name
        # https://serverfault.com/a/593003
        set $backend "http://poker_user:8080/";
        proxy_pass $backend;

        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';






        Hope this help!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 8:52









        Igor NikolaevIgor Nikolaev

        2,75711318




        2,75711318





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315958%2f504-response-from-ngingx-reverse-proxy-with-nodejs-docker%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript