我在 Go 服務(wù)前使用了一個(gè) nginx 實(shí)例。我想將端口 80 上的任何內(nèi)容重定向到 https。[完畢]/* 處的所有(非 websocket)https 請(qǐng)求都應(yīng)轉(zhuǎn)到https://localhost:8443/ * [完成]/ws/* 的所有 websocket https 請(qǐng)求都應(yīng)該轉(zhuǎn)到https://localhost:8443/ws/ * [missing]我目前的配置:ssl_certificate ...ssl_certificate_key ...ssl_ciphers ...ssl_prefer_server_ciphers on;server { listen 80; location / { return 301 https://$host$request_uri; }}server { listen 443 ssl; server_name www.mydomain.com mydomain.com; add_header Strict-Transport-Security "max-age=31536000"; location /ws { <--- This only works for /ws but not /ws/app1 proxy_pass http://localhost:8443/ws; proxy_http_version 1.1; proxy_set_header Host $http_host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location / { <--- Catches anything, even without wildcard ?! proxy_pass http://localhost:8443; }}server { listen 443 ssl; server_name *.mydomain.com; return 444;}為什么這是必要的?好吧,據(jù)我所知,您必須明確設(shè)置升級(jí)標(biāo)頭,所以我想您必須指定另一個(gè)位置。理想情況下,我只使用一個(gè)位置,但隨后 websockets 被阻止(因?yàn)樯?jí)標(biāo)頭永遠(yuǎn)不會(huì)進(jìn)入 Go 服務(wù)......)我不是 nginx 專(zhuān)家,所以請(qǐng)耐心等待 =)。
1 回答

藍(lán)山帝景
TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
在https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms查看文章
您沒(méi)有使用 any location_match
,因此匹配是前綴匹配。
使用~
作為位置匹配修飾符把它解釋為一個(gè)正則表達(dá)式。
該行location /ws
應(yīng)該匹配每個(gè)以 開(kāi)頭的查詢(xún)/ws
。
- 1 回答
- 0 關(guān)注
- 179 瀏覽
添加回答
舉報(bào)
0/150
提交
取消