Skip to content

Commit a44fa7c

Browse files
committed
make nginx use keep alive to upstreams
1 parent 9e7edbe commit a44fa7c

13 files changed

Lines changed: 522 additions & 407 deletions

File tree

.github/workflows/lint-and-format.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- name: nginxbeautifier
3838
run: |
3939
pnpm add -g nginxbeautifier
40-
nginxbeautifier -s 4 -r rootfs/usr/local/nginx/conf
40+
nginxbeautifier -s 2 -r rootfs/usr/local/nginx/conf
4141
- name: push changes
4242
run: |
4343
git add -A

backend/internal/nginx.js

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ const internalNginx = {
132132
const renderEngine = utils.getRenderEngine();
133133
let renderedLocations = "";
134134

135-
for (const location of host.locations) {
135+
for (const [idx, location] of (host.locations || []).entries()) {
136136
if (location.npmplus_enabled === false) {
137137
continue;
138138
}
139139

140140
if (
141+
location.forward_host &&
141142
location.forward_host.indexOf("/") > -1 &&
142143
!location.forward_host.startsWith("/") &&
143144
!location.forward_host.startsWith("unix")
@@ -148,12 +149,75 @@ const internalNginx = {
148149
location.forward_path = `/${split.join("/")}`;
149150
}
150151

152+
location.forward_upstream_name = `upstream_${host.id}_location_${idx}`;
151153
renderedLocations += await renderEngine.parseAndRender(template, location);
152154
}
153155

154156
return renderedLocations;
155157
},
156158

159+
/**
160+
* Generates upstream blocks for main host and custom locations
161+
* @param {Object} host
162+
* @returns {Promise}
163+
*/
164+
renderUpstreams: async (host) => {
165+
let template;
166+
167+
try {
168+
template = await readFile(`${__dirname}/../templates/_upstream.conf`, {
169+
encoding: "utf8",
170+
});
171+
} catch (err) {
172+
throw new errs.ConfigurationError(err.message);
173+
}
174+
175+
const renderEngine = utils.getRenderEngine();
176+
let renderedUpstreams = "";
177+
178+
if (["http", "https", "grpc", "grpcs"].includes(host.forward_scheme)) {
179+
if (
180+
host.forward_host &&
181+
host.forward_host.indexOf("/") > -1 &&
182+
!host.forward_host.startsWith("/") &&
183+
!host.forward_host.startsWith("unix")
184+
) {
185+
const split = host.forward_host.split("/");
186+
host.forward_host = split.shift();
187+
host.forward_path = `/${split.join("/")}`;
188+
}
189+
190+
host.forward_upstream_name = `upstream_${host.id}`;
191+
renderedUpstreams += await renderEngine.parseAndRender(template, host);
192+
}
193+
194+
for (const [idx, location] of (host.locations || []).entries()) {
195+
if (location.npmplus_enabled === false) {
196+
continue;
197+
}
198+
199+
if (!["http", "https", "grpc", "grpcs"].includes(location.forward_scheme)) {
200+
continue;
201+
}
202+
203+
if (
204+
location.forward_host &&
205+
location.forward_host.indexOf("/") > -1 &&
206+
!location.forward_host.startsWith("/") &&
207+
!location.forward_host.startsWith("unix")
208+
) {
209+
const split = location.forward_host.split("/");
210+
location.forward_host = split.shift();
211+
location.forward_path = `/${split.join("/")}`;
212+
}
213+
214+
location.forward_upstream_name = `upstream_${host.id}_location_${idx}`;
215+
renderedUpstreams += await renderEngine.parseAndRender(template, location);
216+
}
217+
218+
return renderedUpstreams;
219+
},
220+
157221
/**
158222
* @param {String} host_type
159223
* @param {Object} host
@@ -175,14 +239,25 @@ const internalNginx = {
175239
throw new errs.ConfigurationError(err.message);
176240
}
177241

178-
// For redirection hosts, if the scheme is not http or https, set it to $scheme
242+
host.env = process.env;
243+
179244
if (
180-
nice_host_type === "redirection_host" &&
181-
["http", "https"].indexOf(host.forward_scheme.toLowerCase()) === -1
245+
host.forward_host &&
246+
host.forward_host.indexOf("/") > -1 &&
247+
!host.forward_host.startsWith("/") &&
248+
!host.forward_host.startsWith("unix")
182249
) {
183-
host.forward_scheme = "$scheme";
250+
const split = host.forward_host.split("/");
251+
host.forward_host = split.shift();
252+
host.forward_path = `/${split.join("/")}`;
184253
}
185254

255+
if (host.domain_names) {
256+
host.server_names = host.domain_names.map((domain_name) => domainToASCII(domain_name) || domain_name);
257+
}
258+
259+
host.upstreams = await internalNginx.renderUpstreams(host);
260+
186261
if (host.locations) {
187262
_.map(host.locations, (location) => {
188263
if (location.npmplus_auth_request === "anubis") {
@@ -208,32 +283,14 @@ const internalNginx = {
208283
host.locations = await internalNginx.renderLocations(host);
209284
}
210285

211-
if (
212-
host.forward_host &&
213-
host.forward_host.indexOf("/") > -1 &&
214-
!host.forward_host.startsWith("/") &&
215-
!host.forward_host.startsWith("unix")
216-
) {
217-
const split = host.forward_host.split("/");
218-
219-
host.forward_host = split.shift();
220-
host.forward_path = `/${split.join("/")}`;
221-
}
222-
223-
if (host.domain_names) {
224-
host.server_names = host.domain_names.map((domain_name) => domainToASCII(domain_name) || domain_name);
225-
}
226-
227-
host.env = process.env;
228-
229286
try {
230287
const config_text = await renderEngine.parseAndRender(template, host);
231288

232289
await writeFile(filename, config_text, { encoding: "utf8" });
233290
debug(logger, "Wrote config:", filename);
234291

235292
if (process.env.DISABLE_NGINX_BEAUTIFIER === "false") {
236-
await utils.execFile("nginxbeautifier", ["-s", "4", filename]).catch(() => {});
293+
await utils.execFile("nginxbeautifier", ["-s", "2", filename]).catch(() => {});
237294
}
238295

239296
return true;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { migrate as logger } from "../logger.js";
2+
3+
const migrateName = "revert_redirect_auto_scheme";
4+
5+
/**
6+
* Migrate
7+
*
8+
* @see https://knexjs.org/guide/migrations.html#migration-api
9+
*
10+
* @param {Object} knex
11+
* @returns {Promise}
12+
*/
13+
const up = (knex) => {
14+
logger.info(`[${migrateName}] Migrating Up...`);
15+
16+
return knex.schema
17+
.table("redirection_host", async (table) => {
18+
// change the column default from auto to $scheme
19+
await table.string("forward_scheme").notNull().defaultTo("$scheme").alter();
20+
await knex("redirection_host").where("forward_scheme", "auto").update({ forward_scheme: "$scheme" });
21+
})
22+
.then(() => {
23+
logger.info(`[${migrateName}] redirection_host Table altered`);
24+
});
25+
};
26+
27+
/**
28+
* Undo Migrate
29+
*
30+
* @param {Object} knex
31+
* @returns {Promise}
32+
*/
33+
const down = (knex) => {
34+
logger.info(`[${migrateName}] Migrating Down...`);
35+
36+
return knex.schema
37+
.table("redirection_host", async (table) => {
38+
await table.string("forward_scheme").notNull().defaultTo("auto").alter();
39+
await knex("redirection_host").where("forward_scheme", "$scheme").update({ forward_scheme: "auto" });
40+
})
41+
.then(() => {
42+
logger.info(`[${migrateName}] redirection_host Table altered`);
43+
});
44+
};
45+
46+
export { up, down };

backend/templates/_proxy_host_custom_location.conf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@ location {{ location_type }}{{ path }} {
1717

1818
{% if forward_scheme == "http" or forward_scheme == "https" %}
1919

20-
set $upstream {{ forward_host }};
2120
include proxy-headers.conf;
2221
{% if npmplus_upstream_compression != true %}proxy_set_header Accept-Encoding "";{% endif %}
2322
{% if npmplus_proxy_request_buffering == true and npmplus_crowdsec_appsec == true %}proxy_request_buffering off;{% endif %}
2423
{% if npmplus_proxy_response_buffering == true %}proxy_buffering off;{% endif %}
25-
proxy_pass {{ forward_scheme }}://{% if forward_path != null %}{{ forward_host }}{% else %}$upstream{% endif %}{% if forward_port != null %}:{{ forward_port }}{% endif %}{% if forward_path != null %}{{ forward_path }}{% else %}$request_uri{% endif %};
24+
proxy_pass {{ forward_scheme }}://{{ forward_upstream_name }}{% if forward_path != null %}{{ forward_path }}{% else %}$request_uri{% endif %};
2625

2726
{% elsif forward_scheme == "grpc" or forward_scheme == "grpcs" %}
2827

29-
set $upstream {{ forward_host }};
3028
include grpc-headers.conf;
3129
{% if npmplus_upstream_compression != true %}grpc_set_header Accept-Encoding "";{% endif %}
32-
grpc_pass {{ forward_scheme }}://{% if forward_path != null %}{{ forward_host }}{% else %}$upstream{% endif %}{% if forward_port != null %}:{{ forward_port }}{% endif %}{% if forward_path != null %}{{ forward_path }}{% else %}$request_uri{% endif %};
30+
grpc_pass {{ forward_scheme }}://{{ forward_upstream_name }}{% if forward_path != null %}{{ forward_path }}{% else %}$request_uri{% endif %};
3331

3432
{% elsif forward_scheme == "path" %}
3533

backend/templates/_upstream.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
upstream {{ forward_upstream_name }} {
2+
zone upstream_{{ id }} 64k;
3+
server {{ forward_host }}{% if forward_port != null %}:{{ forward_port }}{% endif %} resolve;
4+
}

backend/templates/proxy_host.conf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
{% assign forward_host_last_char = forward_host | slice: -1 -%}
66

77
{% if enabled %}
8+
{{ upstreams }}
9+
810
server {
911
{% include "_common.conf" %}
1012

@@ -47,19 +49,17 @@
4749

4850
{% if forward_scheme == "http" or forward_scheme == "https" %}
4951

50-
set $upstream {{ forward_host }};
5152
include proxy-headers.conf;
5253
{% if npmplus_upstream_compression != true %}proxy_set_header Accept-Encoding "";{% endif %}
5354
{% if npmplus_proxy_request_buffering == true and npmplus_crowdsec_appsec == true %}proxy_request_buffering off;{% endif %}
5455
{% if npmplus_proxy_response_buffering == true %}proxy_buffering off;{% endif %}
55-
proxy_pass {{ forward_scheme }}://{% if forward_path != null %}{{ forward_host }}{% else %}$upstream{% endif %}{% if forward_port != null %}:{{ forward_port }}{% endif %}{% if forward_path != null %}{{ forward_path }}{% else %}$request_uri{% endif %};
56+
proxy_pass {{ forward_scheme }}://{{ forward_upstream_name }}{% if forward_path != null %}{{ forward_path }}{% else %}$request_uri{% endif %};
5657

5758
{% elsif forward_scheme == "grpc" or forward_scheme == "grpcs" %}
5859

59-
set $upstream {{ forward_host }};
6060
include grpc-headers.conf;
6161
{% if npmplus_upstream_compression != true %}grpc_set_header Accept-Encoding "";{% endif %}
62-
grpc_pass {{ forward_scheme }}://{% if forward_path != null %}{{ forward_host }}{% else %}$upstream{% endif %}{% if forward_port != null %}:{{ forward_port }}{% endif %}{% if forward_path != null %}{{ forward_path }}{% else %}$request_uri{% endif %};
62+
grpc_pass {{ forward_scheme }}://{{ forward_upstream_name }}{% if forward_path != null %}{{ forward_path }}{% else %}$request_uri{% endif %};
6363

6464
{% elsif forward_scheme == "path" %}
6565

backend/templates/stream.conf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
# --------------------------------------------------------------------------
55

66
{% if enabled %}
7+
8+
upstream upstream_{{ id }} {
9+
zone upstream_{{ id }} 64k;
10+
server {{ forwarding_host }}{% if forwarding_port != "" %}:{{ forwarding_port }}{% endif %} resolve;
11+
}
12+
713
{% if tcp_forwarding %}
814
server {
915
listen {{ env.IPV4_BINDING }}:{{ incoming_port }}{% if certificate and certificate_id > 0 %} ssl{% endif %} reuseport deferred so_keepalive=on;
@@ -42,8 +48,7 @@
4248
{% endif %}
4349
{% endif %}
4450

45-
set $upstream {{ forwarding_host }};
46-
proxy_pass $upstream{% if forwarding_port != "" %}:{{ forwarding_port }}{% endif %};
51+
proxy_pass upstream_{{ id }};
4752

4853
{{ npmplus_advanced_config }}
4954

@@ -58,8 +63,7 @@
5863
listen {{ env.IPV4_BINDING }}:{{ incoming_port }} udp reuseport;
5964
{% if env.DISABLE_IPV6 == "false" %}listen {{ env.IPV6_BINDING }}:{{ incoming_port }} udp reuseport;{% endif %}
6065

61-
set $upstream {{ forwarding_host }};
62-
proxy_pass $upstream{% if forwarding_port != "" %}:{{ forwarding_port }}{% endif %};
66+
proxy_pass upstream_{{ id }};
6367

6468
{{ npmplus_advanced_config }}
6569

frontend/src/modals/RedirectionHostModal.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ const RedirectionHostModal = EasyModal.create(({ id, visible, remove }: Props) =
176176
required
177177
{...field}
178178
>
179-
<option value="auto">
180-
<T id="auto" />
181-
</option>
179+
<option value="$scheme">keep</option>
182180
<option value="http">http</option>
183181
<option value="https">https</option>
184182
</select>

rootfs/usr/local/bin/envs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ if [ "$GOA" = "true" ] && [ "$LOGROTATE" = "false" ]; then
804804
fi
805805

806806

807-
export TV="12"
807+
export TV="13"
808808
if [ ! -s /data/npmplus/env.sha512sum ] || [ "$(cat /data/npmplus/env.sha512sum)" != "$( (grep "env\.[A-Z0-9_]\+" -roh /app/templates | sed "s|env.||g" | sort | uniq | xargs printenv; echo "$TV") | tr -d "\n" | sha512sum | cut -d" " -f1)" ]; then
809809
echo "At least one env or the template version changed, all hosts will be regenerated. Please make sure to read the changelog."
810810
export REGENERATE_ALL="true"

rootfs/usr/local/nginx/conf/conf.d/goaccess.conf.disabled

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ server {
66
return 444;
77
}
88

9+
upstream goaccess {
10+
server unix:/run/goaccess.sock;
11+
}
12+
913
server {
1014
listen 0.0.0.0:91 ssl bind reuseport deferred multipath so_keepalive=on default_server;
1115
listen [::]:91 ssl bind reuseport deferred multipath so_keepalive=on default_server;
@@ -38,7 +42,7 @@ server {
3842
include proxy-headers.conf;
3943
proxy_set_header Accept-Encoding "";
4044
if ($http_upgrade = "websocket") {
41-
proxy_pass http://unix:/run/goaccess.sock:$request_uri;
45+
proxy_pass http://goaccess$request_uri;
4246
}
4347

4448
root /tmp/goa;

0 commit comments

Comments
 (0)