最近在尝试反向代理EXSi虚拟化平台时,发现vnc无法连接,提示连接失败,遂在互联网上查找解决方案。

问题原因是Nginx默认没有支持Websocket代理,需要手动修改反向代理部分的配置文件

在反向代理部分添加允许http升级的参数即可解决问题

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

例如,我的ESXi服务器在127.0.0.1的5001端口监听https协议,配置文件如下

#PROXY-START/
location  ~* \.(gif|png|jpg|css|js|woff|woff2)$
{
    proxy_pass https://127.0.0.1:5001;
    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 REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    expires 12h;
}
location /
{
    proxy_pass https://127.0.0.1:5001;
    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 REMOTE-HOST $remote_addr;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache

        add_header Cache-Control no-cache;
}

#PROXY-END/

Q.E.D.