-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNginxConfiguration.js
More file actions
189 lines (144 loc) · 4.95 KB
/
Copy pathNginxConfiguration.js
File metadata and controls
189 lines (144 loc) · 4.95 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const PORT_RANGE_START = 5000;
const PORT_RANGE_END = 6000;
async function isPortInUse(port) {
try {
const result = execSync(`netstat -tuln | grep ":${port}"`);
return result.toString().length > 0;
} catch (error) {
return false; // Port is free
}
}
async function findFreePort() {
try {
// Read the starting port value asynchronously
const data = fs.readFileSync('variable.txt', 'utf-8');
let port_start = parseInt(data.trim(), 10);
for (let port = port_start; port <= PORT_RANGE_END; port++) {
let response = await isPortInUse(port);
if (!response) {
// Write the new port back to variable.txt
fs.writeFileSync('variable.txt', port.toString());
return port; // Return the free port
}
}
} catch (err) {
console.error("Error reading or writing the file:", err);
}
}
async function configureNginxFrontEndStatic(id, port,deployDirectory) {
const domain = `${id}.server.ddks.live`;
const user=id;
const dir=deployDirectory;
const nginxConfig = `
server {
listen 80;
server_name ${domain};
# Redirect all HTTP requests to HTTPS
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name ${domain};
# SSL configuration
ssl_certificate /etc/letsencrypt/live/server.ddks.live/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/server.ddks.live/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
# Serve static files from /home/abc/build
root /home/${id}/${dir};
location / {
try_files $uri $uri/ =404;
}
location /index.html {
try_files $uri /index.html =404;
}
# Handle static assets (CSS, JS, etc.)
location /static/ {
try_files $uri =404;
}
# Optionally set some caching for static files (optional for performance)
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}
`;
const configPath = `/etc/nginx/sites-available/${domain}`;
fs.writeFileSync(configPath, nginxConfig);
// Create symbolic link to enable site
const enabledPath = `/etc/nginx/sites-enabled/${domain}`;
if (!fs.existsSync(enabledPath)) {
execSync(`ln -s ${configPath} ${enabledPath}`);
}
// Reload Nginx
execSync(`sudo setfacl -m u:www-data:rx /home/${user}`)
execSync('nginx -t && systemctl reload nginx');
return port;
}
async function configureNginxBackend(id, port) {
const domain = `${id}.server.ddks.live`;
const user=id;
// const dir=deployDirectory;
const nginxConfig = `
server {
listen 80;
server_name ${domain};
# Redirect all HTTP requests to HTTPS
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name ${domain};
# SSL configuration
ssl_certificate /etc/letsencrypt/live/server.ddks.live/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/server.ddks.live/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
# Reverse proxy to localhost:${port}
location / {
proxy_pass http://localhost:${port};
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;
}
# Serve static assets from the reverse-proxied application if needed
location /static/ {
proxy_pass http://localhost:${port};
expires 30d;
add_header Cache-Control "public, no-transform";
}
# Optionally set some caching for static files (optional for performance)
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}
`;
const configPath = `/etc/nginx/sites-available/${domain}`;
fs.writeFileSync(configPath, nginxConfig);
// Create symbolic link to enable site
const enabledPath = `/etc/nginx/sites-enabled/${domain}`;
if (!fs.existsSync(enabledPath)) {
execSync(`ln -s ${configPath} ${enabledPath}`);
}
// Reload Nginx
execSync(`sudo setfacl -m u:www-data:rx /home/${user}`)
execSync('nginx -t && systemctl reload nginx');
return port;
}
// Main function
async function reservePort(id,deployDirectory,type) {
const port =await findFreePort();
console.log(`Reserve Port In Final Depoy Triggered ${id} ${deployDirectory} ${type}`)
if(type===0) return (await configureNginxFrontEndStatic(id,port,deployDirectory));
else return (await configureNginxBackend(id,port))
}
module.exports = {reservePort,isPortInUse,findFreePort,configureNginxFrontEndStatic};