Skip to content

Instantly share code, notes, and snippets.

@CWFranklin
Created February 9, 2022 11:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CWFranklin/e74471360ee420c102be75e1214f018c to your computer and use it in GitHub Desktop.
Save CWFranklin/e74471360ee420c102be75e1214f018c to your computer and use it in GitHub Desktop.
NGinx Example Setup
PORT=4100
var socket = io(window.location.origin);
/* View Buttons */
$('#waiting').click(function() {
socket.emit('changePage', 'waiting');
});
/* This is attached to a PUG Template */
window.socket = io(window.location.origin + '/stream');
window.socket.on('changePage', function(page) {
location.href = window.location.origin + '/stream/' + page;
});
upstream stream {
server 127.0.0.1:4100;
}
server {
server_name sub.domain.com;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/sub.domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/sub.domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
proxy_pass http://stream;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
add_header Front-End-Https on;
}
location /socket.io/ {
proxy_pass http://stream;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
if ($host = sub.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name sub.domain.com;
rewrite ^(.*) https://$host$1 permanent;
}
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
require('dotenv').config()
const http = require('http')
const https = require('https')
const express = require('express')
const app = express()
const httpServer = http.createServer(app)
const io = require('socket.io').listen(httpServer, { log: true })
const streamio = io.of('/stream')
app.use(express.static(__dirname + '/../public'))
app.set('view engine', 'pug')
app.set('views', __dirname + '/../public/views')
/* Routes */
app.get('/', function(req, res) {
res.render('index')
})
io.on('connection', (socket) => {
socket.emit('initData', { data: 'data' })
})
socket.on('changePage', (page) => {
currentPage = page
streamio.emit('changePage', page)
})
streamio.on('connection', (socket) => {
socket.emit('initData', { data: 'data' })
})
httpServer.listen(process.env.PORT)
console.log('Server started on port', process.env.PORT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment