504来自ngingx反向代理与Node.js + Docker的响应

时间:2019-03-23 16:36:22

标签: javascript node.js docker nginx docker-compose

更新

我发现,如果我使用docker exec -it backend_gateway_1 nginx -s reload在容器上重新启动Nginx,它将起作用。我从this question找到了一些信息。似乎可以连接到127.0.0.1而不是localhost,但是我使用的是docker网络,所以我不确定我的处境。

原始问题

我正在尝试将nginx用作docker的反向代理。我无法完全弄清出什么问题了,有时它有时在什么时候起作用,但是如果我使用docker-compose down && docker-compose up重新启动所有功能,那么通常会收到504错误

我觉得这与nginx或docker配置有关。我可以向/提出请求,但对/ user的任何请求都导致间歇性问题。

当我没有收到504消息时,节点应用程序运行良好,并创建/返回了用户。我还可以使用mongo-express很好地连接到数据库,这样应该可以缩小范围。

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

    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我发现了几个类似的相关问题,并将尝试从中找出两个可能的解决方案:

第一个解决方案

第一个解决方案是反转服务之间的依赖关系:您可以poker_user依赖用户,而不是gateway依赖gateway。这样gateway启动时,便可以解析poker_user主机名。

在这种情况下,Dockerfile的外观如下:

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:

我还删除了一些其他依赖项,因为我认为它们是多余的。

第二个解决方案

此解决方案基于更改nginx的配置。似乎nginx试图解析proxy_pass中预先指定的主机名,并且由于nginx启动时尚不可用,它将继续以504进行响应,因为它不会尝试再次解决。

proxy_pass指令中使用变量似乎有一种变通方法,这些变量被区别对待并强制nginx再次执行DNS解析。这要求使用用户定义的网络时,必须在Docker内置的nginxluckily there's one中配置解析器。

这是一个示例nginx配置(尽管我自己尚未测试过):

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

    }
}

希望获得帮助!