繁体   English   中英

Nginx 错误:502 Bad Gateway nginx/1.23.2 on Docker + Django + Postgres

[英]Nginx error: 502 Bad Gateway nginx/1.23.2 on Docker + Django + Postgres

所以这是我从日志中得到的错误

2022/11/15 04:30:08 [error] 29#29: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.80.1, server: mysite.local, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://192.168.80.3:3000", host: "mysite.local", referrer: "http://mysite.local/"

我不知道是什么问题。 我设法让 Docker 启动并运行,nginx、postgres 和 django 运行。

这是我的mysite.local文件。 我正在练习/学习。

server {
        listen 80;

        server_name mysite.local;

        root /app/mysite_proj;
        index index.php;

        # https://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/

        # Deny access to hidden files
        location ~ /\. {
            access_log off;
            log_not_found off;
            deny all;
        }

        # max upload size
        client_max_body_size 75M;   # adjust to taste

        # Django media
        location /media  {
        alias /app/mysite/media;  # your Django project's media files - amend as required
        }

        location /static {
        alias /app/mysite/static; # your Django project's static files - amend as required
        }

         # Finally, send all non-media requests to the Django server.
    location / {
        include /etc/nginx/uwsgi_params;
        uwsgi_pass django:3000;
    }

}

我的docker-compose.yml

version: '3.9'
services:

  django_api_backend:
    container_name: django
    platform: linux/amd64
    build: docker/python
    restart: always
    expose:
      - "127.0.0.1:3000:3000"
    volumes:
      - .:/app
    depends_on:
      - local_db
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_HOST: ${DB_HOST}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      SETTINGS_MODULE: ${SETTINGS_MODULE}

  nginx:
    container_name: nginx
    image: nginx:latest
    hostname: nginx
    ports:
      - "127.0.0.1:80:80"
    volumes:
      - ${NGINX_FILES_PATH}/nginx.conf:/etc/nginx/nginx.conf:ro                     # config file
      - ${NGINX_FILES_PATH}/vhosts:/etc/nginx/vhosts                                # virtual hosts
      - ./logs/nginx:/var/log/nginx:rw
    depends_on:
      - django_api_backend

  local_db:
    container_name: postgres
    image: postgres:15
    ports:
      - "5432:5432"
    volumes:
      - /var/lib/postgresql/data/
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}

我的 nginx.conf

worker_processes  4;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    client_max_body_size 25M;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    # https://github.com/vfarcic/continuous-deployment/issues/2
    include /etc/nginx/vhosts/*;
}

第一次在这里发帖,长期搜索者/潜伏者:) 感谢您的帮助,谢谢。

要从另一个 docker 或服务访问服务,您应该使用 docker 服务名称而不是容器名称。 这里你的 django 服务名称是 django_api_backend。 替换它 nginx 配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM