我在 Go 服務前使用了一個 nginx 實例。我想將端口 80 上的任何內容重定向到 https。[完畢]/* 處的所有(非 websocket)https 請求都應轉到https://localhost:8443/ * [完成]/ws/* 的所有 websocket https 請求都應該轉到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;}為什么這是必要的?好吧,據我所知,您必須明確設置升級標頭,所以我想您必須指定另一個位置。理想情況下,我只使用一個位置,但隨后 websockets 被阻止(因為升級標頭永遠不會進入 Go 服務......)我不是 nginx 專家,所以請耐心等待 =)。
1 回答

藍山帝景
TA貢獻1843條經驗 獲得超7個贊
在https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms查看文章
您沒有使用 any location_match
,因此匹配是前綴匹配。
使用~
作為位置匹配修飾符把它解釋為一個正則表達式。
該行location /ws
應該匹配每個以 開頭的查詢/ws
。
- 1 回答
- 0 關注
- 191 瀏覽
添加回答
舉報
0/150
提交
取消