简体   繁体   English

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

[英]Nginx, fastapi and streamlit in docker - reverse proxy does not work for streamlit

i want to use docker to containerize nginx, fastapi and 2 streamlit apps.我想使用 docker 来容器化 nginx、fastapi 和 2 个流媒体应用程序。 All 3 apps (fastapi, 2streamlit apps) do not interact with each other.所有 3 个应用程序(fastapi、2streamlit 应用程序)不相互交互。 Nginx should work as reverse-proxy for the 3 apps. Nginx 应该作为 3 个应用程序的反向代理。 For fastapi it is working.对于fastapi,它正在工作。 I can send rest-api requests to http://ip:80/twxservices .我可以将 rest-api 请求发送到 http://ip:80/twxservices 。 "twxservices" as endpoint is added in the app.py file and not in the nginxconfig. “twxservices”作为端点添加在 app.py 文件中,而不是在 nginxconfig 中。

The streamlit apps are not reachable through this http://ip:80/stream1 and http://ip:80/stream2 I get the error: 404: Not found无法通过此 http://ip:80/stream1 和 http://ip:80/stream2 访问流应用程序我收到错误:404:未找到

Thanks in advance for your help.在此先感谢您的帮助。 Find below the filestructures and the config files.在文件结构和配置文件下面找到。 This is the filestructure and how the reverse-proxy should work:这是文件结构以及反向代理应该如何工作:

在此处输入图片说明 在此处输入图片说明

docker-compose file: 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 config (default.conf): 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 container: Dockerfile nginx 容器:

FROM nginx:latest

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

Dockerfile streamlit app dgraph container: Dockerfile streamlit 应用程序 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 app spectrum container: 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" 

Dockerfile fastapi app twxservices container: Dockerfile fastapi app 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","*"]

I made it work with these nginx config我让它与这些 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;
        }

and this Dockerfile config for the streamlit app:以及这个用于 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM