Nginx鐨刉ebSocket璺ㄥ煙璁块棶鎺у埗鎬庝箞閰嶇疆
瑕侀厤缃甆ginx浠ュ厑璁竁ebSocket璺ㄥ煙璁块棶锛屽彲浠ュ湪Nginx閰嶇疆鏂囦欢涓坊鍔犱互涓嬪唴瀹癸細
location / {
if ($http_upgrade != "websocket") {
proxy_pass http://backend;
break;
}
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
鍦ㄨ繖涓厤缃腑锛屾垜浠鍏堟鏌ヨ姹傛槸鍚﹀寘鍚?code>Upgrade澶达紝濡傛灉鍖呭惈涓斿€间负websocket锛屽垯琛ㄧず杩欐槸涓€涓猈ebSocket杩炴帴銆傛帴涓嬫潵锛屾垜浠缃?code>Upgrade鍜?code>Connection澶达紝骞跺皢璇锋眰浠g悊鍒板悗绔湇鍔″櫒銆傛敞鎰忓皢http://backend鏇挎崲涓哄疄闄呯殑鍚庣鏈嶅姟鍣ㄥ湴鍧€銆?/p>
鍙﹀锛岃繕闇€瑕佸湪Nginx閰嶇疆鏂囦欢涓坊鍔犱互涓嬪唴瀹规潵鍏佽璺ㄥ煙璁块棶锛?/p>
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization';
}
鍦ㄨ繖涓厤缃腑锛屾垜浠缃簡鍏佽鎵€鏈夋潵婧愯闂紙Access-Control-Allow-Origin: *锛夛紝鍏佽鎼哄甫鍑瘉锛?code>Access-Control-Allow-Credentials: true锛夛紝浠ュ強鍏佽鐨勮姹傛柟娉曞拰澶撮儴銆傛牴鎹疄闄呴渶姹傦紝鍙互鏍规嵁闇€瑕佷慨鏀硅繖浜涢厤缃€?/p>
瀹屾垚浠ヤ笂閰嶇疆鍚庯紝閲嶆柊鍔犺浇Nginx閰嶇疆鏂囦欢鍗冲彲浣块厤缃敓鏁堛€傝繖鏍峰氨閰嶇疆浜哊ginx浠ュ厑璁竁ebSocket璺ㄥ煙璁块棶銆?/p>