如何使用 nginx 设置安全的 websocket

如何使用 nginx 设置安全的 websocket

我有一个在端口 9000 上运行的 Web 服务器,我想让它在端口 80 上可用,同时我还想在端口 9021 上提供 WebSocket 连接。如果我运行这个,http一切都会正常。但是当我转到httpsWebSocket 时,无法连接。

这是我的 nginx 配置:这给出了警告:

nginx: [warn] conflicting server name "oyun.net" on 0.0.0.0:443, ignored

server {
     listen 443 ssl;
     server_name          oyun.net;
     ssl_certificate      /etc/key.pem
     ssl_certificate_key  /etc/key2.pem
     listen 80;
     location / {
         proxy_pass http://localhost:9000
     }
}

server {
     listen 443 ssl;
     server_name          oyun.net;
     ssl_certificate      /etc/key.pem
     ssl_certificate_key  /etc/key2.pem
     listen 9021;
     location / {
        proxy_pass http://localhost:9000;
        proxy_http_version 1.1;
        proxy_set_header upgrade $http_upgrade;
        proxy_set_header connection "upgrade";
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header host $host;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
     }
 }

这是浏览器错误:

WebSocket connection to 'wss://oyun.net:9021/socket/v1?sri=tcylqwzjnl' failed: 

Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

相关内容