The reverse proxy websocket configuration example of Nginx learning

Source: Internet
Author: User
Tags nginx reverse proxy

Written in the beginning of last year, did a contest scoring app. Specific needs, the same group of teachers can communicate with each other, timely notify the same group of personnel, the other team did those operations (of course, this is only for specific operations) written in the beginning of last year, did a contest scoring app. Specific needs, the same group of teachers can communicate with each other, timely notify the same group of personnel, the other team did those operations (of course, this is only for specific operations). Implementation of the current more mature WebSocket technology, WebSocket protocol for the creation of the client and the server side need real-time two-way communication WebApp provide a choice. 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 clusters in a real-world production environment, requiring multiple websocket servers 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 after 1.3 version of the support WebSocket reverse proxy, so to use the support WebSocket features, you must upgrade to 1.3 Future versions of Nginx support 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. code example: Upstream wsbackend {server 127.0.0.1:8080; server 127.0.0.1:8081;} server {listen; server_name ws.52itstyle.com; Location/{Proxy_pass http://wsbackend; proxy_http_version 1.1; Proxy_set_header Upgrade $http _upgrade; proxy_set_head ER Connection "Upgrade"; }} Front-end configuration: $ (function () {socket.init ();}); Nginx reverse Proxy implementation websocket VAR basEpath = "ws://ws.52itstyle.com//acts_competition/"; socket = {webSocket: "", Init:function () {if (' WebSocket ' in Windo W) {webSocket = new WebSocket (basepath+ ' websocketserver ');} else if (' mozwebsocket ' in window) {webSocket = new Mozwebs Ocket (basepath+ "Websocketserver"); } else {webSocket = new Sockjs (basepath+ "Sockjs/websocketserver"),} websocket.onerror = function (event) {//alert ("WEBSO Ckt connection error, please refresh the page retry! ")}; Websocket.onopen = function (event) {}; Websocket.onmessage = function (event) {}; }, Senddata:function (data) {http://www.dajiafa8.com websocket.send (data);}, and finally, restart the nginx. The challenge facing the reverse proxy server in supporting 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.

Nginx Learning Reverse proxy websocket Configuration instance

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.