簡體   English   中英

Nginx Config for SSL PHP網站和Websockets WSS?

[英]Nginx Config for SSL PHP site & Websockets WSS?

我正在嘗試設置websocket連接(wss)。 我的域使用ssl(certbot),由Nginx提供支持。 我不確定如何配置/etc/nginx/sites-available/example.com文件。

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
...
}

我將以下內容添加到我的配置塊中:

location /websocket {
    proxy_pass         https://example.com;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection "upgrade";
    proxy_set_header   Host $host;
}

通過wss://example.com/連接時,出現錯誤消息

WebSocket connection to 'wss://example.com/' failed: 
Error during WebSocket handshake: Unexpected response code: 200

那里的大多數示例都使用nodejs框架來為其站點提供服務,但是我使用的是php。 有沒有可以指導我的教程,或者有人對如何配置我的配置文件有想法嗎? 謝謝!

我發現proxy_pass https://example.com; 在您的nginx配置中,但是https://example.com是http服務器而不是websocket服務器,因此您獲得了響應代碼200

這是一個例子

location ~ ^/websocket/(.*$) {
    tcp_nodelay on;
    keepalive_timeout  1200s 1200s;
    keepalive_requests 100000;
    proxy_pass http://127.0.0.1:51966/$1;
    proxy_read_timeout 1200s;
    proxy_send_timeout 1200s;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

websocket正在監聽的端口是51966

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM