【问题标题】:Nginx Config for SSL PHP site & Websockets WSS?用于 SSL PHP 站点和 Websockets WSS 的 Nginx 配置?
【发布时间】:2018-09-28 22:44:41
【问题描述】:

我正在尝试设置 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。有没有可以指导我的教程,或者有没有人知道如何配置我的配置文件?谢谢!

【问题讨论】:

    标签: php nginx websocket


    【解决方案1】:

    我在你的 nginx 配置中找到 proxy_pass https://example.com;,但是 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;
    }
    

    51966是websocket正在监听的端口

    【讨论】:

    • 我试过这个,它仍然给我一个类似的错误。明天我会谷歌学习更多,谢谢!
    • wss://example.com/websocket/ 这应该会有所帮助,将配置 location /websocket 更改为 location ~^ /websocket 会更好
    • 谢谢。我忘了设置我的主管。这有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多