gpt4 book ai didi

python - docker 中的 Nginx、fastapi 和 streamlit - 反向代理不适用于 streamlit

转载 作者:行者123 更新时间:2023-12-05 03:35:05 61 4
gpt4 key购买 nike

我想使用 docker 来容器化 nginx、fastapi 和 2 个 streamlit 应用程序。所有 3 个应用程序(fastapi、2streamlit 应用程序)都不会相互交互。 Nginx 应该作为这 3 个应用程序的反向代理。对于 fastapi,它正在工作。我可以将 rest-api 请求发送到 http://ip:80/twxservices 。“twxservices”作为端点被添加到 app.py 文件中,而不是在 nginxconfig 中。

streamlit 应用程序无法通过此 http://ip:80/stream1 和 http://ip:80/stream2 访问我收到错误:404:未找到

在此先感谢您的帮助。在下面找到文件结构和配置文件。这是文件结构以及反向代理应该如何工作:

enter image description here enter image description here

docker-compose 文件:


services:
web:
build:
context: ./nginxfolder
dockerfile: Dockerfile
container_name: web

ports:
- 80:80
networks:
- my-network
depends_on:
- app

app:
build:
context: ./twxservicesfolder
dockerfile: Dockerfile
container_name: app

networks:
- my-network

spectrum:
build:
context: ./spectrumfolder
dockerfile: Dockerfile
container_name: spectrum

networks:
- my-network

dgraph:
build:
context: ./dgraphfolder
dockerfile: Dockerfile
container_name: dgraph

networks:
- my-network

networks:
my-network:
driver: bridge

nginx 配置(default.conf):

upstream fastapi-backend {
server app:8000;
}



server {
listen 80;


location / {
proxy_pass http://fastapi-backend;

proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;


proxy_redirect off;
proxy_buffering off;
client_body_buffer_size 100M;
client_max_body_size 100M;
}

location /stream1 {
proxy_pass http://spectrum:8501/stream1;
}
location ^~ /stream1/static {
proxy_pass http://spectrum:8501/stream1/static/;
}
location ^~ /stream1/healthz {
proxy_pass http://spectrum:8501/stream1/healthz;
}
location ^~ /stream1/vendor {
proxy_pass http://spectrum:8501/stream1/vendor;
}
location /stream1/spectrum {
proxy_pass http://spectrum:8501/stream1/spectrum;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}

location /stream2 {
proxy_pass http://dgraph:8503/stream2;
}
location ^~ /stream2/static {
proxy_pass http://dgraph:8503/stream2/static/;
}
location ^~ /stream2/healthz {
proxy_pass http://dgraph:8503/stream2/healthz;
}
location ^~ /stream2/vendor {
proxy_pass http://dgraph:8503/stream2/vendor;
}
location /stream2/dgraph {
proxy_pass http://dgraph:8503/stream2/dgraph;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}

}

Dockerfile nginx 容器:

FROM nginx:latest

RUN rm /etc/nginx/conf.d/default.conf
COPY default.conf /etc/nginx/conf.d

Dockerfile streamlit app dgraph 容器:

FROM python:3.8

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

EXPOSE 8503
COPY app2.py /var/dashboard/app2.py
CMD streamlit run /var/dashboard/app2.py --server.address="0.0.0.0" --server.port="8503"

Dockerfile streamlit 应用频谱容器:

FROM python:3.8

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

EXPOSE 8501
COPY app1.py /var/dashboard/app1.py
CMD streamlit run /var/dashboard/app1.py --server.address="0.0.0.0" --server.port="8501"

Dockerfile fastapi 应用程序 twxservices 容器:

FROM python:3.8-slim-buster

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .

ENV PYTHONUNBUFFERED 1

EXPOSE 8000
CMD ["uvicorn","app:app","--proxy-headers","--host","0.0.0.0","--forwarded-allow-ips","*"]

最佳答案

我让它与这些 nginx 配置一起工作

    location /spectrum {
proxy_pass http://spectrum1:8501/spectrum;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}

和这个 streamlit 应用程序的 Dockerfile 配置:

FROM python:3.8
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

EXPOSE 8501
COPY app1.py /var/dashboard/app1.py
CMD streamlit run /var/dashboard/app1.py --server.address="0.0.0.0" --server.port="8501" --server.baseUrlPath="spectrum" --server.enableCORS=false --server.enableXsrfProtection=false

关于python - docker 中的 Nginx、fastapi 和 streamlit - 反向代理不适用于 streamlit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69979818/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com