部署时,我收到此错误[error] 9#9:*1个连接()失败(111:连接到上游Nginx 502时连接被拒绝)网关错误

tp5buhyn  于 2023-01-04  发布在  Nginx
关注(0)|答案(1)|浏览(180)

我有一个包含Django-Docker-Postgresql-Nginx的项目
我按照这个教程添加SSL到我的项目使用LetsEncrypt Link在按照这个教程之前,我的项目是活的digitalocean成功。之后,我得到了nginx 502网关错误在我的浏览器上,当我检查证书,证书得到验证,我的连接是安全的。当我检查代理容器的日志在终端上,我看到了[错误] 9#9:* 1个连接()失败(111:连接被拒绝)时连接到上游错误:用户名://我的IP地址:9000。
我的docker-compose-prod-yml文件:

version: "3.9"

services:
  app:
    build:
      context: .
      dockerfile: ./Dockerfile
    restart: always
    command: /start
    environment:
      - DJANGO_SECRET_KEY=${DJANGO_SECRET_KEY}
      - DJANGO_ALLOWED_HOSTS=${DOMAIN}
    depends_on:
      - postgres

  proxy:
    build:
      context: ./docker/proxy
    restart: always
    depends_on:
      - app
    ports:
      - 80:80
      - 443:443
    volumes:
      - certbot-web:/vol/www
      - proxy-dhparams:/vol/proxy
      - certbot-certs:/etc/letsencrypt
    environment:
      - DOMAIN=${DOMAIN}

  certbot:
    build:
      context: ./docker/certbot
    command: echo "Skipping..."
    environment:
      - EMAIL=${ACME_DEFAULT_EMAIL}
      - DOMAIN=${DOMAIN}
    env_file:
      - ./.env
    volumes:
      - certbot-web:/vol/www
      - certbot-certs:/etc/letsencrypt/
    depends_on:
      - proxy

  postgres:
    image: "postgres:latest"
    container_name: postgres_data
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=te7eyp9cc
      - POSTGRES_DB=dj-crm-tenant
    ports:
      - "54320:5432"

volumes:
  certbot-web:
  proxy-dhparams:
  certbot-certs:
  postgres_data:

我的默认ssl配置文件:

server {
    listen 80;
    server_name ${DOMAIN} www.${DOMAIN};

    location /.well-known/acme-challenge/ {
        root /vol/www/;
    }

    location / {
        return 301 https://$host$request_uri;
    }
   
}

server {
    listen      443 ssl;
    server_name ${DOMAIN} www.${DOMAIN};

    ssl_certificate     /etc/letsencrypt/live/${DOMAIN}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${DOMAIN}/privkey.pem;

    include     /etc/nginx/options-ssl-nginx.conf;

    ssl_dhparam /vol/proxy/ssl-dhparams.pem;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    location /static {
        alias /vol/static;
    }

    location / {
        uwsgi_pass           ${APP_HOST}:${APP_PORT};
        include              /etc/nginx/uwsgi_params;
        client_max_body_size 10M;
    }
}

我想我得到这个错误是因为nginx配置文件,我尝试了很多组合,但我不能改变这个错误。请帮助我!
我尝试添加到我的nginx配置文件

listen 443 ssl http2;
    listen [::]:443 ssl http2;

       proxy_pass http://localhost:8000/;
       proxy_pass http://localhost:9000/;
jtoj6r0c

jtoj6r0c1#

在location中尝试proxy_pass而不是uwsgi,并使用下面提到的代理配置。
defult-ssl-conf.tpl

location / {

        proxy_redirect      off;
        proxy_set_header    Host                $host;
        proxy_set_header    REMOTE_ADDR         $remote_addr;
        proxy_set_header    X-Url-Scheme        $scheme;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   https;
        proxy_set_header    User-Agent          $http_user_agent;
        proxy_pass   http://app:8000/;
    }

相关问题