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

FastAPI上传.7z文件遇502错误及Docker自动刷新问题求助

文件上传异常问题分析

问题现象

  • 上传120MB的.7z文件时,Swagger UI持续返回502 Bad Gateway未记录错误
  • 上传16-17MB的小文件时,Docker容器会在处理过程中自动刷新,导致需要重复上传

部署环境:Coolify托管的Docker Compose环境

错误信息

Code    Details
502
Undocumented
Error: response status is 502

Response body
Unrecognized response type; displaying content as text.

Download
Bad Gateway
Response headers
 content-length: 11 
 date: Fri,18 Oct 2024 17:27:56 GMT 
 vary: Accept-Encoding 

相关代码

FastAPI上传接口

@router.post("/7zip/")
async def upload_7zip(background_tasks: BackgroundTasks, archive_file: UploadFile = File(...)):
    if not archive_file.filename.endswith(".7z"):
        raise HTTPException(status_code=400, detail="Please upload a valid 7z file.")

    try:
        # Generate a unique filename to avoid overwrites
        archive_filename = f"{uuid.uuid4()}_{archive_file.filename}"
        archive_path = os.path.join(UPLOAD_DIR, archive_filename)

        # Save the uploaded file
        with open(archive_path, "wb") as buffer:
            shutil.copyfileobj(archive_file.file, buffer)

        # Add the extraction task to the background
        background_tasks.add_task(extract_file, archive_path)

        return JSONResponse({"message": "File uploaded successfully, extraction is in progress."})
    except Exception as e:
        logger.error(f"An error occurred while processing the 7z file: {str(e)}")
        raise HTTPException(status_code=500, detail="An error occurred while processing the 7z file.")

Docker Compose配置

app:
    container_name: search
    build: .
    command: bash -c "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port 8000 --timeout-keep-alive=240"
    ports:
      - 8001:8000
    env_file:
      - .env
    volumes:
      - uploads_data:/app/uploads
    depends_on:
      - db 
    restart: always

可能成因分析

针对502 Bad Gateway(大文件上传)

  1. Coolify反向代理超时限制:Coolify自带的反向代理(如Nginx)可能设置了较短的client_body_timeout或proxy_read_timeout,大文件上传耗时超过阈值,代理直接返回502。
  2. 请求体大小限制:UVicorn或FastAPI中间件可能设置了max_request_size,大文件超出限制后请求被中断,代理返回502。
  3. 容器资源耗尽:大文件上传时容器内存/CPU占用过高,进程被OOM Killer终止,代理无法连接后端服务导致502。
  4. 环境层面文件大小限制:Coolify托管环境的防火墙、负载均衡器可能设置了文件上传大小上限,直接拒绝大文件请求。

针对小文件上传时Docker自动刷新

  1. 容器内存不足:小文件解压过程占用内存超过容器分配的阈值,触发OOM重启,表现为"自动刷新"。
  2. Uvicorn进程崩溃:解压任务中出现未捕获的异常,导致Uvicorn进程终止,容器因restart: always配置自动重启。
  3. 健康检查触发重启:Coolify对容器设置的健康检查规则过于严格,文件处理期间服务响应变慢,无法通过健康检查,被强制重启。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 05:33:11