FastAPI + NGINX + Docker 连接被拒绝

huangapple go评论59阅读模式
英文:

FastAPI + NGINX + Docker connection refused

问题

我试图创建一个简单的Docker Compose,其中包括启动两个容器,一个是下面所示的应用容器:

from fastapi import APIRouter

router = APIRouter()

@router.get("/")
async def root():
    return {"message": "This is a Test"}
from fastapi import FastAPI
import uvicorn as uvicorn

from routers import test

app = FastAPI()
app.include_router(test.router)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

另一个容器是NGINX,其配置如下:

worker_processes 1;

events {
  worker_connections 1024;
}

http {
    server {
        listen       80;

        location / {
            proxy_pass http://0.0.0.0:8000;
        }
    }
}

我的Docker Compose 如下:

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

当我启动一切都正常,可以通过访问0.0.0.0:8000/docs连接到FastAPI创建的Swagger,并且端点返回了代码中定义的字符串。

但如果我尝试使用NGINX端口连接,它会显示连接失败的消息:

[error] 30#30: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:8000/", host: "127.0.0.1"

我在配置中漏掉了什么?

英文:

I'm trying to create a simple docker-compose that would launch 2 containers one for the app as seen below:

from fastapi import APIRouter

router = APIRouter()


@router.get("/")
async def root():
    return {"message": "This is a Test"}
from fastapi import FastAPI
import uvicorn as uvicorn

from routers import test


app = FastAPI()
app.include_router(test.router)


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

The other for NGINX with the configuration:

worker_processes 1;

events {
  worker_connections 1024;
}

http {
    server {
        listen       80;

        location / {
            proxy_pass http://0.0.0.0:8000;
        }
    }
}

My docker-compose is as followed:

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

When i fire up everything starts fine and I can connect to the Swagger created by FastAPI by going 0.0.0.0:8000/docs and the endpoint returns the string defined in the code.

But if I try to connect using nginx port it says connection failed.

[error] 30#30: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:8000/", host: "127.0.0.1"

What am I missing in the configurations?

答案1

得分: 0

nginx配置中的proxy_pass http://0.0.0.0:8000;这行没有意义(因为nginx部署在容器内,地址0.0.0.0无法解析),在这里,您需要在最终设备或域名之前指定完整的地址。在您的情况下,您可以使用Docker Compose文件中的域名,例如:
proxy_pass http://app:8000;
或者手动设置主域名

app:
  build: app
  hostname: my-fastapi-app
  ports:
    - "8000:8000"

proxy_pass http://my-fastapi-app:8000;
英文:

The line proxy_pass http://0.0.0.0:8000; of nginx conf does not make sense (since nginx is deployed inside a container, address 0.0.0.0 cannot be resolved), here you need to specify the full address before the end device or domain name. In your case, you can use the domain name from the docker compose file such as
proxy_pass http://app:8000 or set domain main manually

app:
	build: app
    hostname: my-fastapi-app
	ports:
	  - "8000:8000"

and

proxy_pass http://my-fastapi-app:8000;

huangapple
  • 本文由 发表于 2023年3月3日 18:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定