-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx.js
More file actions
121 lines (101 loc) · 3.84 KB
/
nginx.js
File metadata and controls
121 lines (101 loc) · 3.84 KB
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
var fs = require('fs');
var ssl = {};
fs.readdirSync('./security').forEach(function(folder){
var stat = fs.statSync('./security/' + folder);
if (stat.isDirectory()){
ssl[folder] = {};
fs.readdirSync('./security/' + folder).forEach(function(file){
var stat = fs.statSync('./security/' + folder + '/' + file);
if (stat.isFile()){
if (file.indexOf('.key', this.length - 4) !== -1){
ssl[folder].key = '/security/' + folder + '/' + file;
} else if (file.indexOf('.crt', this.length - 4) !== -1) {
ssl[folder].crt = '/security/' + folder + '/' + file;
}
}
})
}
});
var router = "";
var prerouter = "";
fs.readdirSync('./sub').forEach(function(subdomain){
var ssldata;
for (regex in ssl){
var nregex = "^" + regex.replace(/\*/,'\\w+').replace(/\./,'\\.') + "$";
if ((new RegExp(nregex)).test(subdomain) && ssl[regex].crt && ssl[regex].key){
ssldata = ssl[regex];
break;
}
}
var config = require('./sub/' + subdomain + '/config.js');
if (ssldata){
router += "server {\n\tlisten 80;\n\tserver_name " + subdomain + ";\n\treturn 301 https://$server_name$request_uri;\n}\n";
router += "server {\n\tlisten 443 ssl spdy;\n\tserver_name " + subdomain + ";";
router += "\n\tssl_certificate " + __dirname + ssldata.crt +
";\n\tssl_certificate_key " + __dirname + ssldata.key + ";\n\tssl_protocols TLSv1 TLSv1.1 TLSv1.2;";
} else {
router += "server {\n\tlisten 80;\n\tserver_name " + subdomain + ";";
}
if (config.DYNAMIC){
if (!(config.DYNAMICURL instanceof Array)){
config.DYNAMICURL = [config.DYNAMICURL];
}
config.DYNAMICURL.forEach(function(dyn){
if (dyn instanceof RegExp){
router += "\n\tlocation ~ " + dyn.source + " {";
} else {
router += "\n\tlocation " + dyn + " {";
}
hosts = (config.DYNAMICHOST instanceof Array) ? config.DYNAMICHOST : [config.DYNAMICHOST];
hosts = hosts.map(function(e){return e.host + ':' + e.port})
prerouter += "upstream serv." + subdomain + " {";
prerouter += "\n\tleast_conn;\n\tserver ";
prerouter += hosts.join(';\n\tserver ') + ";\n}\n";
router += "\n\t\tproxy_pass http://serv." + subdomain + ";" +
"\n\t\tproxy_http_version 1.1;" +
"\n\t\tproxy_set_header Upgrade $http_upgrade;" +
"\n\t\tproxy_set_header Connection 'upgrade';" +
"\n\t\tproxy_set_header Host $host;" +
"\n\t\tproxy_set_header X-NginX-Proxy true;" +
(config.HIDDEN ? "\n\t\tproxy_set_header X-Robots-Tag 'noindex, nofollow';" : "") +
(config.ERR404 ? ("\n\t\terror_page 404 " + config.ERR404 + ";") : "") +
"\n\t\tproxy_cache_bypass $http_upgrade;\n\t}";
});
}
if (config.STATIC){
if (!(config.STATICURL instanceof Array)){
config.STATICURL = [config.STATICURL];
}
config.STATICURL.forEach(function(st,index){
if (st instanceof RegExp){
router += "\n\tlocation ~ " + st.source + " {";
} else {
router += "\n\tlocation " + st + " {";
}
var lifetime = config.STATICLIFETIME;
if (config.STATICLIFETIME instanceof Array){
lifetime = config.STATICLIFETIME[index];
}
var folder = config.STATICFOLDER;
if (config.STATICLIFETIME instanceof Array){
folder = config.STATICFOLDER[index];
}
router += (lifetime ? "\n\t\texpires " + JSON.stringify(lifetime) + ";" : "") +
"\n\t\troot " + __dirname + "/sub/" + subdomain + "/" + folder + ";" +
(config.ERR404 ? ("\n\t\terror_page 404 " + config.ERR404 + ";") : "");
var headers = config.STATICHEADERS;
if (config.STATICHEADERS instanceof Array){
headers = config.STATICHEADERS[index];
}
if (headers){
Object.keys(headers).forEach(function(header){
router += "\n\t\tadd_header " + JSON.stringify(header) + " " + JSON.stringify(headers[header]) + ";";
});
}
router += "\n\t\tadd_header Pragma public;" +
"\n\t\tadd_header Cache-Control \"public\";\n\t}";
});
}
router += "\n}\n";
});
console.log(prerouter + router);