Traefik 2在django生产docker-compose中作为Nginx的reverse_proxy

m4pnthwp  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(118)

我已经成功地构建了一个小的Django应用程序一些基本的东西。Localy工作正常,我想在已经运行的生产服务器上启动此WebApp。
在这个服务器上已经运行了一些docker-compose配置,其中traefik 2用作具有letsencrypt证书的revers_proxy。到目前为止一切顺利。
当我设计我的django项目时,我想使用nginx作为静态文件交付,并让traefik 2在nginx前面作为revers_proxy工作。
docer-compose.pro.yml

version: "3.9"

# docker-compose for production
# It is important that the .env.prod is used.
# For this, docker-compose must always be executed
# with the command docker-compose -f docker-compose.pro.yml --env-file .env.prod.

services:
  web:
    build:
      context: .
      dockerfile: ./compose/Dockerfile
    restart: always
    command: gunicorn project.wsgi:application --bind 0.0.0.0:8000
    env_file:
      - ./.env.prod
    expose:
      - 8000
    volumes:
      - static_volume:/code/staticfiles
      - media_volume:/code/mediafiles
    depends_on:
      - redis
      - db
    networks:
      - pjnet

  db:
    image: postgres:15
    restart: always
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=${DB_USERNAME}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
    networks:
      - pjnet

  redis:
    image: redis:7
    networks:
      - pjnet

  celery:
    build:
      context: .
      dockerfile: ./compose/Dockerfile
    restart: always
    command: celery -A project worker -l info
    volumes:
      - .:/code
    env_file:
      - ./.env.prod
    depends_on:
      - db
      - redis
      - web
    networks:
      - pjnet

  nginx:
    build: ./compose/nginx
    restart: always
    ports:
      - ${NGINX_PORT}:80
    volumes:
      - static_volume:/code/staticfiles
      - media_volume:/code/mediafiles
    labels:
      - traefik.enable=true
      - traefik.http.routers.blogng.rule=Host(`blog.project.com`)
      - traefik.http.routers.blogng.entrypoints=http
      - traefik.http.routers.blogng.middlewares=https-redirect

      - traefik.http.routers.blogngsec.rule=Host(`blog.project.com`)
      - traefik.http.routers.blogngsec.entrypoints=https
      - traefik.http.routers.blogngsec.tls.certresolver=le
      - traefik.http.routers.blogngsec.service=blogngsec
      - traefik.http.services.blogngsec.loadbalancer.server.port=${NGINX_PORT}
    depends_on:
      - web
    networks:
      - pjnet
      - traefik-net

volumes:
  postgres_data:
  static_volume:
  media_volume:

networks:
  traefik-net:
    external: true
  pjnet:

字符串
和nginx.conf

upstream web_app {
    server web:8000;
}

server {

    listen 80;

    location /static/ {
        alias /code/staticfiles/;
    }

    location /media/ {
        alias /code/mediafiles/;
    }

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

}

问题

有了这个配置,我得到一个“坏网关”错误从我的浏览器。有人知道我做错了什么吗?

其他

如果我在web服务中使用“labels”部分,django可以工作,但我不能获得静态文件。这向我证实了Traefik和Django正在工作。当我使用上面的配置表单时,从docker-compose看起来没有对nginx的请求通过。

q8l4jmvw

q8l4jmvw1#

嗯,问这样的问题实在是太尴尬了。
解决方案是正确设置Nginx侦听的端口,并将其从docker-compose中删除。
因此,需要更正以下行。
docker-compose.pro.yml

nginx:
    build: ./compose/nginx
    restart: always
    #ports:               <--- can also be deleted
      #- ${NGINX_PORT}:80  <--- can also be deleted
    volumes:
      - static_volume:/code/staticfiles
      - media_volume:/code/mediafiles
    labels:

字符串
nginx.conf

server {

    listen 8080;   <-- in my example port I use

    location /static/ {

相关问题