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 forward socket in Nginx

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to forward socket in Nginx, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

Nginx forwarding socket

Pre-configuration preparations

Nginx socket forwarding requires stream module support, compile time requires--with-stream, this module is not installed by default when Nginx is installed. Also Nginx version should be higher than 1.9;

Forward operations through reverse proxy

Notes: Officially provided forwarding method, shortcomings, can not share port 80, need to open additional ports. stream module and event module are level.stream { #Add socket forwarding proxy (upstream server) upstream bss_num_socket { hash $remote_addr consistent; #forwarding destination address and port server 130.51.11.33:19001 weight=5 max_fails=3 fail_timeout=30s; } #Provide forwarding service, i.e. visit localhost:30001, will jump to the forwarding address specified by proxy bss_num_socket server { listen 30001; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass bss_num_socket; For the above, refer to the official documentation. (identical)

another operational

This way, through front-end access, a dedicated URI, Nginx by URI to confirm forwarding. Advantages, sharing 80 ports. Note that location is naturally within the server, at the same level as other locationslocation = /socket { #Forward to this address #Forwarding address can be directly in the form of IP: port, such as: http://127.0.0.1:9999 proxy_pass http://socket.byingcn.com/; #Set header when forwarding, add port number after domain name proxy_set_header Host $host:9502; #Set HTTP1.1 proxy_http_version 1.1; #Forward client's Upgrade request (as websocket important ID)(required) proxy_set_header Upgrade $http_upgrade; #Forward client Connection(as important websocket ID)(optional, but best) proxy_set_header Connection "upgrade"; #Forward IP address proxy_set_header X-Forwarded-For $remote_addr;} Small knot: If you don't understand the meaning of the above two proxy_set_headers, you can grab the package from the foreground and see the websocket request header. The proxy_set_header is a setup request header that goes without saying. Upgrade and Connection information is relatively small. Should remember, through checking a lot of information and pro-test practice. Upgrade and Connection set here This is called Upgrade and Connection in the http request header. The $http_upgrade system variable is a system variable defined by Nginx to obtain the Upgrade in the request header. By changing the Upgrade $http_upgrade; item setting to Upgrade "websocket"(same as the request header); works perfectly. Basically, it can be considered that the above statement is correct. To sum up, basically when forwarding, to forward TCP/IP(socket) data in the header of the Upgrade and Connection to the past (or set the same as the front end) can be. Notes: This part is placed in the ordinary server module; To do not have to reopen other ports, just specify a file. Part of the principle is: The front-end websocket section requests to/socket files. Nginx intercepts and obtains http://socket.byingcn.com/for all operations that request the file. But this operation also requires Nginx installation--with-stream

websocket request header

GET /socket HTTP/1.1Host: test.virtualserver.comConnection: Upgrade Pragma: no-cacheCache-Control: no-cacheUpgrade: websocket Origin: chrome-extension://pfdhoblngboilpfeibdedpjgfnlcodooSec-WebSocket-Version: 13User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.8,en;q=0.6Sec-WebSocket-Key: /mFCat+ePPs61TKUBZ5zHA==Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits Comments: The Connection field and the Upgrade field are explained in detail in the http authoritative guide (http request header); is it helpful to read the above? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report