You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何提升FastAPI应用中Nginx/Uvicorn的文件上传速度?

问题:FastAPI文件上传速度异常缓慢(经Nginx反向代理)

我在AWS EC2 g4n.xlarge实例(弗吉尼亚地区)上部署了FastAPI应用,服务器系统为Ubuntu,通过Nginx反向代理Uvicorn运行,前端采用HTMX。家中是千兆光纤以太网,上传速度接近1GBPs,但上传一个48MB、时长35分钟的MP3文件耗时约10分钟,完全无法接受。尝试过分片上传但无改善,分片代码如下:

async with aiofiles.open(video_file_path, "wb") as out_file:
    while True:
        content = await file.read(1024 * 1024)  # 每次读取1MB分片
        if not content:
            break
        await out_file.write(content)

本地直接用Uvicorn测试时上传瞬间完成,且我的上传速度与EC2实例带宽均无问题,因此怀疑是Nginx配置导致的速度瓶颈,当前Nginx配置如下:

server {
    server_name example.com;

    # 增大客户端最大请求体大小
    client_max_body_size 6000M;  # 允许6GB上传

    # 调整超时时间
    client_body_timeout 7200s;
    client_header_timeout 7200s;

    location / {
        proxy_pass http://127.0.0.1:8001; # 代理到FastAPI端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # WebSocket支持所需配置
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # 代理超时设置
        proxy_read_timeout 7200s;
        proxy_connect_timeout 7200s;
        proxy_send_timeout 7200s;
    }
}

内容的提问来源于stack exchange,提问作者Lynob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:37:16