Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to reverse proxy webSocket in nginx

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to reverse proxy webSocket in nginx, the content is concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.

GET /chat HTTP/1.1Host: server.example.comUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==Sec-WebSocket-Protocol: chat, superchatSec-WebSocket-Version: 13Origin: http://example.com

Children familiar with HTTP may have noticed that there are only a few more things in this HTTP-like handshake request.

Upgrade: websocketConnection: Upgrade This is the core of Websocket, telling Apache, Nginx and other servers: I initiated the Websocket protocol. Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==Sec-WebSocket-Protocol: chat, superchatSec-WebSocket-Version: 13

First of all, Sec-WebSocket-Key is a Base64 encode value, which is randomly generated by the browser, telling the server: Peat, don't fool the nest, I want to verify whether Ni is really a Websocket assistant.

Finally, Sec-WebSocket-Version is to tell the server the Websocket Draft (protocol version) used. At the beginning, the Websocket protocol was still in the Draft stage. There were all kinds of strange protocols, and there were many strange and different things. Firefox and Chrome did not use the same version. At the beginning, too many Websocket protocols were a big problem. But it's okay now. It's settled. It's something everyone uses.

The server will then return the following, indicating that the request has been accepted and the Websocket has been successfully established!

HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=Sec-WebSocket-Protocol: chat

Here is the last responsible area of HTTP, telling customers that I have successfully switched protocols ~

Upgrade: websocketConnection: Upgrade

Still fixed, it's the Websocket protocol that tells clients about upcoming upgrades. At this point HTTP has done all its work, and the next step is to fully follow the Websocket protocol.

Once you understand the protocol, you can move on.

First of all, nginx first configures https certificates.

Server certificate is boss configuration good, I used directly. Check it out for yourself 0.0

Add the following configuration to the service node of the nginx configuration file

location /wss { proxy_pass http://127.0.0.1:8888; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header X-Real-IP $remote_addr; }

Explain the parameters.

/wss This is random, tell Nginx to proxy url, now I set it to wss , when I visit my server https://abc.com/wss,Nginx will map my request to port 8888 of this machine.

proxy_pass To proxy to url, my proxy to port 8888 on this machine.

proxy_http_version The http version used when proxy.

Here's the point:

Key parameters of proxy webSocket

proxy_set_header Upgrade Set the Upgrade of http request header in proxy to the request header of original http request, and the request header of wss protocol to websocket.

proxy_set_header Connection The HTTP header Connection is set to Upgrade because of the proxy's wss protocol.

proxy_set_header X-Real-IP Set IP of original http request to proxy, fill in $remote_addr

As for the response parameters of the websocket protocol, don't worry about them when you reverse proxy.

Here,Nginx reverse proxy webSocket configuration is complete, restart Nginx, try using websocket connection, fill in wss://abc.com/wss in the original wss address. If the websocket is successfully connected, it means that the Nginx reverse proxy websocket has succeeded.

summary

The current configuration is only the configuration of the reverse proxy to the local machine. If you want to reverse the proxy to another host, there may be cross-domain problems when the proxy is used. You need to do cross-domain configuration in the reverse proxy of Nginx.

thinking

You can see this in the Nginx configuration file.

location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;}

This is Nginx php configuration file, I wipe, how so familiar, this configuration list with just websocket reverse proxy so similar. Through the Internet to find out, the original Nginx in the processing of php type requests, the request fastcgi management process processing, fascgi management process select cgi sub-process processing results and return to nginx, and php-fpm is a PHP FastCGI manager,nginx itself can not process PHP, it is just a web server, when receiving the request, if it is a php request, it is sent to the php interpreter processing, and the results are returned to the client. So Nginx is essentially implemented through reverse proxy functionality when processing php type requests.

We can expand our thinking and use Nginx reverse proxy to achieve more functions, such as Tomcat proxy.

location /Tomcat { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_addr; } The above is how to reverse proxy webSocket in nginx. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report