[英]How to set up nginx in a Docker container in an Azure App Service with secure WebSockets (wss)?
I have 2 services running in a Docker container in an Azure App Service.我有 2 个服务在 Azure 应用服务的 Docker 容器中运行。 I want to expose some APIs from each and one service also uses sockets.
我想从每个服务中公开一些 API,并且一个服务也使用套接字。 Everything works fine when using the Docker container locally (no SSL), but not when using the Azure App Service: if I go to the front end for my service, eg https://myservicename.azurewebsites.net , then there are errors creating a WebSocket connection.
在本地使用 Docker 容器(无 SSL)时一切正常,但在使用 Azure 应用程序服务时却不行:如果我转到我的服务的前端,例如https://myservicename.azurewebsites.net ,那么创建时会出错一个 WebSocket 连接。 The DevTools says it's trying to connect to "wss://myservicename.azurewebsites.net/stream".
DevTools 说它正在尝试连接到“wss://myservicename.azurewebsites.net/stream”。
They are both Python services.它们都是 Python 服务。 One is a Flask service, the other uses Streamlit.
一个是 Flask 服务,另一个使用 Streamlit。
nginx.conf: nginx.conf:
server {
listen 8502;
# One service runs on port 5000 (Python Flask).
# It just uses HTTP requests.
location /api/run {
proxy_pass http://localhost:5000/run;
}
location /api/results {
proxy_pass http://localhost:5000/results;
}
# The other service runs on port 8501 (Python Streamlit).
# It uses HTTP requests, has a front end web page, and uses WebSockets.
location / {
proxy_pass http://localhost:8501;
# Get web sockets to work too:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Dockerfile: Dockerfile:
FROM mcr.microsoft.com/mirror/docker/library/python:3.9-slim
# Set up some environment variables.
...
EXPOSE 8502
RUN apt --yes update && apt --yes install curl nginx
COPY nginx.conf /etc/nginx/conf.d/my_app.conf
# Install some dependencies.
...
# Start nginx and my 2 services.
CMD nginx && \
(poetry run python ./back_end/main.py & \
poetry run streamlit run ./dashboard/dashboard.py)
What should I change so that secure WebSockets work in the Azure App Service?我应该更改哪些内容才能使安全的 WebSockets 在 Azure 应用服务中工作? I've seen information about configuring SSL and certs in nginx configurations, but I thought I wouldn't have to worry about that because the Azure App Service handles the cert stuff for my app.
我已经看到有关在 nginx 配置中配置 SSL 和证书的信息,但我认为我不必担心这一点,因为 Azure 应用服务会为我的应用处理证书内容。
From the websocket-nginx docs, it seems your nginx.conf
is missing the Host header从websocket-nginx文档中,您的
nginx.conf
似乎缺少 Host 标头
proxy_set_header Host $host;
I've had mixed experience with lack of capitalization for headers, so try "Upgrade" with a capital "U"我对标题缺少大写的经验有好有坏,因此请尝试使用大写“U”进行“升级”
proxy_set_header Connection "Upgrade";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.