使用 Traefik 时如何使用 Nginx 和 Django Gunicorn 提供静态内容

How to serve static content with Nginx and Django Gunicorn when using Traefik

我有一个使用多个容器的 Web 应用程序(基于 Django):

  1. Web 应用程序(Django + Gunicorn)
  2. Traefik(作为反向代理和 SSL 终止)
  3. 与 Web 应用程序一起使用的数据库
  4. 与Web应用程序一起使用的Redis

根据我阅读的一些文档,我应该使用 NGINX 之类的东西来提供我的静态内容。但我不知道我会怎么做。我是将 NGINX 安装在我的 Web 应用程序容器上还是作为单独的 NGINX 容器。如何传递 Traefik 的请求?据我所知,您不能使用 Traefik 服务器静态内容。

这是我的 docker-compose.yml 的样子:

 traefik:
    image: traefik
    ports:
      - 80:80
      - 8080:8080
      - 443:443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik/traefik.toml:/etc/traefik/traefik.toml:ro
      - ./traefik/acme:/etc/traefik/acme

  web:
    build: .
    restart: always
    depends_on:
        - db
        - redis
        - traefik
    command: python3 /var/www/html/applications/py-saleor/manage.py makemigrations --noinput
    command: python3 /var/www/html/applications/py-saleor/manage.py migrate --noinput
    command: python3 /var/www/html/applications/py-saleor/manage.py collectstatic --noinput
    command: bash -c "cd /var/www/html/applications/py-saleor/ && gunicorn saleor.wsgi -w 2 -b 0.0.0.0:8000"
    volumes:
      - .:/app
    ports:
      - 127.0.0.1:8000:8000
    labels:
      - "traefik.enable=true"
      - "traefik.backend=web"
      - "traefik.frontend.rule=${TRAEFIK_FRONTEND_RULE}"
    environment:
      - SECRET_KEY=changemeinprod

  redis:
    image: redis

  db:
    image: postgres:latest
    restart: always
    environment:
      POSTGRES_USER: saleoradmin
      POSTGRES_PASSWORD: **
      POSTGRES_DB: **
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - ~/py-saleor/database:/app

我对 Traefik 和 Docker 了解不多。

但我可以告诉您如何安装 nginx 并使用它来提供静态文件(始终推荐这样做,以免因提供静态文件而阻塞 django 服务器)

安装 nginx 并按照提到的步骤进行设置 nginx

sudo apt-get install nginx

站点可用文件应如下所示:

server {
        listen 80;
        listen [::]:80;

        server_name xyz.com;
        client_max_body_size 20M;

        # xyz.com/media/any_static_asset_file.jpg 
        # when anyone hits the above url then the request comes to this part.
        location /media/ {

                # do make sure that the autoindex is off so that your assets are only accessed when you have proper path

                autoindex off; 

                # this is the folder where your asset files are present.

                alias /var/www/services/media/; 
        }

        # whenever any request comes to xyz.com then this part would handle the request
        location / {
                proxy_pass http://unix:/var/www/services/xyz/django_server.sock;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

如果其他人需要对此的答案,答案在于创建一个单独的 NGINX 服务,然后将前端规则定向到静态位置 (xyz.com/static),例如见下文(docker-compose.yml 的一部分):

  nginx:
       image: nginx:alpine
       container_name: nginx_static_files
       restart: always
       volumes:
           - ./default.conf:/etc/nginx/conf.d/default.conf
           - ./saleor/static/:/static
       labels:
           - "traefik.enable=true"
           - "traefik.backend=nginx"
           - "traefik.frontend.rule=Host:xyz.co;PathPrefix:/static"
           - "traefik.port=80"

您还需要确保您的 Nginx 配置文件 (default.conf) 已正确配置:

server {
   listen                      80;
   server_name                 _;
   client_max_body_size        200M;
   set                         $cache_uri $request_uri;

   location                    = /favicon.ico { log_not_found off; access_log off; }
   location                    = /robots.txt  { log_not_found off; access_log off; }
   ignore_invalid_headers      on;
   add_header                  Access-Control-Allow_Origin *;

   location /static {
       autoindex on;
       alias /static;
   }

   location /media {
       autoindex on;
       alias /media;
   }

   access_log                  /var/log/nginx/access.log;
   error_log                   /var/log/nginx/error.log;
}

所有功劳都归功于 Traefik 松弛频道上的 Pedro Rigotti,他帮助我找到了解决方案。