nginx反代docker试了很久总是出错,但是不用nginx反代直接运行则运行正常,尝试很久才明白原因。
这其实是 Streamlit 特有的问题。
它默认认为自己直接对外服务(host 为 localhost,WebSocket 直接连自己的8501端口),所以一旦你反向代理,Streamlit里前端的WebSocket就失效了。
为什么出错?
Streamlit前端会自动尝试建立 WebSocket连接,比如:
1 | wss://sub.yourdoman.com/_stcore/stream |
但它不知道你用的是 wss
(HTTPS+WebSocket),而且路径前缀也会错,导致连不上。
Streamlit反代时,必须加一些额外配置:
解决方案
修改你的 docker-compose
,在 ndsri-ai-calculator
容器启动时加环境变量(或者加启动参数),告诉Streamlit:
1.dock-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | services: your_app: container_name: your_app image: yourusername/your_app:latest networks: - test_network restart: always depends_on: - test_nginx environment: - STREAMLIT_SERVER_ENABLECORS=false - STREAMLIT_SERVER_ENABLEWEBSOCKETCOMPRESSION=false - STREAMLIT_SERVER_HEADLESS=true - STREAMLIT_SERVER_BASEURLPATH="" - STREAMLIT_SERVER_PORT=8501 - STREAMLIT_SERVER_ADDRESS=0.0.0.0 - STREAMLIT_SERVER_USEWEBSOCKET=true #如果镜像里已经默认 streamlit run了,环境变量就够了,不需要额外加 command。 #command: ["streamlit", "run", "your_app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableCORS=false", "--server.enableWebsocketCompression=false", "--server.headless=true"] test_nginx: container_name: test_nginx image: nginx:latest volumes: - /etc/nginx/ssl:/etc/nginx/ssl - ./nginx.conf:/etc/nginx/nginx.conf ports: - "80:80" - "443:443" networks: - test_network networks: test_network: name: test_network |
2.nginx.conf配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | server { listen 443 ssl; server_name sub.yourdomain.com; ssl_certificate /etc/nginx/ssl/yourdomain.crt; ssl_certificate_key /etc/nginx/ssl/yourdomain.key; location / { proxy_pass http : //yourapp : 8501; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade" ; proxy_redirect off ; proxy_buffering off ; } } |
3.nginx.conf配置
运行docker,重启nginx(如果之前已安装)
1 2 3 | docker-compose up -d #若只重置或者运行 docker restart test_nginx |