Nginx无法将代理传递给Django Docker容器

时间:2020-08-21 13:19:15

标签: django docker nginx

我在docker-compose中运行我的Django应用,并且暴露了端口8000。 我在没有docker的VM中安装了Nginx,我希望它将命令转发到我的Django应用程序,我试图在docker-compose中使用主机网络并创建一个外部网络,但无济于事,而且我总是遇到502 Bad gateway < / p>

这是我的nginx.conf:

events {
  worker_connections  4096;  ## Default: 1024
}
http{
     include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf


server {
    listen   80;

    location /static/ {
        autoindex on;
        alias   /var/lib/docker/volumes/app1_django-static;
    }

    location /media/ {
        autoindex on;
        alias   /var/lib/docker/volumes/app1_django-media;
    }

    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;

    }
    #For favicon
    location  /favicon.ico {
        alias /code/assets/favicon.ico;
    }

    # Error pages
    error_page 404 /404.html;
    location = /404.html {
        root /code/templates/;
    }
}

}

这是我的docker-compose文件:

version: "3.2"
services:
  web:
    network_mode: host
    image: nggyapp/newsapp:1.0.2
    restart: always
    ports:
      - "8000:8000"
    volumes:
      - type: volume
        source: django-static
        target: /code/static
      - type: volume
        source: django-media
        target: /code/media
    environment:
      - "DEBUG_MODE=False"
      - "DB_HOST=.."
      - "DB_PORT=5432"
      - "DB_NAME=db"
      - "DB_USERNAME=.."
      - "DB_PASSWORD=.."
volumes:
  django-static:
  django-media:

0 个答案:

没有答案