[Solved] Websocket+Nginx appears Connection’ header value must contain ‘Upgrade’ error solution

1. What is Upgrade

Connection: Upgrade indicates that Upgrade is a hop-by-hop field. This header is for proxy
Upgrade: websocket indicates that the browser wants to upgrade to the WebSocket protocol. This header is for the program that ultimately handles the request.
If there is only Upgrade: websocket, the proxy does not support WebSocket upgrade, and should be regarded as a normal HTTP request according to the standard.

Great job by this author to show agents and programs that I’m not a normal requester (https://robberphex.com/why-is-connection-upgrade-necessary/)

2. Possible reasons for the problem

90% of the reasons may be due to nginx configuration. There are other problems. If websocket has been used on the server, these are of course not considered.
a. No constant configuration
map $http_upgrade $connection_upgrade{
default upgrade;
” close;
}
b.nginx version is low

3.nginx configuration solution, where are the important three lines added

Then you need to be familiar with the nginx file: (check where the configuration file is located nginx.conf, if there is no command, look for it under app/nginx/conf)

 server {
    listen 8888; #Port number
    ...
    ...
 location /test { #Request path, only one / is all
 #important three lines
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    proxy_pass http://127.0.0.1:9999/; #Where to proxy
    include proxy_params;
    access_log /home/work/data/httplogs/test/access.log main; #nginx request log
    error_log /home/work/data/weblogs/test/error.log; #nginx error log
   }

Remember to restart nginx (regular sudo ~/app/nginx/sbin/nginx -t;sudo ~/app/nginx/sbin/nginx -s reload;
docker: sudo docker ps -a|grep nginx;sudo docker ecex -it container name bash;nginx -t;nginx -s reload)
At this point it’s likely to be fine.

4. There are still problems to continue to troubleshoot

Really depressing enough, the connection to the socket is still the same mistake;
a. First, look at the nginx error log. If not, look at the access.log to see if the request is 200. The high probability is 400, indicating insufficient parameters.
b. Do not call nginx server

curl --no-buffer -H 'Connection: keep-alive, Upgrade' -H 'Upgrade: websocket' -v -H 'Sec-WebSocket-Version: 13' -H ' Sec-WebSocket-Key: websocket' http://127.0.0.1:9999/websocket ws | od -t c

The data I want is returned, indicating that the service cloth is no problem
c. Take a look at nginx, this is the port above

curl --no-buffer -H 'Connection: keep-alive, Upgrade' -H 'Upgrade: websocket' -v -H 'Sec-WebSocket-Version: 13' -H ' Sec-WebSocket-Key: websocket' http://127.0.0.1:8888/test/websocket ws | od -t c

ah, still good
d. Why is there a problem as soon as I go to the external network, well, the problem of dmz.
Enter dmz, use ifconfig to get the ip, the access is abnormal, there is also nginx configuration here.
Restarting access after configuration is finally normal.
Little knowledge: use netstat -ntlop to get the port, use ps -ef|grep nginx to get the nginx path

What does 5.dmz do?

It is used to allow free access to the internal network while preventing direct access to the external network.

I don’t allow anyone who hasn’t seen this blogger’s paintings. https://blog.csdn.net/itworld123/article/details/122905390