├── .gitignore ├── README.md ├── config.json ├── node-nginx.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Node template 3 | # Logs 4 | logs 5 | *.log 6 | npm-debug.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | 13 | # Directory for instrumented libs generated by jscoverage/JSCover 14 | lib-cov 15 | 16 | # Coverage directory used by tools like istanbul 17 | coverage 18 | 19 | # nyc test coverage 20 | .nyc_output 21 | 22 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 23 | .grunt 24 | 25 | # node-waf configuration 26 | .lock-wscript 27 | 28 | # Compiled binary addons (http://nodejs.org/api/addons.html) 29 | build/Release 30 | 31 | # Dependency directories 32 | node_modules 33 | jspm_packages 34 | 35 | # Optional npm cache directory 36 | .npm 37 | 38 | # Optional REPL history 39 | .node_repl_history 40 | 41 | # Intellij 42 | .idea 43 | 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #Node -Nginx 2 | ------------ 3 | **A simple command-line tool to create & manage ngninx proxy/virtualhost configuaration for node.js based app** 4 | **** 5 | 6 | ##Installation 7 | ``` 8 | $ git clone https://github.com/thesabbir/node-nginx.git node-nginx 9 | $ cd node-nginx 10 | $ npm install -d 11 | $ ln -s /path/to/node-nginx.js /usr/local/bin/nodenginx 12 | ``` 13 | 14 | `note : please edit your 'config.json' file according to your nginx.conf directory` 15 | 16 | 17 | 18 | ###Options 19 | ``` 20 | -h, --help output usage information 21 | -V, --version output the version number 22 | -n, --name [name] Specify a domain name / Server Name 23 | -p, --port [port] Port number where your node app is running 24 | -P, --sport [sport] Port where your sever will listening (Default 80) 25 | -e, --enabled List enabled sites 26 | -a, --available List available sites 27 | ``` 28 | 29 | ###Usage 30 | ``` 31 | $ nodenginx -n [domain] -p [port] -P [server-port] 32 | ``` 33 | 34 | ###Example 35 | ``` 36 | $ nodenginx -n example.com -p 8080 -P 80 37 | ``` 38 | 39 | 40 | ###For help 41 | ``` 42 | $ nodenginx --help 43 | 44 | ``` 45 | 46 | 47 | **TODO : Automated installation** -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "nginxPath" : "/usr/local/etc/nginx" 3 | 4 | } -------------------------------------------------------------------------------- /node-nginx.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var program = require('commander'), 4 | fs = require('fs-extra'), 5 | p = require('path'), 6 | eol = require('os').EOL, 7 | config = fs.readJsonSync('/Users/sabbir/Flow/Node/CLI/config.json'), 8 | available = p.join(config.nginxPath, 'sites-available'), 9 | enabled = p.join(config.nginxPath, 'sites-enabled'), 10 | filename; 11 | 12 | program 13 | .version('0.1.0') 14 | .usage(' -n [domain] -p [port] -P [server-port]') 15 | .option('-n, --name [name]', 'Specify a domain name / Server Name') 16 | .option('-p, --port [port]', 'Port number where your node app is running') 17 | .option('-P, --sport [sport]', 'Port where your sever will listening (Default 80)', 80) 18 | .option('-e, --enabled', "List enabled sites") 19 | .option('-a, --available', "List available sites") 20 | .on('--help', help) 21 | .parse(process.argv); 22 | 23 | var nignxConf = [ 24 | 'server {', 25 | ' listen {{sport}};', 26 | ' access_log /var/log/nginx/{{domain}}.access.log;', 27 | ' error_log /var/log/nginx/{{domain}}.error.log;', 28 | ' server_name {{domain}};', 29 | ' location / {', 30 | ' proxy_set_header X-Real-IP $remote_addr;', 31 | ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;', 32 | ' proxy_set_header Host $http_host;', 33 | ' proxy_pass http://127.0.0.1:{{port}};', 34 | ' proxy_redirect off;', 35 | ' proxy_http_version 1.1;', 36 | ' proxy_set_header Upgrade $http_upgrade;', 37 | ' proxy_set_header Connection $connection_upgrade;', 38 | ' }', 39 | '}' 40 | 41 | ].join(eol); 42 | 43 | if (program.enabled) status(enabled) 44 | if (program.available) status(available) 45 | 46 | if (program.name !== undefined & program.port !== undefined) { 47 | filename = program.name; 48 | nignxConf = nignxConf.replace(new RegExp('{{domain}}', 'ig'), program.name); 49 | nignxConf = nignxConf.replace(new RegExp('{{port}}', 'ig'), program.port); 50 | nignxConf = nignxConf.replace(new RegExp('{{sport}}', 'ig'), program.sport); 51 | main(filename, nignxConf); 52 | } 53 | 54 | function write (path, str) { 55 | fs.writeFile(path, str); 56 | console.log('Created SuccessFully'); 57 | program.confirm('Do you want to enable this site ?', function (dec) { 58 | if (dec) { 59 | enable(filename); 60 | } else { 61 | abort("Cancelled activation !") 62 | } 63 | }); 64 | } 65 | 66 | function enable (filename) { 67 | fs.mkdirs(available); 68 | var src = p.join(available, filename) + ".conf", 69 | des = p.join(enabled, filename); 70 | fs.symlink(src, des, function (err) { 71 | if (err) { 72 | if (err.code == "EEXIST") { 73 | console.log("Dude it's already enabled :p"); 74 | return process.exit(0); 75 | } else { 76 | console.log(err); 77 | return process.exit(1); 78 | } 79 | } else { 80 | console.log(filename + " enabled"); 81 | return process.exit(0); 82 | } 83 | }) 84 | } 85 | function abort (msg) { 86 | console.log(msg); 87 | return process.exit(1); 88 | } 89 | 90 | function status (dirname) { 91 | fs.readdir(dirname, function (err, files) { 92 | if (files != undefined) { 93 | console.log(files.join(eol).replace(new RegExp('.conf', 'ig'), '')); 94 | } 95 | }); 96 | } 97 | 98 | function help (add) { 99 | if (add != undefined) console.log(add); 100 | console.log(''); 101 | console.log("Usage Example :"); 102 | console.log(''); 103 | console.log("$ nodenginx -d example.com -p 3000"); 104 | console.log("$ nodenginx --help (for help)"); 105 | console.log(''); 106 | } 107 | 108 | function main (filename, str) { 109 | var path = p.join(available, filename) + ".conf"; 110 | fs.createFile(path, function (err) { 111 | if (err !== null) { 112 | console.log(err); 113 | } else { 114 | fs.readFile(path, function (err, content) { 115 | if (content.length > 0) { 116 | console.log("It contains something dude !"); 117 | program.confirm('Do you want to continue ?', function (dec) { 118 | if (dec) { 119 | write(path, str); 120 | } else { 121 | abort("Aborted"); 122 | } 123 | }); 124 | 125 | } else { 126 | write(path, str); 127 | } 128 | 129 | }); 130 | } 131 | }); 132 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-nginx", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "fs-extra" : "0.6.3", 7 | "commander" : "1.3.2" 8 | } 9 | } 10 | --------------------------------------------------------------------------------