Nginx: [emerg] unknown "connection_upgrade" variable solution and thinking

problem

 《nginx: [emerg] unknown
Start nginx after updating the main branch one day, and the error is reported: nginx: [emerg] unknown "connection_upgrade" variable

resolvent

Online search found that there was a problem with the nginx configuration file. Just add the following map code block.

 http { map $http_upgrade $connection_upgrade { default upgrade; ''      close; } server { location / { #… proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } } }

reflection

Although the problem was solved, later I wanted to know why this happened and the real principle of the solution.
stay This blog We can see that the problem is Nginx proxy websocket On.

What is websocket

The traditional http communication mode is that the client initiates a request, and the server receives the request and responds.
 《nginx: [emerg] unknown

The websocket protocol reuses the http handshake channel. Specifically, the client negotiates the upgrade protocol with the WebSocket server through HTTP requests.

The first step is to establish a connection. The client sends a request for protocol upgrade in the format of http message, and the server responds to the protocol upgrade.
 《nginx: [emerg] unknown

The second step is to exchange data. The client and server can use websocket protocol for two-way communication.
 《nginx: [emerg] unknown

Nginx reverse proxy websocket

 《nginx: [emerg] unknown
First, the client initiates a request for protocol upgrade, and nginx needs to recognize it as a request for protocol upgrade when intercepting it, so the upgrade head and connection head must be explicitly set, as follows:

 location /sockjs-node/ { proxy_pass  http://127.0.0.1:4200/sockjs -node/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; }

After completion, nginx processes it as a WebSocket connection.

 《nginx: [emerg] unknown

Then, the server responds to the upgrade request, and nginx acts as the agent for processing. At this time, the following configuration needs to be performed:

 http { map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { ... } }

At this time, the place I modified at the beginning appears. Combined with the content of the above paragraph, I can guess that the role of the map code segment is mainly to construct and change the value of $connection_upgrade according to the value of $http_upgrade in the client request, that is, to create a new variable $connection_upgrade according to the value of the variable $http_upgrade.
Since I do not map, it does not know what connection_upgrade is, so it will appear unknown "connection_upgrade" variable Error.

summary

Even a small change will hide huge information. If you stop at solve the problem , not Explore questions , there will never be progress.

My level is limited. You are welcome to point out your shortcomings in the comment area. Your feedback is my driving force for progress!
Reference documents:
https://www.nginx.com/blog/we
https://www.cnblogs.com/chyin

Original author: Chen Jie
Original address: https://segmentfault.com/a/1190000018712908
This article is transferred from an online article, which is only for sharing knowledge. If there is infringement, please contact the blogger to delete it.
give the thumbs-up