Nginx-配置WebSocket反向代理

客户环境因开放端口有限,部署Portainer后默认端口无法访问,故使用nginx做转发,按照正常http协议配置nginx,启动后发现portainer默认的进入容器的功能无法使用,排查后发现报错如下。

错误信息为websocket连接问题,需要更改nginx配置为websocket。

仅修改http块中的内容即可。

map $http_upgrade $connection_upgrade {
        default keep-alive;
        'websocket' upgrade;
    }
 
upstream ceair-idps-server {
    server    127.0.0.1:20269;
}
server {
    listen    20269;
    server_name 127.0.0.1;
 
    location ^~ /portainer/ {
        client_max_body_size 5000M;
        proxy_connect_timeout 1200;
        keepalive_timeout 1200;
        proxy_read_timeout 1200;
        proxy_send_timeout 1200;
        proxy_pass http://127.0.0.1:9000/;
        proxy_http_version 1.1;
        # 重点为添加以下两行,其他部分与正常http配置无任何区别,它表明是websocket连接进入的时候,进行一个连接升级将http连接变成websocket的连接。
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
 
}
posted @ 2023-08-24 10:43  王寄鱼  阅读(251)  评论(0编辑  收藏  举报