Skip to content

Commit e645f93

Browse files
committed
[Nginx] Add env var for HTTP to HTTPS redirection
1 parent bbdec09 commit e645f93

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

data/Dockerfiles/nginx/bootstrap.py

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def prepare_template_vars():
5858
'RSPAMDHOST': os.getenv("RSPAMDHOST", "rspamd-mailcow"),
5959
'PHPFPMHOST': os.getenv("PHPFPMHOST", "php-fpm-mailcow"),
6060
'DISABLE_IPv6': os.getenv("DISABLE_IPv6", "n").lower() in ("y", "yes"),
61+
'HTTP_REDIRECT': os.getenv("HTTP_REDIRECT", "n").lower() in ("y", "yes"),
6162
}
6263

6364
ssl_dir = '/etc/ssl/mail/'

data/conf/nginx/templates/nginx.conf.j2

+40
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,42 @@ http {
4141
https https;
4242
}
4343

44+
{% if HTTP_REDIRECT %}
45+
# HTTP to HTTPS redirect
46+
server {
47+
root /web;
48+
listen {{ HTTP_PORT }} default_server;
49+
listen [::]:{{ HTTP_PORT }} default_server;
50+
51+
server_name {{ MAILCOW_HOSTNAME }} autodiscover.* autoconfig.* {{ ADDITIONAL_SERVER_NAMES | join(' ') }};
52+
53+
if ( $request_uri ~* "%0A|%0D" ) { return 403; }
54+
location ^~ /.well-known/acme-challenge/ {
55+
allow all;
56+
default_type "text/plain";
57+
}
58+
location / {
59+
return 301 https://$host$uri$is_args$args;
60+
}
61+
}
62+
{%endif%}
63+
4464
# Default Server Name
4565
server {
4666
listen 127.0.0.1:65510; # sogo-auth verify internal
67+
68+
{% if not HTTP_REDIRECT %}
4769
listen {{ HTTP_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%};
70+
{%endif%}
4871
listen {{ HTTPS_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%} ssl;
72+
4973
{% if not DISABLE_IPv6 %}
74+
{% if not HTTP_REDIRECT %}
5075
listen [::]:{{ HTTP_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%};
76+
{%endif%}
5177
listen [::]:{{ HTTPS_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%} ssl;
5278
{%endif%}
79+
5380
http2 on;
5481

5582
ssl_certificate /etc/ssl/mail/cert.pem;
@@ -64,12 +91,19 @@ http {
6491
{% for SERVER_NAME in ADDITIONAL_SERVER_NAMES %}
6592
server {
6693
listen 127.0.0.1:65510; # sogo-auth verify internal
94+
95+
{% if not HTTP_REDIRECT %}
6796
listen {{ HTTP_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%};
97+
{%endif%}
6898
listen {{ HTTPS_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%} ssl;
99+
69100
{% if not DISABLE_IPv6 %}
101+
{% if not HTTP_REDIRECT %}
70102
listen [::]:{{ HTTP_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%};
103+
{%endif%}
71104
listen [::]:{{ HTTPS_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%} ssl;
72105
{%endif%}
106+
73107
http2 on;
74108

75109
ssl_certificate /etc/ssl/mail/cert.pem;
@@ -127,12 +161,18 @@ http {
127161

128162
{% for cert in valid_cert_dirs %}
129163
server {
164+
{% if not HTTP_REDIRECT %}
130165
listen {{ HTTP_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%};
166+
{%endif%}
131167
listen {{ HTTPS_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%} ssl;
168+
132169
{% if not DISABLE_IPv6 %}
170+
{% if not HTTP_REDIRECT %}
133171
listen [::]:{{ HTTP_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%};
172+
{%endif%}
134173
listen [::]:{{ HTTPS_PORT }}{% if NGINX_USE_PROXY_PROTOCOL %} proxy_protocol{%endif%} ssl;
135174
{%endif%}
175+
136176
http2 on;
137177

138178
ssl_certificate {{ cert.cert_path }}cert.pem;

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ services:
385385
- SKIP_SOGO=${SKIP_SOGO:-n}
386386
- SKIP_RSPAMD=${SKIP_RSPAMD:-n}
387387
- DISABLE_IPv6=${DISABLE_IPv6:-n}
388+
- HTTP_REDIRECT=${HTTP_REDIRECT:-n}
388389
- PHPFPMHOST=${PHPFPMHOST:-}
389390
- SOGOHOST=${SOGOHOST:-}
390391
- RSPAMDHOST=${RSPAMDHOST:-}

generate_config.sh

+3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ HTTP_BIND=
267267
HTTPS_PORT=443
268268
HTTPS_BIND=
269269
270+
# Redirect HTTP connections to HTTPS - y/n
271+
HTTP_REDIRECT=n
272+
270273
# ------------------------------
271274
# Other bindings
272275
# ------------------------------

update.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ adapt_new_options() {
352352
"SPAMHAUS_DQS_KEY"
353353
"SKIP_UNBOUND_HEALTHCHECK"
354354
"DISABLE_NETFILTER_ISOLATION_RULE"
355+
"HTTP_REDIRECT"
355356
)
356357

357358
sed -i --follow-symlinks '$a\' mailcow.conf
@@ -637,7 +638,13 @@ adapt_new_options() {
637638
echo '# Prevent netfilter from setting an iptables/nftables rule to isolate the mailcow docker network - y/n' >> mailcow.conf
638639
echo '# CAUTION: Disabling this may expose container ports to other neighbors on the same subnet, even if the ports are bound to localhost' >> mailcow.conf
639640
echo 'DISABLE_NETFILTER_ISOLATION_RULE=n' >> mailcow.conf
640-
fi
641+
fi
642+
elif [[ ${option} == "HTTP_REDIRECT" ]]; then
643+
if ! grep -q ${option} mailcow.conf; then
644+
echo "Adding new option \"${option}\" to mailcow.conf"
645+
echo '# Redirect HTTP connections to HTTPS - y/n' >> mailcow.conf
646+
echo 'HTTP_REDIRECT=n' >> mailcow.conf
647+
fi
641648
elif ! grep -q ${option} mailcow.conf; then
642649
echo "Adding new option \"${option}\" to mailcow.conf"
643650
echo "${option}=n" >> mailcow.conf

0 commit comments

Comments
 (0)