Nginx + Docker Compose-连接到上游时connect()失败(111:连接被拒绝)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx + Docker Compose-连接到上游时connect()失败(111:连接被拒绝)相关的知识,希望对你有一定的参考价值。

感谢您抽出宝贵的时间阅读本文档。我正在尝试使用docker-compose将应用程序部署到AWS EC2实例。当我运行命令docker-compose up并访问该站点时,我从nginx收到一条错误,提示以下错误。我了解nginx收到了请求,但无法找到与我的react应用程序的上游连接,希望对正确配置端口/设置提供任何帮助。

错误

2 connect() failed (111: Connection refused) while connecting to upstream, client: 108.212.77.70 server: example.com, request: "GET / HTTP/1.1", upstream: "http://172.29.0.4:8003/", host: "example.com"

这是我的Nginx默认配置

upstream meetup_ws 
    server channels:8001;


upstream meetup_backend 
    server backend:8000;


upstream meetup_frontend 
    server frontend:8003;


server 
    listen 0.0.0.0:80;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;


server 
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com example.com;
    root /var/www/frontend;
    index index.html;
    ssl_certificate /etc/nginx/certs/fullchain.pem;
    ssl_certificate_key /etc/nginx/certs/privkey.pem;
    add_header Strict-Transport-Security "max-age=31536000";

    location / 
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://meetup_frontend;
    

    location /api 
        try_files $uri @proxy_api;
    

    location @proxy_api 
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://meetup_backend;
    

    location /ws 
        try_files $uri @proxy_websocket;
    

    location @proxy_websocket 
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://meetup_ws;
    

这是我的docker-compose.yml

version: '3'

services: 
    nginx:
        build: ./nginx
        restart: always
        ports:
            - 80:80
            - 443:443
        volumes:
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
            - ./frontend/build:/var/www/frontend
            - ./nginx/certs:/etc/nginx/certs
        depends_on: 
            - channels
    db:
        image: postgres:12.0-alpine
        ports: 
            - 5432:5432
        environment: 
            - POSTGRES_USER=postgres
            - POSTGRES_HOST=db
            - POSTGRES_PASSWORD=password
        volumes:
            - postgres_data:/var/lib/postgresql/data/
    backend: &backend
        build: ./backend
        volumes: 
            - ./backend:/app
        ports: 
            - 8000:8000
        command: ["python", "manage.py", "runserver"]
        env_file:
            - ./.env
        depends_on: 
            - db
            - redis
    frontend:
        build: ./frontend
        volumes: 
            - ./frontend:/app
            - node_modules:/app/node_modules
        ports: 
            - 8003:8003
        command: npm start
        stdin_open: true
    redis:
        image: "redis:5.0.7"
    worker_channels:
        <<: *backend
        command: ["python", "manage.py", "runworker", "channels"]
        depends_on:
            - db
            - redis
        ports:
            - 8002:8002
    channels:
        <<: *backend
        command: daphne -b 0.0.0.0 -p 8001 backend.asgi:application
        ports: 
            - 8001:8001
        depends_on:
            - db
            - redis

volumes:
    node_modules:
    postgres_data:
答案

为什么存在这个问题有点令人尴尬,但是我能够解决这个问题。我做了我的Nginx容器中的ping frontend,它已成功ping通前端容器。接下来,我做了curl -L http://frontend:8003,并说了curl: (7) Failed to connect to frontend port 8003: Connection refused。我去了前端容器并执行了netstat -tulpn,它列出了3000作为暴露的端口。我检查了我的.env文件,它缺少port = 8003。之后,Nginx能够连接上游。

以上是关于Nginx + Docker Compose-连接到上游时connect()失败(111:连接被拒绝)的主要内容,如果未能解决你的问题,请参考以下文章

Nginx 错误失败(111:连接被拒绝)同时连接到上游,docker-compose nodejs

docker compose MySQL 容器 [2002] 连接被拒绝

docker-compose部署emqx集群(三节点,nginx做TCP的负载均衡)

docker-compose快速启动nginx

docker run 和 docker-compose 配置挂载 Volume 并运行 nginx

docker-compose 简单搭建nginx的ssl环境