Home  >  Article  >  Operation and Maintenance  >  How to configure Nginx to support websocket

How to configure Nginx to support websocket

王林
王林forward
2023-05-17 21:28:135341browse

1. Understanding of wss and nginx proxy wss:

1. The wss protocol is actually websocket SSL, which means adding an SSL layer to the websocket protocol, similar to https (http SSL).

2. Use nginx to proxy wss [communication principle and process]

  • The client initiates a wss connection to connect to nginx

  • Nginx forwards the WSS protocol data to Workerman's WebSocket protocol port and converts it into WS protocol data

  • Workerman does business logic processing after receiving the data

  • When Workerman sends a message to the client, it is the opposite process. The data is converted into wss protocol through nginx/and then sent to the client

2. Nginx supports websocket Configuration

server {
      listen   80;
      server_name 域名;
      location / {
        proxy_pass   http://127.0.0.1:8080/; // 代理转发地址
     proxy_http_version 1.1;
        proxy_read_timeout   3600s; // 超时设置
        // 启用支持websocket连接
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
      }
      location /upload { // 静态资源地址
            root   /mnt/resources;        
      }
}

The important thing is these two lines, which indicate that when the websocket connection comes in, a connection upgrade is performed to turn the http connection into a websocket connection.

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

proxy_read_timeout; Indicates the time to wait for the server response after the connection is successful. If not configured, the default is 60s;

proxy_http_version 1.1; Indicates that the http version is 1.1

The above is the detailed content of How to configure Nginx to support websocket. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete