有一个websocket的项目,需要通过nginx给其加上https功能。整个需求就是nginx代理https协议下的websocket.
nginx版本要求是1.9以上,编译参数必须包括:
--with-http_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module
#编译nginx
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module
make
make install
配置https server
# HTTPS server
#
server {
listen 443;
server_name ws.xxxxxxx.com;
ssl on;
ssl_certificate xxxxx.pem;
ssl_certificate_key xxxxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location /
{
index index.jsp;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://10.0.0.6:61001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 30s;
proxy_read_timeout 30s;
proxy_send_timeout 30s;
}
}
启动nginx完成nginx代理的配置。
注:
1. websocket协议,ws对就http, wss对应https。 刚开始测试时ws://ws.xxxx.com去测试https的连接,连接失败;nginx日志报400错误。更换成wss后成功连接到Websocket server.
2. websocket连接成功时,nginx日志的http状态码为101.
3. proxy配置中斜体部分,标记数据是从http到websocket. 协议这块不是很了解,后面详细了解后再补充相关背景知识。