Nginx configures wss access to realize websocket communication of WeChat applet

WSS is short for Web Socket Secure, which is the encrypted version of WebSocket. WSS and WS are similar to HTTPS and HTTP, the difference is that they are different communication protocols, and both run on top of SSL (Secure Socket Layer, Secure Socket Layer).

Although the WebSocket protocol is different from the HTTP protocol, the handshake of the WebSocket protocol is compatible with HTTP. It uses the HTTP Upgrade protocol header to upgrade the connection from an HTTP connection to a WebSocket connection. This feature allows WebSocket applications to be easily applied to existing infrastructure

1 Core code block

location /wss {  
 			
      proxy_http_version 1.1;  
      proxy_set_header Upgrade $http_upgrade;  
      proxy_set_header Connection "Upgrade";  
    } 

2 Complete nginx configuration

insert image description here

 # 后台接口服务
upstream zb_backendss {
    server localhost:8080       weight=10;

}
upstream zb_backendss_admin {
    server localhost:8083       weight=10;
}

server {

    listen       443 ssl;
    listen 80 ;
    server_name wuliu.test.com;
    ssl on;
    ssl_certificate  6447499_wuliu.test.com.pem;   
    ssl_certificate_key 6447499_wuliu.test.com.key;
    ssl_session_timeout  300m;   
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   
    ssl_prefer_server_ciphers   on; 

    location /Z9Mjp4uAGR.txt {
        alias   /opt/learncoal-webapps/web-admin-toupiao/le/Z9Mjp4uAGR.txt;

              }

    location /demo/wx {
                proxy_pass http://zb_backendss;
                proxy_set_header Host               $host;
                proxy_set_header X-Real-IP          $remote_addr;
                proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;

                 }
    location /demo/imserver {
                proxy_pass http://zb_backendss;
                proxy_set_header Host               $host;
                proxy_set_header X-Real-IP          $remote_addr;
                proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;

                proxy_http_version 1.1;  
                proxy_set_header Upgrade $http_upgrade;  
                proxy_set_header Connection "Upgrade";  
        }
      location /demo/admin {
                proxy_pass http://zb_backendss_admin;
                proxy_set_header Host               $host;
                proxy_set_header X-Real-IP          $remote_addr;
                proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
                }
 
      location /admin-api/storage {
                rewrite ^/admin-api/storage/(.*)$ /$1 break;
                root /opt/ershoutushou/api/dts/storage;               
               }

     location / {
                 root   /opt/learncoal-webapps/web-admin-wuliu/dist;
                  index index.html index.htm;
                }

}

3 Access directly in WeChat applet

wss://wuliu.test.com/wss

Guess you like

Origin blog.csdn.net/zl18603543572/article/details/122280758