正在进入ing...

Docker-compo 部署 Fastapi + Nginx 支持流式返回(stream)

发布时间:2025-05-16 浏览量: 1109 文章分类: python

Docker-compo 部署 Fastapi + Nginx 支持流式返回(stream)

正常我们的部署大致是 docker-compose 管理2个容器, 一个 nginx 、一个 python的,里面可以跑各种web框架。

实际这个本身到和语言没有太多关系,以 fastapi 为例, 我们只需要在 fastapi 中返回 StreamingResponse 即可。

    return StreamingResponse(
        generate(),
        media_type='application/json',
        headers={
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
            'Access-Control-Allow-Headers': 'Content-Type, Authorization',
        }
    )

更多配置是在nginx中,我贴了一个完整的例子,可以查看例子。

user    root;
worker_processes auto;

events {
    worker_connections  4089;
}

http {
    upstream fastapi_backend{
        server web_1:8000;
        server web_2:8000;
        server web_3:8000;
        server web_4:8000;
        server web_5:8000;
        server web_6:8000;
        server web_7:8000;
        server web_8:8000;
        server web_9:8000;
        server web_10:8000;
    }

    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    client_max_body_size   20m;
    server {
        listen  8003 ssl;
        ssl_certificate /data/conf/nginx/xxxx.crt;
        ssl_certificate_key /data/conf/nginx/xxxx.key;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        server_name  xxxx.xxxx.com;
        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://fastapi_backend;

            # 禁用响应缓存和代理缓冲,启用分块传输编码
            proxy_read_timeout 86400s; # 设置较长的超时时间
            proxy_buffering off; # 禁用缓冲
            proxy_cache off; # 关闭代理缓存
            proxy_http_version 1.1;
            proxy_set_header Connection "keep-alive";
        }
    }   
}