I. Source Code Installation
To use socket, you can only download the latest version from the official website and install it from the source code. (The package name under Ubuntu is recorded here, which is difficult to find)
1: Install libpcre3-dev,
2: OpenSSL library is not used. I have installed OpenSSL libssl-Dev. Later we found that we wanted to specify the source code path of OpenSSL.
./Configure -- prefix =/usr/local/nginx -- With-OpenSSL = ../openssl-1.0.0e -- with-http_ssl_module
3: configuration,After several days, I feel that there is no accurate configuration on the Internet.
My requirements are:
Nginx: 80 Static Page, apahce2: 8001 dynamic page, socket. IO: 8002 socket communication.
The result is that I access a page from nginx. I want to perform socket. Io communication on this page. Nginx then distinguishes dynamic, static pages and websocket, and forwards them to apache2 and socket. Io respectively.
The configuration is actually quite simple, but no correct method is provided online.
Add the following red content directly in nginx. conf
The socket. Io forwarding proxy is added here.
HTTP {********************* server {************** ******* omittedLocation/socket. IO/{proxy_pass http: // localhost: 8002; proxy_http_version 1.1; proxy_set_header upgrade $ http_upgrade; proxy_set_header connection "Upgrade"; proxy_set_header host $ host;}******************}}
Server listenerCodeServer. js
VaR IO = require ('socket. Io '). Listen (8002);Io. sockets. On ('Connection', Function (socket) {Io. sockets. emit ('Eas', {will: 'Be supported ed by everyone'}); Socket. On ('Message', Function (from, MSG) {console. Log ('I have Ed a private message by', from, 'saing', MSG) ;}); socket. On ('Disconnect', Function () {Io. sockets. emit ('User disconnected');});});
Customer Service index.html code
<HTML> <SCRIPT src = "/socket. IO/socket. Io. js"> </SCRIPT> <SCRIPT> $ (Function (){VaR iosocket = Io. Connect ();Iosocket. On ( 'Connect' , Function () {$ ( '# Incomingchatmessages'). append ($ ('<li> connected </LI>' ); Iosocket. On ( 'Message' , Function (Message) {$ ( '# Incomingchatmessages'). append ($ ('<li> </LI>'). Text (Message) ;}); iosocket. On ( 'Disconnect' , Function () {$ ( '# Incomingchatmessages'). append ('<li> disconnected </LI>' ) ;}); $ ( '# Outgoingchatmessage' ). Keypress (function (event ){ If (Event. Which = 13 ) {Event. preventdefault (); iosocket. Send ($ ( '# Outgoingchatmessage' ). Val (); $ ( '# Incomingchatmessages'). append ($ ('<li> </LI>'). Text ($ ('# outgoingchatmessage'). Val (); $ ( '# Outgoingchatmessage'). Val ('' );}});}); </SCRIPT> Incoming chat WWW Index 1: & nbsp; <ul id = "incomingchatmessages"> </ul> <br/> <input type = "text" id = "outgoingchatmessage"> </body>
4:
Location/socket. IO /{
In many places, this is written in various forms and won't be successful after many attempts. This is the only way to succeed.
5:
VaR IO = require ('socket. Io '). Listen (8002 );This port is the same as nginx in many places, or a new server is created here, and then listening is not what I want. I will not return any page, just process the socke message.
6:
<SCRIPT src = "/socket. IO/socket. Io. js"> </SCRIPT> <SCRIPT >$ (function () {var iosocket = Io. Connect ();
7. After nginx is modified, you can re-import the configuration file without restarting the service. nginx-s reload
In the future, detailed descriptions of socket. Io methods will be recorded and explained in detail during the work process.