Skip to content

Commit b012ef7

Browse files
authored
[feature] Added support for non-default Nginx external ports #496
E.g. for installations running behind a reverse proxy. Closes #496
1 parent 71b25ba commit b012ef7

File tree

7 files changed

+58
-12
lines changed

7 files changed

+58
-12
lines changed

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ services:
131131
- dashboard.internal
132132
- api.internal
133133
ports:
134-
- "80:80"
135-
- "443:443"
134+
- "${NGINX_PORT:-80}:80"
135+
- "${NGINX_SSL_PORT:-443}:443"
136136
depends_on:
137137
- dashboard
138138
- api

docs/user/settings.rst

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ properly on your system.
8383
``SSL_CERT_MODE``
8484
~~~~~~~~~~~~~~~~~
8585

86-
- **Explanation:** Flag to enable or disable HTTPs. If it is set to
86+
- **Explanation:** Flag to enable or disable HTTPS. If it is set to
8787
``Yes``, letsencrypt certificates are automatically fetched with the
8888
help of certbot and a cronjob to ensure they stay updated is added. If
89-
it is set to ``SelfSigned``, self-signed certificates are used and
90-
cronjob for the certificates is set. If set to ``No``, site is
91-
accessible via HTTP, if set if ``EXTERNAL``, it tells HTTPs is used but
92-
managed by external tool like loadbalancer / provider. Setting this
89+
it is set to ``SelfSigned``, self-signed certificates are used and a
90+
cronjob for the certificates is set. If set to ``No``, the site is
91+
accessible via HTTP. If set to ``External``, it tells HTTPS is used but
92+
managed by an external tool like a loadbalancer/provider. Setting this
9393
option as ``No`` is not recommended and might break some features, only
9494
do it when you know what you are doing.
9595
- **Valid Values:** ``External``, ``Yes``, ``SelfSigned``, ``No``.
@@ -869,6 +869,26 @@ Nginx
869869
- **Example:** ``index index.html index.htm;``.
870870
- **Default:** ``""`` (empty string).
871871

872+
``NGINX_SSL_PORT``
873+
~~~~~~~~~~~~~~~~~~
874+
875+
- **Explanation:** Nginx container external HTTPS port. Change if, for
876+
example, OpenWISP runs behind a reverse proxy listening on port 443 on
877+
the same host. Non-default ports are incompatible with
878+
``SSL_CERT_MODE=Yes``.
879+
- **Valid Values:** ``INTEGER``.
880+
- **Default:** ``443``.
881+
882+
``NGINX_PORT``
883+
~~~~~~~~~~~~~~
884+
885+
- **Explanation:** Nginx container external HTTP port. Change if, for
886+
example, OpenWISP runs behind a reverse proxy listening on port 80 on
887+
the same host. Non-default ports are incompatible with
888+
``SSL_CERT_MODE=Yes``.
889+
- **Valid Values:** ``INTEGER``.
890+
- **Default:** ``80``.
891+
872892
``NGINX_GZIP_SWITCH``
873893
~~~~~~~~~~~~~~~~~~~~~
874894

images/common/openwisp/settings.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,25 @@
5050
ACCOUNT_LOGOUT_REDIRECT_URL = LOGIN_REDIRECT_URL
5151
ROOT_URLCONF = "openwisp.urls"
5252
HTTP_SCHEME = request_scheme()
53+
HTTP_PORT = (
54+
os.getenv("NGINX_SSL_PORT", "443")
55+
if HTTP_SCHEME == "https"
56+
else os.getenv("NGINX_PORT", "80")
57+
)
58+
if (
59+
HTTP_SCHEME == "http"
60+
and HTTP_PORT == "80"
61+
or HTTP_SCHEME == "https"
62+
and (HTTP_PORT == "443" or os.environ["SSL_CERT_MODE"].lower() == "external")
63+
):
64+
HTTP_PORT = ""
65+
else:
66+
HTTP_PORT = f":{HTTP_PORT}"
5367

