首先
WebSocket協(xié)議不是超文本傳輸協(xié)議。
盡管WebSocket協(xié)議確實使用HTTP請求啟動了一個新連接,但它會迅速將連接升級為完整的WebSocket (這需要在初始請求中包含Connection: upgrade頭,而不是Connection: keep-alive)。
所以,在nginx配置的時候,先對請求進(jìn)行升級

配置項:
map $http_upgrade $connection_upgrade {
default keep-alive; #默認(rèn)為keep-alive 可以支持一般http請求
'websocket' upgrade; #如果為websocket 則n為upgrade可升級的。
}
然后再對nginx代理的地址進(jìn)行配置文章來源:http://www.zghlxwxcb.cn/news/detail-604109.html

location ^~ /shared_java/ {
proxy_redirect off;
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 Upgrade $http_upgrade;#此處配置 上面定義的變量
proxy_set_header Connection $connection_upgrade;
proxy_pass http://127.0.0.1:8086/;
}
至此,ws和http請求可以同時被nginx代理了文章來源地址http://www.zghlxwxcb.cn/news/detail-604109.html
到了這里,關(guān)于Nginx共同配置WS(WebSocket)和Http的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!