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

How to configure Nginx to support websocket

王林
Release: 2023-05-17 21:28:13
forward
5525 people have browsed it

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;        
      }
}
Copy after login

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";
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template