Nginx 配置https 和 websocket wss 访问

最近小程序上迭代了一个基于websocket的即时聊天功能。打开微信公众平台后台设置socket合法域名时发现使用的是wss协议。什么是wss呢?在 SSL 上运行 WebSocket 协议就是 WSS; 在 SSL 上运行 HTTP 协议就是 HTTPS

WSS 是 Web Socket Secure 的简称, 它是 WebSocket 的加密版本. 我们知道 WebSocket 中的数据是不加密的, 但是不加密的数据很容易被别有用心的人窃取, 因此为了保护数据安全, 人们将 WebSocket 与 SSL 结合, 实现了安全的 WebSocket 通信, 即 WebSocket Secure. 所以说 WSS 是使用 SSL 进行加密了的 WebSocket 通信技术。

先在微信公众号后台和字节跳动开发者平台配置socket 合法域名

1、首先申请域名的https证书,各大云服务商都有提供免费https证书,这里我就不演示如何申请证书了。

2、配置https证书。不想手动配置的可以到这个网站一键生成 Nginx config

server
    {
        listen 80;
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name caiguanrong.com www.caiguanrong.com  ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /web_root;

        ssl_certificate /usr/local/nginx/conf/ssl_key/www.caiguanrong.com.pem;
        ssl_certificate_key /usr/local/nginx/conf/ssl_key/www.caiguanrong.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
		ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";#配置加密套件  

        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        include rewrite/thinkphp.conf;
        #error_page   404   /404.html;
		
        include enable-php-pathinfo.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }
        access_log  /home/wwwlogs/caiguanrong.com.log;
    }

3、配置wss


map $http_upgrade $connection_upgrade {  
    default upgrade;  
    '' close;  
}  
#wss配置代理到ws:127.0.0.1:9503 
upstream websocket {  
    server 127.0.0.1:9503;  #ip:port
}  

server
    {
        listen 80;
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name caiguanrong.com www.caiguanrong.com  ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /web_root;

        ssl_certificate /usr/local/nginx/conf/ssl_key/www.caiguanrong.com.pem;
        ssl_certificate_key /usr/local/nginx/conf/ssl_key/www.caiguanrong.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
		ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";#配置加密套件  

        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        include rewrite/thinkphp.conf;
        #error_page   404   /404.html;
		
        include enable-php-pathinfo.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }
		#websocket 配置 通过配置端口指向部署websocket的ip
        location /socket {
            proxy_pass http://websocket;  
            proxy_http_version 1.1;  
            proxy_set_header Upgrade $http_upgrade;   # 升级协议头
            proxy_set_header Connection "Upgrade"; 
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr; 
        }

        access_log  /home/wwwlogs/caiguanrong.com.log;
    }

最后重载nginx 配置。

nginx -t  #检测配置是否正确,输出如下就配置正确了

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

nginx -s reload #重新加载配置文件

然后可以通过在线websocket 测试是否能正确连接 。

蔡关荣博客
请先登录后发表评论
  • latest comments
  • 总共0条评论