简体   繁体   中英

Nginx Config for SSL PHP site & Websockets WSS?

I'm trying to setup a websocket connection (wss). My domain uses ssl (certbot) and is powered by Nginx. I am unsure how to configure my /etc/nginx/sites-available/example.com file.

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

I added the following into my config block:

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;
}

When connecting via: wss://example.com/ , I am getting an error of

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

Most of the examples out there are using a nodejs framework to serve their sites, but I am using php. Are there any tutorials out there that can guide me, or does anyone have an idea how to configure my config file? Thanks!

i find proxy_pass https://example.com; in your nginx config, howerever https://example.com is http server instead of websocket server, so you got response code 200

this is an example

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 is the port that websocket is listening

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM