使用 Nginx 连接失败(111:连接被拒绝)反向代理

问题描述 投票:0回答:1

我尝试使用 docker、nginx 和 django 将端口恢复到 8000,但出现连接被拒绝错误。 docker 构建和组合工作正常。 这是我的 nginx.conf 文件:

    worker_processes auto;

    events {
    worker_connections 4;
    }

    http {
    upstream backend {
        # web is the docker-compose.yml backend service
        server web:8000;
    }

    server {
        listen 80;
        server_name 127.0.0.1;

        location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        }
    }
    }

和我的 docker-compose.yml 文件:

    services:
      db:
        image: postgres
        volumes:
          - ./data/db:/var/lib/postgresql/data
        environment:
          - POSTGRES_DB=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
      web:
        build: .
        command: python manage.py runserver 127.0.0.1:8000
        volumes:
          - .:/code
        ports:
          - "8000:8000"
        environment:
          - POSTGRES_NAME=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
        depends_on:
          - db
      nginx:
        image: nginx
        ports:
          - 80:80
        volumes:
          - ./nginx/config/nginx.conf:/etc/nginx/nginx.conf
        depends_on:
          - web


我遇到的错误:

`2023/10/28 08:22:33 [error] 22#22: *1 connect() failed (111: Connection refused) while connecting to upstream, client: my_pc_ip, server: 127.0.0.1, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "127.0.0.1"`

我尝试更改 nginx.conf 和 .yml 文件,但没有任何改变

django docker nginx reverse-proxy
1个回答
0
投票

我终于找到了! 问题是 django 没有正常运行。 我现在正在 Daphne 上启动服务器,感谢 ABHAY KOTAL 对于他的评论,最终的 nginx.conf 是:

worker_processes auto;

events {
worker_connections 1024;
}

http {
    upstream backend {
        # web is the docker-compose.yml backend service
        server web:8000;
    }

    server {
        listen 8001;
        # server_name 127.0.0.1;

        location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        }
    }

}

就是这样!

© www.soinside.com 2019 - 2024. All rights reserved.