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

FastAPI+NGINX+Docker部署遇连接拒绝问题,求配置排查方案

问题解决方案

问题出在NGINX配置的proxy_pass地址上,按以下步骤修改即可解决:

修改NGINX配置

将NGINX配置文件中的proxy_pass http://0.0.0.0:8000;替换为:

proxy_pass http://app:8000;

原因说明

  • Docker Compose会为所有服务创建默认内部网络,服务之间通过服务名即可互相访问,而非0.0.0.0或宿主机IP。
  • 你在NGINX容器内写http://0.0.0.0:8000,实际是尝试访问NGINX容器自身的8000端口,但该端口未监听任何服务,因此触发连接拒绝错误。
  • 你的FastAPI服务对应的服务名是app(在docker-compose.yml中定义的service名称),所以NGINX需要通过http://app:8000才能正确转发请求到FastAPI容器。

额外优化提示

虽然depends_on能保证app服务先启动,但如果FastAPI应用启动较慢,NGINX可能在应用就绪前尝试连接。可添加健康检查配置,确保NGINX只在app服务就绪后才转发请求:

修改docker-compose.yml

version: "3.7"
services:
    web:
        build: nginx
        ports:
          - "80:80"
        depends_on:
          - app
        container_name: tensor_nginx

    app:
        build: app
        ports:
          - "8000:8000"
        container_name: test
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:8000/"]
          interval: 5s
          timeout: 5s
          retries: 3

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 21:42:51