因为小程序对后台接口和websocket都有要求使用https,但是当前项目api和socket是在一个项目,分别使用了8090和8091,所以需要在443这个端口实现一个分流,直接上conf文件配置吧。
upstream webapi.teerus.cn { #这里类似一个别名,在下文中使用
server 127.0.0.1:8091; #api服务器的地址
keepalive 64;
}
upstream websocket {
server 127.0.0.1:8090; #websocket服务的地址
keepalive 64;
}
server {
listen 443 ssl;
ssl_certificate webapi.teerus.cn.pem;
ssl_certificate_key webapi.teerus.cn.key;
access_log /www/wwwlogs/webapi.teerus.cn.log;
location /ws { #根据自己项目的实际情况填写,如我的websocket地址wss://webapi.teerus.cn/ws 就用的/ws
proxy_pass http://websocket/ws; #这里使用了websocket这个别名,原样转发到/ws这个地址供websocket使用
proxy_read_timeout 60s;
proxy_set_header Host $http_host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
}
location / { #除了/ws其它都走这个代理
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://webapi.teerus.cn/; #这里使用了webapi.teerus.cn
}
}