前言
最近接触了有关微信小程序的websocket,小程序后台服务的需要https配置和wss配置,经过一番测试,终于实现了小程序后台服务的部署。这里就总结一下微信小程序,SpringBoot为后台的使用nginx配置来解决https配置和wss配置方案。
方案
阿里免费CA证书,nginx代理请求转发,netty构建websocket服务端并保持心跳
阿里证书就不介绍了,很简单。
nginx代理
小程序有个苛刻的要求https、wss(小程序websocket需要)
wss其实是http的升级协议,所以它们是可以共用一个端口的
这里给出nginx的ngxin.conf的配置案例:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server localhost:9999; #本地websocket反向代理地址
}
upstream web {
server localhost:8090; #本地web反向代理地址
}
server {
listen 443;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
ssl on;
ssl_certificate 214660810500.pem; #证书,我的证书和nginx.conf在同一目录
ssl_certificate_key 214660810500.key; #证书
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://web;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header Host $http_host;
}
#小程序websocket访问:wss://localhost/websocket
location /websocket {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
web服务:tomcat 8090
websocket服务: netty 9999
注意:nginx 1.3以上版本才开始支持websocket的反向代理
版权声明:本文为qq_28089993原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。