文章目录
一、背景
1、由于联调环境需要在内网或外网使用wss,这里使用Nginx作为代理实现ws或wss。
2、WebSocket 在线测试
二、使用Nginx代理ws
1、Nginx配置如下
server {
listen 9001; # 监听9000端口
server_name 192.168.1.219;
add_header Access-Control-Allow-Origin *;
location / {
#添加wensocket代理
proxy_pass http://192.168.1.219:5670; #websocket服务器
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 1800s;
proxy_send_timeout 1800s;
proxy_read_timeout 1800s;
}
}
2、测试结果如下
三、使用Nginx代理wss
说明:如果是内网环境使用,可以手动创建证书使用,如果是公网环境,则必须为公网域名证书或ip证书,不可以手动为公网ip或域名创建证书,否则调用会失败的。
3.1、内网使用Nginx代理wss
1、内网使用当前工具创建证书文件
《Linux运维实战:使用shell脚本一键自动生产nginx https证书(方法二)》
2、Nginx配置如下
server {
listen 9002 ssl; # 监听9000端口
server_name 192.168.1.219;
ssl_certificate /etc/nginx/ServerCA.pem;
ssl_certificate_key /etc/nginx/ServerCA.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
ssl_verify_client off;
add_header Access-Control-Allow-Origin *;
location / {
#添加wensocket代理
proxy_pass http://192.168.1.219:5670; # websocket服务器。不用管 ws://
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 1800s;
proxy_send_timeout 1800s;
proxy_read_timeout 1800s;
}
}
3、测试结果如下
3.2、外网使用Nginx代理wss
说明:如果是公网环境必须使用公网域名证书文件或公网ip证书文件,手动为公网ip或域名创建证书文件wss是不生效的。
1、Nginx配置如下
server {
listen 9000 ssl; # 监听9000端口
server_name telsip.svc.com 258.11.121.31;
ssl_certificate /etc/nginx/6723447__svc.com.pem;
ssl_certificate_key /etc/nginx/6723447__svc.com.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
ssl_verify_client off;
add_header Access-Control-Allow-Origin *;
location / {
#添加wensocket代理
proxy_pass http://192.168.1.219:5670; # websocket服务器。不用管 ws://
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 1800s;
proxy_send_timeout 1800s;
proxy_read_timeout 1800s;
}
}
2、测试结果如下
总结:整理不易,如果对你有帮助,可否点赞关注一下?
更多详细内容请参考:Linux运维实战总结