宝塔/Nginx/WebSocket

记录下,宝塔配置默认wss无法连接

选择网站 ->设置->反向代理->没有就添加一个,有就直接点配置文件

#PROXY-START/

location /
{
    proxy_pass http://localhost:端口/;
    proxy_set_header Host www.xxx.com;
    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 $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache
    
    set $static_filesSQnNhw0 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
    	set $static_filesSQnNhw0 1;
    	expires 1m;
        }
    if ( $static_filesSQnNhw0 = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

#PROXY-END/

加两行

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

改动后就是这样

#PROXY-START/

location /
{
    proxy_pass http://localhost:端口/;
    proxy_set_header Host www.xxx.com;
    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 $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    
    set $static_filesSQnNhw0 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
    	set $static_filesSQnNhw0 1;
    	expires 1m;
        }
    if ( $static_filesSQnNhw0 = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

#PROXY-END/

反向代理这么加

宝塔/Nginx/WebSocket_第1张图片

最后提一嘴

解决nginx下websocket的长连接问题https://baijiahao.baidu.com/s?id=1714370851394804830&wfr=spider&for=pc这个文章有用,好东西

你可能感兴趣的:(websocket,nginx,网络协议)