5468
# CORS
5569
CORS_ALLOWED_ORIGINS = [
56-
f'{HTTP_SCHEME}://{os.environ["DASHBOARD_DOMAIN"]}',
57-
f'{HTTP_SCHEME}://{os.environ["API_DOMAIN"]}',
70+
f'{HTTP_SCHEME}://{os.environ["DASHBOARD_DOMAIN"]}{HTTP_PORT}',
71+
f'{HTTP_SCHEME}://{os.environ["API_DOMAIN"]}{HTTP_PORT}',
5872
] + os.environ["DJANGO_CORS_HOSTS"].split(",")
5973
CORS_ALLOW_CREDENTIALS = True
6074

images/openwisp_dashboard/module_settings.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,18 @@
8989
# pregenerate static gzip files to save CPU
9090
GZIP_STATIC_COMPRESSION = True
9191

92-
API_BASEURL = f'{request_scheme()}://{os.environ["API_DOMAIN"]}'
92+
HTTP_SCHEME = request_scheme()
93+
HTTP_PORT = (
94+
os.getenv("NGINX_SSL_PORT", "443")
95+
if HTTP_SCHEME == "https"
96+
else os.getenv("NGINX_PORT", "80")
97+
)
98+
HTTP_PORT = (
99+
""
100+
if HTTP_SCHEME == "https" and os.environ["SSL_CERT_MODE"].lower() == "external"
101+
else f":{HTTP_PORT}"
102+
)
103+
API_BASEURL = f'{HTTP_SCHEME}://{os.environ["API_DOMAIN"]}{HTTP_PORT}'
93104

94105
OPENWISP_NETWORK_TOPOLOGY_API_URLCONF = "openwisp_network_topology.urls"
95106
OPENWISP_MONITORING_API_URLCONF = "openwisp_monitoring.urls"

images/openwisp_nginx/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ ENV MODULE_NAME=nginx \
4444
NGINX_GZIP_TYPES='text/plain image/svg+xml application/json application/javascript text/xml text/css application/xml application/x-font-ttf font/opentype' \
4545
NGINX_CUSTOM_FILE=False \
4646
NINGX_REAL_REMOTE_ADDR='$real_ip' \
47+
NGINX_SSL_PORT=443 \
4748
# USWGI pass_port
4849
DASHBOARD_APP_PORT=8000 \
4950
API_APP_PORT=8001 \

images/openwisp_nginx/openwisp.ssl.80.template.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ server {
99
location /.well-known/ {
1010
try_files ${DOLLAR}uri /dev/null =404;
1111
}
12-
return 301 https://${DOLLAR}host${DOLLAR}request_uri;
12+
return 301 https://${DOLLAR}host:${NGINX_SSL_PORT}${DOLLAR}request_uri;
1313
}

images/openwisp_nginx/openwisp.ssl.template.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ server {
2424
add_header Referrer-Policy "same-site" always;
2525
add_header Permissions-Policy "interest-cohort=()" always;
2626
add_header Strict-Transport-Security "max-age=31536000" always;
27-
add_header Content-Security-Policy "default-src http: https: data: blob: 'unsafe-inline'; script-src 'unsafe-eval' https: 'unsafe-inline' 'self'; frame-ancestors 'self'; connect-src *.${ROOT_DOMAIN} wss: 'self'; worker-src https://${DOMAIN} blob: 'self';" always;
27+
add_header Content-Security-Policy "default-src http: https: data: blob: 'unsafe-inline'; script-src 'unsafe-eval' https: 'unsafe-inline' 'self'; frame-ancestors 'self'; connect-src *.${ROOT_DOMAIN}:${NGINX_SSL_PORT} wss: 'self'; worker-src https://${DOMAIN} blob: 'self';" always;
2828

2929
# GZIP Configurations
3030
gzip ${NGINX_GZIP_SWITCH};

0 commit comments

Comments
 (0)