The configuration example of the reverse proxy websocket in Nginx combat

Source: Internet
Author: User
Tags nginx reverse proxy

Http://www.jb51.net/article/112183.htm

Recently encountered a demand in the work, need to use Nginx reverse proxy websocket, after looking for some information, has been tested, so this article mainly introduces the Nginx reverse proxy websocket configuration related information, the need for friends can reference, Let's take a look below.

Implementation scenarios

Using the more mature websocket technology, the WebSocket protocol provides a choice for creating webapp that requires real-time bidirectional communication between the client and server side. As part of the HTML5, WebSocket is easier to develop than the original approach to developing such apps. Most current browsers support WebSocket, such as Firefox,ie,chrome,safari,opera, and a growing number of server frameworks now support WebSocket as well.

WebSocket Cluster

In a real-world production environment where multiple WebSocket servers are required to be high-performance and highly available, the WebSocket protocol requires a load-balancing layer Nginx supports WebSocket starting from 1.3, which can be used as a reverse proxy and load balancing for websocket programs.

Nginx Configuration

Note: See official documents said Nginx in the version after 1.3 only support websocket reverse proxy, so to use the support WebSocket features, you must upgrade to 1.3 later version

Nginx supports websocket by allowing a tunnel to be established between the client and the back-end server. The upgrade and connection headers must be set explicitly in order for Nginx to send the client upgrade request to the backend server.

upstream Wsbackend {  127.0.0.1:8080;   127.0.0.1:8081;} server {  listen  ;  server_name ws. 52itstyle. com;   / {   proxy_pass http://wsbackend;   Proxy_http_version 1.1;    $http _upgrade ;   " Upgrade ";  }}
Front-end configuration:
$(function() {socket.init ();});//Nginx Reverse proxy implementation WebSocketvarBasePath = "ws://ws.52itstyle.com//acts_competition/"; socket={WebSocket: "",Init:function() { if(' WebSocket 'in window) {WebSocket=NewWebSocket (basepath+ ' Websocketserver '); } Else if(' Mozwebsocket 'in window) {WebSocket=NewMozwebsocket (basepath+ "Websocketserver"); } Else{WebSocket=NewSockjs (basepath+ "Sockjs/websocketserver"); } webSocket. onerror =function(event) {//alert ("Websockt connection error, please refresh page retry!") }; WebSocket. OnOpen =function(event) {}; WebSocket. OnMessage =function(event) {};},SendData:function(data) {WebSocket.send (data);},}

Finally, restart the nginx.

Challenges faced by reverse proxy servers in support of WebSocket

    • WebSocket is end-to-end, so when a proxy server intercepts a upgrade request from the client, it needs to send its own upgrade request to the backend server, also including the appropriate header.
    • Because WebSocket is a long connection and not a typical short connection like HTTP, the reverse proxy server needs to allow the connection to remain open rather than shutting them down when they appear to be idle.

Summarize

The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring certain help, if there are questions you can message exchange, thank you for the script home support.

Original link: http://blog.52itstyle.com/archives/736/

The configuration example of the reverse proxy websocket in Nginx combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.