-
Notifications
You must be signed in to change notification settings - Fork 3
/
nginx
128 lines (98 loc) · 2.59 KB
/
nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
const fs = require('node:fs');
const path = require('node:path');
function help() {
console.log(`
Usage:
nginx [options]
You have to be in sudoers group to use this
Options:
domain Domain name to expose
target Target hostname to expose to domain
certificate Certificate Location
privatekey Private key Location
Example:
nginx example.com https://127.0.0:3000 /etc/nginx/cert.crt /etc/nginx/key.pem
`);
}
function main() {
if (!process.env.SUDO_UID) return console.error('You must be a root user to run this script');
let args = process.argv;
if (args.length < 6) return help();
args = args.slice(2, 6);
const keys = ['domain', 'target', 'certificate', 'privateKey'];
args = args.reduce((a, v, i) => ({ ...a, [keys[i]]: v }), {});
const { domain, target, certificate, privateKey } = args;
const template = `
# This is an autogenerated template
# Modify at your own risk
# Author: Rakibul Yeasin <[email protected]>
# Github: https://github.com/dreygur
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server ${domain};
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate ${certificate};
ssl_certificate_key ${privateKey};
server_name www.${domain};
return 301 ${domain}$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name www.${domain};
return 301 https://${domain}$request_uri;
}
server {
listen 80;
listen [::]:80;
ssl_certificate ${certificate};
ssl_certificate_key ${privateKey};
server_name ${domain};
return 301 https://${domain}$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate ${certificate};
ssl_certificate_key ${privateKey};
access_log /var/log/nginx/access_log_${domain};
error_log /var/log/nginx/error_log_${domain};
keepalive_timeout 60;
server_name ${domain};
location / {
proxy_pass ${target};
}
location /api {
proxy_pass ${target}/api;
}
location /peerserver/peerjs {
proxy_pass ${target};
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}
}
`;
try {
fs.writeFileSync(path.join(`/etc/nginx/sites-available/${domain}`), template);
} catch (e) {
/* eslint-disable */
switch (e.code) {
case 'ENOENT':
console.error('Couldn\'t write site configuration file. NGINX is not available');
return;
default:
console.error('Unknow error');
}
/* eslint-enable */
}
}
(() => main())();