Skip to content

Commit f367046

Browse files
committed
set custom port trough env variables. sometimes user may need to use host network where some other services may already use 80,443,81 ports
1 parent 7747db9 commit f367046

File tree

6 files changed

+91
-11
lines changed

6 files changed

+91
-11
lines changed

backend/internal/nginx.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ const internalNginx = {
240240

241241
// Set the IPv6 setting for the host
242242
host.ipv6 = internalNginx.ipv6Enabled();
243+
host.http_port = internalNginx.getHttpPort();
244+
host.https_port = internalNginx.getHttpsPort();
243245

244246
locationsPromise.then(() => {
245247
renderEngine
@@ -285,6 +287,7 @@ const internalNginx = {
285287
}
286288

287289
certificate.ipv6 = internalNginx.ipv6Enabled();
290+
certificate.http_port = internalNginx.getHttpPort();
288291

289292
renderEngine
290293
.parseAndRender(template, certificate)
@@ -432,6 +435,27 @@ const internalNginx = {
432435

433436
return true;
434437
},
438+
439+
/**
440+
* @returns {number}
441+
*/
442+
getHttpPort: () => {
443+
return Number.parseInt(process.env.HTTP_PORT, 10) || 80;
444+
},
445+
446+
/**
447+
* @returns {number}
448+
*/
449+
getHttpsPort: () => {
450+
return Number.parseInt(process.env.HTTPS_PORT, 10) || 443;
451+
},
452+
453+
/**
454+
* @returns {number}
455+
*/
456+
getWebUiPort: () => {
457+
return Number.parseInt(process.env.WEB_UI_PORT, 10) || 81;
458+
},
435459
};
436460

437461
export default internalNginx;

backend/templates/_listen.conf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
listen 80;
1+
listen {{ http_port }};
22
{% if ipv6 -%}
3-
listen [::]:80;
3+
listen [::]:{{ http_port }};
44
{% else -%}
5-
#listen [::]:80;
5+
#listen [::]:{{ http_port }};
66
{% endif %}
77
{% if certificate -%}
8-
listen 443 ssl;
8+
listen {{ https_port }} ssl;
99
{% if ipv6 -%}
10-
listen [::]:443 ssl;
10+
listen [::]:{{ https_port }} ssl;
1111
{% else -%}
12-
#listen [::]:443;
12+
#listen [::]:{{ https_port }};
1313
{% endif %}
1414
{% endif %}
1515
server_name {{ domain_names | join: " " }};

backend/templates/default.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# Skipping output, congratulations page configration is baked in.
66
{%- else %}
77
server {
8-
listen 80 default;
8+
listen {{ http_port }} default;
99
{% if ipv6 -%}
10-
listen [::]:80 default;
10+
listen [::]:{{ http_port }} default;
1111
{% else -%}
12-
#listen [::]:80 default;
12+
#listen [::]:{{ http_port }} default;
1313
{% endif %}
1414
server_name default-host.localhost;
1515
access_log /data/logs/default-host_access.log combined;

backend/templates/letsencrypt-request.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{% include "_header_comment.conf" %}
22

33
server {
4-
listen 80;
4+
listen {{ http_port }};
55
{% if ipv6 -%}
6-
listen [::]:80;
6+
listen [::]:{{ http_port }};
77
{% endif %}
88

99
server_name {{ domain_names | join: " " }};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/command/with-contenv bash
2+
# shellcheck shell=bash
3+
4+
set -e
5+
6+
log_info 'Ports ...'
7+
8+
# Default ports
9+
HTTP_PORT=${HTTP_PORT:-80}
10+
HTTPS_PORT=${HTTPS_PORT:-443}
11+
WEB_UI_PORT=${WEB_UI_PORT:-81}
12+
13+
process_folder () {
14+
FILES=$(find "$1" -type f -name "*.conf")
15+
16+
for FILE in $FILES
17+
do
18+
echo "- ${FILE}"
19+
# Replace HTTP port
20+
sed -i -E "s/listen 80([^0-9]|$)/listen ${HTTP_PORT}\1/g" "$FILE"
21+
sed -i -E "s/listen \[::\]:80([^0-9]|$)/listen [::]:${HTTP_PORT}\1/g" "$FILE"
22+
sed -i -E "s/set \$port \"80\"/set \$port \"${HTTP_PORT}\"/g" "$FILE"
23+
24+
# Replace HTTPS port
25+
sed -i -E "s/listen 443([^0-9]|$)/listen ${HTTPS_PORT}\1/g" "$FILE"
26+
sed -i -E "s/listen \[::\]:443([^0-9]|$)/listen [::]:${HTTPS_PORT}\1/g" "$FILE"
27+
sed -i -E "s/set \$port \"443\"/set \$port \"${HTTPS_PORT}\"/g" "$FILE"
28+
29+
# Replace Web UI port
30+
sed -i -E "s/listen 81([^0-9]|$)/listen ${WEB_UI_PORT}\1/g" "$FILE"
31+
sed -i -E "s/listen \[::\]:81([^0-9]|$)/listen [::]:${WEB_UI_PORT}\1/g" "$FILE"
32+
done
33+
34+
# ensure the files are still owned by the npm user
35+
chown -R "$PUID:$PGID" "$1"
36+
}
37+
38+
process_folder /etc/nginx/conf.d
39+
process_folder /data/nginx

docs/src/advanced-config/index.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,21 @@ Setting these environment variables will create the default user on startup, ski
237237
environment:
238238
INITIAL_ADMIN_EMAIL: my@example.com
239239
INITIAL_ADMIN_PASSWORD: mypassword1
240+
241+
## Custom Ports
242+
243+
If you need to change the default ports that NPM listens on (80 for HTTP, 443 for HTTPS, and 81 for the Web UI), you can do so by setting environment variables. Note that you must also update your Docker port mappings to match these internal ports.
244+
245+
```yml
246+
ports:
247+
- '8080:8080' # Public HTTP Port
248+
- '8443:8443' # Public HTTPS Port
249+
- '8181:8181' # Admin Web Port
250+
environment:
251+
HTTP_PORT: 8080
252+
HTTPS_PORT: 8443
253+
WEB_UI_PORT: 8181
240254
```
255+
256+
> [!IMPORTANT]
257+
> When changing these environment variables, the internal Nginx configuration will be updated to listen on these new ports. Your Docker `ports` mapping must use these new ports as the container-side port (the second number).

0 commit comments

Comments
 (0)