Nginx错误:502错误网关nginx/1.23.2在Docker + Django + Postgres上

snz8szmq  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(213)

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

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/*;
}

第一次在这里发帖,长时间搜索者/潜伏者:)我感谢任何帮助,谢谢。

b0zn9rqh

b0zn9rqh1#

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

相关问题