1、 编辑nginx.conf文件
#server {
# listen 443 ssl;# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include wss.conf;# 这里我们将反向代理新建一个文件引入进来
在 \nginx\conf目录下,nginx.conf 中配置wss.conf
2、 下载证书 https://www.qcloud.com/product/ssl
3、在nginx\conf目录下,新建wss.conf文件,编辑wss.conf
upstream websocket {
server cloud.xxx.net:9050;# 远程websocket服务器地址
}
upstream web{
server cloud.x'x'x.net;# 远程http接口
}
server {
listen 443;#默认https和wss协议端口
ssl on;
ssl_certificate C:\phpStudy\Apache\conf\ssl\1_cloud.xxx.net_bundle.crt;
ssl_certificate_key C:\phpStudy\Apache\conf\ssl\2_cloud.xxx.net.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
underscores_in_headers on;#开启自定义头信息的下划线
#wss协议转发 小程序里面要访问的链接
location /wss {
proxy_pass http://websocket;#代理到上面的地址去
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
#https协议转发 小程序里面要访问的链接
location /{
proxy_pass http://web;#代理到原有的http的地址去
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Access-Control-Allow-Origin *;#跨域访问设置
}
}
3、启动nginx
nginx.exe
4、停止 nginx -s stop
5、注意wss.conf文件中的“ location /wss {”
在客户端访问wss时,应以wss://xxx.xxx.xxx/wss 一致。
https、wss默认都是443端口
下载证书,证书放在nginx/conf目录