Skip to content

Instantly share code, notes, and snippets.

@ankitthakur
Last active June 2, 2018 06:17
Show Gist options
  • Save ankitthakur/7118633 to your computer and use it in GitHub Desktop.
Save ankitthakur/7118633 to your computer and use it in GitHub Desktop.
######################## nginx.conf ########################
include /Users/ankitthakur/nginx/sites-available/*;
######################## nginx.conf ########################
######################## node config ########################
# The upstream module is the link between Node.js and Nginx.
# Upstream is used for proxying requests to other servers.
# All requests for / get distributed between any of the servers listed.
upstream nodeserver {
# Set up multiple Node.js webservers for load balancing.
# max_fails refers to number of failed attempts
# before server is considered inactive.
# weight priorities traffic to server. Ex. weight=2 will recieve
# twice as much traffic as server with weight=1
server localhost:3000 max_fails=0 fail_timeout=10s weight=1;
server localhost:3001 max_fails=0 fail_timeout=10s weight=1;
server unix:/tmp/devNode;
# Send visitors back to the same server each time.
ip_hash;
# Enable number of keep-alive connections.
keepalive 512;
}
server {
listen 71;
listen [::]:71 default_server ipv6only=on;
# Domain names.
# Make sure to set the A Record on your domain's DNS settings to your server's IP address.
# You can test if was set properly by using the `dig` command: dig yourdomain.com
server_name devnode.com www.devnode.com;
access_log /var/log/nginx/access.log;
# GZIP Compression
gzip on;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# proxy to the node instance
location / {
# root www/nodeApps/myapp
# Set this to your upstream module.
proxy_pass http://nodeserver;
# Proxy headers.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
proxy_redirect off;
# Go to next upstream after if server down.
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_connect_timeout 5s;
# Gateway timeout.
proxy_read_timeout 20s;
proxy_send_timeout 20s;
# Buffer settings.
proxy_buffers 8 32k;
proxy_buffer_size 64k;
}
}
######################## node config ########################
######################## command line #######################
ln -nfs /Users/ankitthakur/nginx/sites-available/nodeServer.conf /Users/ankitthakur/nginx/sites-enabled/nodeServer.conf
######################## command line #######################
######################## reference #######################
Unix socket permissions depend on umask, so set umask before listen and reset it after listen succeeds (the callback parameter). The following code will do that.
var http = require("http");
var oldUmask = process.umask(0000);
var server = http.createServer(function(request, response) {
// ...
});
server.listen("/some/socket/path", function() {
process.umask(oldUmask);
});
If there is worry about what happens between the listen() succeeding and the callback being called, you can always set the permissions explicitly (not depending on umask) with a fs.chmodSync call when the listen succeeds.
######################## reference #######################
###################### node server code. #####################
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var fs = require('fs');
var mask = process.umask(0);
var socket = '/tmp/devNode';
if (fs.existsSync(socket)) {
fs.unlinkSync(socket);
}
var app = express();
// all environments
app.set('port', process.env.PORT || socket);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
if (mask) {
mask = null;
}
});
###################### node server code. #####################
@herberthobregon
Copy link

Excelent!! thanks!
this line is what I needed
var mask = process.umask(0);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment