我有一个 Ubuntu 服务器,使用 Uvicorn 为 FastAPI 应用程序运行 Nginx 反向代理。服务器是位于弗吉尼亚州的 AWS EC2 g4n.xlarge。在前端我使用 HTMX 我个人家里的上传速度接近1GBPs,光纤,我通过以太网线连接。
应用程序先上传文件,然后处理它。要上传48mb、35分钟的mp3文件,应用程序需要10分钟左右,这是不可接受的。事实上,考虑到我的网速很快,任何超过 30 秒或一分钟的时间都是不可接受的。我已经尝试过块上传,没有什么区别
async with aiofiles.open(video_file_path, "wb") as out_file:
while True:
content = await file.read(1024 * 1024) # Read chunks of 1 MB
if not content:
break
await out_file.write(content)
我相信这个问题与NGINX有关,因为在我的PC上进行测试,我直接使用Uvicorn而没有NGINX并且上传是即时的。我的上传速度很快,EC2上传速度也很快,所以唯一能怪的就是Nginx了,我想
nginx 配置
server {
server_name example.com;
# Increase client max body size
client_max_body_size 6000M; # Allow 6GB uploads
# Adjust timeouts
client_body_timeout 7200s;
client_header_timeout 7200s;
location / {
proxy_pass http://127.0.0.1:8001; # Proxy pass to the FastAPI port
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Necessary for WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Proxy timeouts
proxy_read_timeout 7200s;
proxy_connect_timeout 7200s;
proxy_send_timeout 7200s;
}
我通过将这两行添加到 nginx
sites-enabled
网站 .conf
文件 解决了这个问题
Header set Accept-Ranges bytes; # inside the server block
proxy_force_ranges on; # inside the location block.
我并没有尝试解决上传问题,我只是尝试解决仅在 Chrome 上的 videojs currentTime 问题。
HTTP Range 请求要求服务器仅将 HTTP 消息的一部分发送回客户端。范围请求对于支持随机访问的媒体播放器、知道它们只需要大文件的一部分的数据工具以及允许用户暂停和恢复下载的下载管理器等客户端非常有用。
proxy_force_ranges 的文档位于这里
为来自代理服务器的缓存和未缓存响应启用字节范围支持,无论这些响应中的“Accept-Ranges”字段如何。