nginx配置websocket支持wss
wss 只能在https的环境下使用。
wss的使用需要websocket服务器的支持。
我websocket服务器使用的是 swoole,swoole原本是不支持 wss的,我这里把原有的代码进行了一定的修改,修改如下:
$anhao = new swoole_websocket_server( $swoole_config[ENV . '_server']['host'], $swoole_config[ENV . '_server']['port'], SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL ); //第四个参数采用了 swoole_ssl 那么必须 配置sslConfig 中的两个文件,否则系统支持wss的支持。 $sslConfig = [ 'ssl_key_file' => '/data/home/songyongzhan/sslCrt/a-test.com.key', 'ssl_cert_file' => '/data/home/songyongzhan/sslCrt/a-test.com.crt' ]; $anhao->set(array_merge($swoole_config[ENV . '_swoole'],$sslConfig));
以上是swoole的配置文件。
那么nginx如何支持wss呢?
首先站点需要是https的,https站点如何配置,请到 进行详细查看。
我这里简单的把80 端口强制跳转到https 443端口上的配置提供出来,如下:
server { listen 80; server_name a-test.com ; access_log /data/log/nginx/songyongzhan/guahaodoctor_im main; error_log /data/log/nginx/songyongzhan/guahaodoctor_im.error; rewrite ^(.*)$ https://$host$1 permanent; } server { listen 443; server_name a-test.com ; root /data/home/songyongzhan/doctorworkerim/public/; index index.php index.html index.htm; access_log /data/log/nginx/songyongzhan/guahaodoctor_webim main; error_log /data/log/nginx/songyongzhan/guanhaodoctor_webim.error; ssl on; #证书 ssl_certificate /data/home/songyongzhan/sslCrt/a-test.com.crt; #私钥 ssl_certificate_key /data/home/songyongzhan/sslCrt/a-test.com.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; fastcgi_param HTTPS on; fastcgi_param HTTP_SCHRMR https; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 3d; access_log off; } location ~ .*\.(js|css)?$ { expires 1d; access_log off; } location ~ /\. { access_log off; deny all; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location /index.php { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/index.php; } error_page 404 /404.html; }
nginx 支持wss配置如下
map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream syzwebsocket { server 127.0.0.1:1130 weight=1; } server { listen 1129; server_name a-test.com; ssl on; #证书 ssl_certificate /data/home/songyongzhan/sslCrt/a-test.com.crt; #私钥 ssl_certificate_key /data/home/songyongzhan/sslCrt/a-test.com.key; ssl_verify_client off; ssl_session_timeout 20m; location / { proxy_pass http://syzwebsocket; proxy_read_timeout 300s; proxy_send_timeout 300s; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } }
127.0.0.1:1129是真正的服务端地址,nginx所在域名是a-test.com,代理的端口号是1129,所以前端访问的时候这样配置:
WEBSOCKET_URL: 'wss://a-test.com:1129',
通过上面的配置 ,采用 /usr/local/nginx/sbin/nginx -t 进行检测是否正确,如果正确 使用 /usr/local/nginx/sbin/nginx -s reload 进行重新启动。
可以看到,系统已经正常执行了。
https://www.jianshu.com/p/def7027b787f
这篇文章是真谛,使用NGINX 将 wss 转换成 ws
https://blog.csdn.net/chopin407/article/details/52937645
说明下:这个是通过nginx将wss反向代理成ws,服务端仍然是ws,而不是wss代理到wss
用了这个配置出现502 bad gateway
server { listen 1129; server_name guahao-test.com; ssl on; #证书 ssl_certificate /data/home/songyongzhan/sslCrt/guahao-test.com.crt; #私钥 ssl_certificate_key /data/home/songyongzhan/sslCrt/guahao-test.com.key; ssl_verify_client off; ssl_session_timeout 20m; location / { proxy_pass http://127.0.0.1:1130; proxy_read_timeout 300s; proxy_send_timeout 300s; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } } /data/home/songyongzhan/im/server.php &
//$anhao = new swoole_websocket_server( // $swoole_config[ENV . '_server']['host'], // $swoole_config[ENV . '_server']['port'], // SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL //); // // //$sslConfig = [ // 'ssl_key_file' => '/data/home/songyongzhan/sslCrt/guahao-test.com.key', // 'ssl_cert_file' => '/data/home/songyongzhan/sslCrt/guahao-test.com.crt' //]; // //$anhao->set(array_merge($swoole_config[ENV . '_swoole'],$sslConfig));