简体   繁体   English

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

[英]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 headerwebsocket-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.

相关问题 Azure应用服务泊坞窗容器'服务不可用' - Azure app service docker container 'Service Unavailable' 如何处理 Azure 应用服务 WebSockets 超时? - How to handle Azure App Service WebSockets timeout? 如何在Jenkins Docker容器中挂载Azure App Service存储? - How to mount Azure App Service storage in Jenkins Docker Container? 如何将 Azure“应用服务文件夹”挂载到 docker 容器? - How to mount an Azure 'App Service Folder' to a docker container? azure 中的 Docker 容器应用服务。 如何将 DefaultAzureCredential 用于 keyvault - Docker container app service in azure. How to use DefaultAzureCredential for keyvault Azure 应用服务 Docker 容器重启 - Azure App Service Docker Container Restart Docker 容器日志 Azure 应用服务未见 - Docker container logs for Azure App service is not seen 如何为 azure 容器应用服务设置 fiexd 主机名 - how to set a fiexd hostname for azure container app service Azure 应用服务部署,如何设置 docker ENV 变量 - Azure App Service Deploy, how to set docker ENV variables 如何从 GitLab 容器注册表将 Docker 容器部署到 Azure 应用服务 - How to deploy a Docker container to Azure App Service from GitLab container registry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM