背景
项目架构:springboot+vue,前后端分离
代码需要连接小程序,小程序需要ssl证书认证,相应的协议需要:http->https,ws->wss
需要准备
域名 证书(与域名绑定)nginx
https
仅支持https,不支持wss,实现比较简单
https://blog.youkuaiyun.com/kk_star/article/details/83273045?ops_request_misc=&request_id=&biz_id=102&utm_term=springboot%20https&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-83273045.nonecase&spm=1018.2226.3001.4187
https+wss
针对部分服务需要https,部分需要http,以下配置支持http、https、ws、wss
wss.conf
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# https通过下面的反向代理到上面的接口去 https ok wss undo 域名 _未配置
server {
listen 443 ssl;
#此处配置域名
server_name _;
#ssl on;
ssl_certificate ../ssl/grg.crt;#你的上传到服务器的证书位置
ssl_certificate_key ../ssl/grg.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;#开启自定义头信息的下划线
#https协议转发 小程序里面要访问的链接
location /{
proxy_pass http://127.0.0.1:9999;#代理到原有的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 *;#跨域访问设置
}
#wss协议转发 小程序里面要访问的链接 写具体ip
location /socket.io {
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://127.0.0.1:9843;
}
}
#http测试配置ok
server {
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:9999;
}
location /socket.io {
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
#ws用不了localhost 写具体ip
proxy_pass http://127.0.0.1:9843;
}
}
在nginx.conf中配置读取wss.conf