项目使用了w5100来连接互联网,使用了mWebsocket库来连接ws服务器,但是另外的控制端口需要https协议,所以这里使用了nginx来做反向代理,经过不断的折腾,终于配置好了nginx代理(入口wss转内部服务器ws)
配置如下
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
map $http_upgrade $connection_upgrade { #//升级http为websocket
default upgrade;
'' close;
}
upstream websocket {
server localhost:3000; #内网websocket服务器
}
server {
listen 3001 ssl; #外网访问格式:wss://xxx.cn:3001/
server_name xxx.cn;#域名
ssl on;
#域名证书配置
ssl_certificate cert/www.xxx.cn.pem;
ssl_certificate_key cert/www.xxx.cn.key;
#超时设置长一点
ssl_session_timeout 20m;
#由于业务上不需要安全,因为本身就是开放的接口,不需要客户端认证
ssl_verify_client off;
location / {
proxy_pass http://websocket; #转发到内网的请求 == ws//websocket
#http1.0不支持本配置方法
proxy_http_version 1.1;
##//升级http为websocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
}