如何禁用上游缓冲 Nginx + Docker + Gunicorn?

如何禁用上游缓冲 Nginx + Docker + Gunicorn?

我正在尝试使用 Nginx 作为客户端和 Gunicorn 之间的代理服务器。Nginx、Gunicorn(Django)是 Docker 的容器。问题是当我从客户端向 Django 应用程序发送大文件时,我无法禁用上游缓冲。TTFB 时间非常短,因此我的进度条(使用 xhr.upload.progress 事件)非常快地(不到一秒)变为 100%。然后我必须等待 30 秒,直到文件上传到服务器。这是我的设置。请帮帮我。我尝试了许多配置,试图将缓冲区大小设置为零等,在 StackOverflow 上查看了许多答案,但都无济于事。

docker-compose.yaml

...
    services:
  db:
    image: postgres:12.4
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    restart: always
    ports:
      - ${DB_PORT}:${DB_PORT}
    env_file:
      - ./.env

  backend:
    build: ./src/backend
    volumes:
      - RUDZASV0021:/code/storage/RUDZASV0021
      - logs:/code/logs
    restart: always
    depends_on:
      - db
    env_file:
      - ./.env

  nginx:
    build:
      context: .
      dockerfile: ./src/frontend/Dockerfile
    volumes:
      - ./docker-settings/default.conf:/etc/nginx/conf.d/default.conf:ro
    restart: always
    ports:
      - 80:80
      - 443:443
    depends_on:
      - backend

后端Dockerfile

FROM python:3.8.7-slim

WORKDIR /code
COPY . .
RUN pip install -r /code/requirements.txt
RUN apt-get update && apt-get install -y mc
CMD gunicorn entrypoints.wsgi:application --workers=4 --worker-class=gevent  --timeout=90 --graceful-timeout=10 --bind 0.0.0.0:8000

Nginx Dockerfile

FROM nginx:1.20.0

WORKDIR /frontend
COPY ./src/frontend/dist .
WORKDIR /cert
COPY ./cert/device.key .
COPY ./cert/device.crt .

Nginx 默认配置文件

upstream hello_django {
    server backend:8000 fail_timeout=0;
}

server {
    listen 80;
    return 301 https://$host$request_uri;
}

server {

    listen 443;

    ssl on;
    ssl_certificate /cert/device.crt;
    ssl_certificate_key /cert/device.key;

    client_max_body_size 2G;
    keepalive_timeout 5;

    access_log /frontend/nginx-access.log;
    error_log /frontend/nginx-error.log;

    location / {
        root /frontend;
        try_files $uri /index.html;
    }

    location /api/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_buffering off;
        proxy_request_buffering off;
        proxy_redirect off;
        proxy_pass http://hello_django;
    }

}

相关内容