Created
November 20, 2016 14:37
-
-
Save DmitryMyadzelets/85c162979fd563c6d72547dfb2285804 to your computer and use it in GitHub Desktop.
Routing nginx + nodejs\expressjs + socket.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Chat</title> | |
<script src="./socket.io/socket.io.js"></script> | |
<script type="text/javascript"> | |
var chat = io({path: "/chat/socket.io"}); | |
chat.on("chat message", function(data) { | |
var message = ''; | |
if (data.from) { | |
message += (data.from + ': '); | |
} | |
message += data.message; | |
document.getElementById("messages").innerHTML = ("<hr/>" + | |
message + document.getElementById("messages").innerHTML); | |
}); | |
function sendMessage() { | |
var msg = document.getElementById("message").value; | |
chat.emit("chat message", {message: msg}); | |
} | |
</script> | |
</head> | |
<body> | |
<h1>Chat</h1> | |
<input type="text" id="message"/> | |
<button onclick="sendMessage()">Send</button> | |
<div id="messages"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Chat server*/ | |
/*jslint browser: false*/ | |
'use strict'; | |
var express = require('express'); | |
var cookies = require('cookie-parser'); | |
var cookie = require('cookie'); | |
var app = express(); | |
var port = 8001; | |
app.use(cookies()); | |
var server = app.listen(port, function () { | |
console.log('Chat server listening on port', port); | |
}); | |
var chat = express.Router(); | |
app.use('/chat', chat); | |
chat.get('/', function (ignore, res, next) { | |
res.sendFile(__dirname + 'index.html', function (err) { | |
if (err) { | |
res.status(500); | |
next(err); | |
} | |
}); | |
}); | |
// ============================================================================ | |
var io = require('socket.io').listen(server, {path: '/chat/socket.io'}); | |
// Creates socket.cookies object | |
io.use(function (socket, next) { | |
try { | |
socket.cookies = cookie.parse(socket.request.headers.cookie); | |
} catch (ignore) { | |
socket.cookies = {}; | |
} | |
next(); | |
}); | |
// ============================================================================ | |
// Returns an object with a required structure, given a string | |
function wrap(s) { | |
return {message: s}; | |
} | |
function connected(socket) { | |
socket.join(spaceId); | |
// welcome message | |
socket.emit(messageId, wrap('you are ' + socket.client.id)); | |
// send to all except itself | |
socket.broadcast.emit(messageId, wrap('new user connected: ' + socket.client.id)); | |
} | |
io.on('connection', function (socket) { | |
console.log(socket.cookies, socket.id); | |
connected(socket); | |
socket.on('disconnect', function () { | |
io.emit(messageId, wrap('user disconnected: ' + socket.client.id)); | |
}); | |
socket.on(messageId, function (data) { | |
try { | |
data.from = socket.client.id; | |
// send to all including itself | |
io.emit(messageId, data); | |
// send message to everybody in the room | |
// io.sockets.in(spaceId).emit(messageId, data); | |
} catch (e) { | |
console.log(e); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nginx configuration file | |
# HTTP server | |
# Redirects all requests to HTTPS server | |
server { | |
location / { | |
return 301 https://$host$request_uri; | |
} | |
} | |
# HTTPS server | |
server { | |
listen 443; | |
ssl on; | |
ssl_certificate /etc/ssl/certs/your-domain.crt; | |
ssl_certificate_key /etc/ssl/private/your-domain.key; | |
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; | |
ssl_session_cache shared:SSL:10m; | |
ssl_session_timeout 10m; | |
ssl_prefer_server_ciphers on; | |
ssl_dhparam /etc/ssl/private/dhparams.pem; | |
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:ECDHE-RSA-AES128-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA128:DHE-RSA-AES128-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA128:ECDHE-RSA-AES128-SHA384:ECDHE-RSA-AES128-SHA128:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA384:AES128-GCM-SHA128:AES128-SHA128:AES128-SHA128:AES128-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4"; | |
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; | |
add_header X-Frame-Options DENY; | |
add_header X-Content-Type-Options nosniff; | |
root /srv/www/your-domain; | |
add_header Cache-control public; | |
expires 5m; | |
# First server | |
location / { | |
proxy_pass http://127.0.0.1:8000; | |
} | |
# Second server | |
location /chat { | |
proxy_pass http://127.0.0.1:8001; | |
# websocket support | |
proxy_http_version 1.1; | |
proxy_set_header Host $host; | |
proxy_set_header Connection 'upgrade'; | |
proxy_set_header Upgrade $http_upgrade; | |
} | |
# Locations for static content is better to define without regex, | |
# according to the author of nginx | |
# i.e except of: | |
# location ~ \.(gif|jpg|jpeg|png|js|woff|woff2|otf|ttf)$ | |
# do this: | |
location /js/ {} | |
location /css/ {} | |
location /img/ {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment