【Nginx】 The interviewer asked me if Nginx can configure websockets. I gave him a live demo!!

Writing in the front

In today’s Internet field, no matter APP or H5, no matter wechat terminal or small program, as long as it is a decent product, in order to increase the sense of interaction and user viscosity, chat function will be more or less involved. For Web and H5, the simplest way to achieve chat is to use WebSocket. In the process of realizing WebSocket chat, multiple WebSocket services are often deployed in the background. Between multiple WebSocket services, load balancing can be carried out through Nginx. Today, we will talk about Nginx how to configure WebSocket.

Nginx configuration WebSocket

Nginx is also relatively easy to configure WebSocket. You only need to configure WebSocket in nginx.conf file. This approach is simple, but effective, and extends the capabilities of the WebSocket server horizontally.

First show the configuration file directly, as shown below (if you use it, just copy it and change the IP address and port)

map $http_upgrade $connection_upgrade { 
	default upgrade; 
	' 'close; } upstream wsbackend{ server ip1:port1; server ip2:port2; keepalive 1000; } server { listen 20038; The location / {proxy_http_version 1.1; proxy_pass http://wsbackend; proxy_redirect off; proxy_set_header Host$host; 
		proxy_set_header X-Real-IP $remote_addr; 
		proxy_read_timeout 3600s; 
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
		proxy_set_header Upgrade $http_upgrade; 
		proxy_set_header Connection $connection_upgrade; }}Copy the code

Next, we will analyze the specific meaning of the above configuration.

First of all:

map $http_upgrade $connection_upgrade { 
	default upgrade; 
	' ' close; 
} 
Copy the code

It means:

  • If $http_upgrade is not "(empty), $connection_upgrade is upgrade.
  • If $http_upgrade is "(empty), $connection_upgrade is close.

Second:

upstream wsbackend{ 
	server ip1:port1; 
	server ip2:port2; 
	keepalive 1000; 
} 
Copy the code

Nginx load balancing:

  • Two servers (ip1:port1) and (ip2:port2) are available.
  • Keepalive 1000 represents the number of idle connections held by the upstream server for each Nginx process. If there are too many idle connections, the keepalive 1000 closes the least used ones. Of course, this is not a limit on the total number of connections, but rather the size of the free connection pool, which should be set to a value that the upstream server can handle.

Finally:

server { listen 20038; The location / {proxy_http_version 1.1; proxy_pass http://wsbackend; proxy_redirect off; proxy_set_header Host$host; 
		proxy_set_header X-Real-IP $remote_addr; 
		proxy_read_timeout 3600s; 
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
		proxy_set_header Upgrade $http_upgrade; 
		proxy_set_header Connection $connection_upgrade; }}Copy the code

Represents the configuration of the listening server

  • Listen 20038 indicates the port that Nginx listens on
  • Locations/Indicates the path to listen on (/ indicates all paths, universal matching, equivalent to default)
  • Proxt_http_version 1.1 indicates that the HTTP version sent by the reverse proxy is 1.1. HTTP1.1 supports long connections
  • proxy_pass http://wsbackend; Represents the URI of the reverse proxy, where load balancing variables can be used
  • proxy_redirect off; Default replaces the path after proxy_pass
  • Host $host; $host is the nginx built-in variable that represents the current request header, and proxy_set_header sets the request header
  • proxy_set_header X-Real-IP $remote_addr; Indicates that the source IP of the delivery is still the current client IP
  • Proxy_read_timeout 3600 s; Table after the interval between two requests exceeds 3600s before closing this connection, default 60s, the main cause of automatic closing
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; This parameter indicates that the X-Forwarded-For header does not change
  • proxy_set_header Upgrade $http_upgrade; Indicates that the Upgrade is not changed
  • proxy_set_header Connection $connection_upgrade; Indicates that if $http_upgrade is upgrade, the request is upgrade(websocket), and if not, the connection is closed

Ok, that’s enough for today! Don’t forget to click a like, to see and forward, let more people see, learn together, progress together!!

Write in the last

If you think glacier wrote good, please search and pay attention to “glacier Technology” wechat public number, learn with glacier high concurrency, distributed, micro services, big data, Internet and cloud native technology, “glacier technology” wechat public number updated a large number of technical topics, each technical article is full of dry goods! Many readers have successfully moved to Dachang by reading articles on the “Glacier Technology” wechat official account; There are also many readers to achieve a technological leap, become the company’s technical backbone! If you also want to like them to improve their ability to achieve a leap in technical ability, into the big factory, promotion and salary, then pay attention to the “Glacier Technology” wechat public account, update the super core technology every day dry goods, so that you no longer confused about how to improve technical ability!