繁体   English   中英

如何使用安全 WebSockets (wss) 在 Azure 应用服务的 Docker 容器中设置 nginx?

[英]How to set up nginx in a Docker container in an Azure App Service with secure WebSockets (wss)?

我有 2 个服务在 Azure 应用服务的 Docker 容器中运行。 我想从每个服务中公开一些 API,并且一个服务也使用套接字。 在本地使用 Docker 容器(无 SSL)时一切正常,但在使用 Azure 应用程序服务时却不行:如果我转到我的服务的前端,例如https://myservicename.azurewebsites.net ,那么创建时会出错一个 WebSocket 连接。 DevTools 说它正在尝试连接到“wss://myservicename.azurewebsites.net/stream”。

它们都是 Python 服务。 一个是 Flask 服务,另一个使用 Streamlit。

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:

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)

我应该更改哪些内容才能使安全的 WebSockets 在 Azure 应用服务中工作? 我已经看到有关在 nginx 配置中配置 SSL 和证书的信息,但我认为我不必担心这一点,因为 Azure 应用服务会为我的应用处理证书内容。

websocket-nginx文档中,您的nginx.conf似乎缺少 Host 标头

proxy_set_header Host $host;

我对标题缺少大写的经验有好有坏,因此请尝试使用大写“U”进行“升级”

proxy_set_header Connection "Upgrade";

暂无
暂无

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

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