Skip to content

Commit 31396f0

Browse files
authored
Merge pull request spantaleev#1295 from nogweii/feat-support-upstream-https-forwarded
Support trusting the upstream server when it says the protocol is HTTPS
2 parents 4f841a7 + 2578ca4 commit 31396f0

File tree

8 files changed

+29
-20
lines changed

8 files changed

+29
-20
lines changed

docs/configuring-playbook-own-webserver.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ matrix_nginx_proxy_container_federation_host_bind_port: '127.0.0.1:8449'
108108
# Since we don't obtain any certificates (`matrix_ssl_retrieval_method: none` above), it won't work by default.
109109
# An alternative is to tweak some of: `matrix_coturn_tls_enabled`, `matrix_coturn_tls_cert_path` and `matrix_coturn_tls_key_path`.
110110
matrix_coturn_enabled: false
111+
112+
# Trust the reverse proxy to send the correct `X-Forwarded-Proto` header as it is handling the SSL connection.
113+
matrix_nginx_proxy_trust_forwarded_proto: true
111114
```
112115
113116
With this, nginx would still be in use, but it would not bother with anything SSL related or with taking up public ports.

roles/matrix-etherpad/tasks/init.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- name: Generate Etherpad proxying configuration for matrix-nginx-proxy
1616
set_fact:
1717
matrix_etherpad_matrix_nginx_proxy_configuration: |
18-
rewrite ^{{ matrix_etherpad_public_endpoint }}$ $scheme://$server_name{{ matrix_etherpad_public_endpoint }}/ permanent;
18+
rewrite ^{{ matrix_etherpad_public_endpoint }}$ {{ matrix_nginx_proxy_x_forwarded_proto_value }}://$server_name{{ matrix_etherpad_public_endpoint }}/ permanent;
1919
2020
location {{ matrix_etherpad_public_endpoint }}/ {
2121
{% if matrix_nginx_proxy_enabled|default(False) %}
@@ -27,7 +27,7 @@
2727
proxy_http_version 1.1; # recommended with keepalive connections
2828
proxy_pass_header Server;
2929
proxy_set_header Host $host;
30-
proxy_set_header X-Forwarded-Proto $scheme; # for EP to set secure cookie flag when https is used
30+
proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }}; # for EP to set secure cookie flag when https is used
3131
# WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
3232
proxy_set_header Upgrade $http_upgrade;
3333
proxy_set_header Connection $connection_upgrade;

roles/matrix-nginx-proxy/defaults/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ matrix_nginx_proxy_container_extra_arguments: []
4040
# - services are served directly from the HTTP vhost
4141
matrix_nginx_proxy_https_enabled: true
4242

43+
# Controls whether matrix-nginx-proxy trusts an upstream server's X-Forwarded-Proto header
44+
#
45+
# Required if you disable HTTPS for the container (see `matrix_nginx_proxy_https_enabled`) and have an upstream server handle it instead.
46+
matrix_nginx_proxy_trust_forwarded_proto: false
47+
matrix_nginx_proxy_x_forwarded_proto_value: "{{ '$http_x_forwarded_proto' if matrix_nginx_proxy_trust_forwarded_proto else '$scheme' }}"
48+
4349
# Controls whether the matrix-nginx-proxy container exposes its HTTP port (tcp/8080 in the container).
4450
#
4551
# Takes an "<ip>:<port>" or "<port>" value (e.g. "127.0.0.1:80"), or empty string to not expose.

roles/matrix-nginx-proxy/templates/nginx/conf.d/matrix-domain.conf.j2

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
{% if matrix_nginx_proxy_floc_optout_enabled %}
2121
add_header Permissions-Policy interest-cohort=() always;
2222
{% endif %}
23-
23+
2424
{% if matrix_nginx_proxy_hsts_preload_enabled %}
2525
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
2626
{% else %}
2727
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
2828
{% endif %}
29-
29+
3030
add_header X-XSS-Protection "{{ matrix_nginx_proxy_xss_protection }}";
3131

3232
location /.well-known/matrix {
@@ -59,7 +59,7 @@
5959

6060
proxy_set_header Host $host;
6161
proxy_set_header X-Forwarded-For $remote_addr;
62-
proxy_set_header X-Forwarded-Proto $scheme;
62+
proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
6363
}
6464
{% endif %}
6565

@@ -77,7 +77,7 @@
7777

7878
proxy_set_header Host $host;
7979
proxy_set_header X-Forwarded-For $remote_addr;
80-
proxy_set_header X-Forwarded-Proto $scheme;
80+
proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
8181
}
8282
{% endif %}
8383

@@ -112,7 +112,7 @@
112112

113113
proxy_set_header Host $host;
114114
proxy_set_header X-Forwarded-For $remote_addr;
115-
proxy_set_header X-Forwarded-Proto $scheme;
115+
proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
116116
}
117117
{% endif %}
118118

@@ -137,7 +137,7 @@
137137

138138
proxy_set_header Host $host;
139139
proxy_set_header X-Forwarded-For $remote_addr;
140-
proxy_set_header X-Forwarded-Proto $scheme;
140+
proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
141141

142142
client_body_buffer_size 25M;
143143
client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_client_api_client_max_body_size_mb }}M;
@@ -152,7 +152,7 @@
152152
#}
153153
location ~* ^/$ {
154154
{% if matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain %}
155-
return 302 $scheme://{{ matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain }}$request_uri;
155+
return 302 {{ matrix_nginx_proxy_x_forwarded_proto_value }}://{{ matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain }}$request_uri;
156156
{% else %}
157157
rewrite ^/$ /_matrix/static/ last;
158158
{% endif %}
@@ -215,12 +215,12 @@ server {
215215
ssl_stapling_verify on;
216216
ssl_trusted_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/chain.pem;
217217
{% endif %}
218-
218+
219219
{% if matrix_nginx_proxy_ssl_session_tickets_off %}
220220
ssl_session_tickets off;
221221
{% endif %}
222222
ssl_session_cache {{ matrix_nginx_proxy_ssl_session_cache }};
223-
ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }};
223+
ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }};
224224

225225
{{ render_vhost_directives() }}
226226
}
@@ -262,7 +262,7 @@ server {
262262
ssl_stapling_verify on;
263263
ssl_trusted_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_trusted_certificate }};
264264
{% endif %}
265-
265+
266266
{% if matrix_nginx_proxy_ssl_session_tickets_off %}
267267
ssl_session_tickets off;
268268
{% endif %}
@@ -283,7 +283,7 @@ server {
283283

284284
proxy_set_header Host $host;
285285
proxy_set_header X-Forwarded-For $remote_addr;
286-
proxy_set_header X-Forwarded-Proto $scheme;
286+
proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
287287

288288
client_body_buffer_size 25M;
289289
client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_federation_api_client_max_body_size_mb }}M;

roles/matrix-nginx-proxy/templates/nginx/conf.d/matrix-jitsi.conf.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
proxy_set_header Connection "upgrade";
7272
proxy_set_header Upgrade $http_upgrade;
7373
proxy_set_header X-Forwarded-For $remote_addr;
74-
proxy_set_header X-Forwarded-Proto $scheme;
74+
proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
7575
tcp_nodelay on;
7676
}
7777
{% endmacro %}
@@ -128,7 +128,7 @@ server {
128128
ssl_stapling_verify on;
129129
ssl_trusted_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_jitsi_hostname }}/chain.pem;
130130
{% endif %}
131-
131+
132132
{% if matrix_nginx_proxy_ssl_session_tickets_off %}
133133
ssl_session_tickets off;
134134
{% endif %}

roles/matrix-nginx-proxy/templates/nginx/conf.d/matrix-sygnal.conf.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
proxy_set_header Host $host;
3131
proxy_set_header X-Forwarded-For $remote_addr;
32-
proxy_set_header X-Forwarded-Proto $scheme;
32+
proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
3333
}
3434
{% endmacro %}
3535

@@ -85,7 +85,7 @@ server {
8585
ssl_stapling_verify on;
8686
ssl_trusted_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_sygnal_hostname }}/chain.pem;
8787
{% endif %}
88-
88+
8989
{% if matrix_nginx_proxy_ssl_session_tickets_off %}
9090
ssl_session_tickets off;
9191
{% endif %}

roles/matrix-registration/tasks/init.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
- name: Generate matrix-registration proxying configuration for matrix-nginx-proxy
2323
set_fact:
2424
matrix_registration_matrix_nginx_proxy_configuration: |
25-
rewrite ^{{ matrix_registration_public_endpoint }}$ $scheme://$server_name{{ matrix_registration_public_endpoint }}/ permanent;
26-
rewrite ^{{ matrix_registration_public_endpoint }}/$ $scheme://$server_name{{ matrix_registration_public_endpoint }}/register redirect;
25+
rewrite ^{{ matrix_registration_public_endpoint }}$ {{ matrix_nginx_proxy_x_forwarded_proto_value }}://$server_name{{ matrix_registration_public_endpoint }}/ permanent;
26+
rewrite ^{{ matrix_registration_public_endpoint }}/$ {{ matrix_nginx_proxy_x_forwarded_proto_value }}://$server_name{{ matrix_registration_public_endpoint }}/register redirect;
2727
2828
location ~ ^{{ matrix_registration_public_endpoint }}/(.*) {
2929
{% if matrix_nginx_proxy_enabled|default(False) %}

roles/matrix-synapse-admin/tasks/init.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
- name: Generate Synapse Admin proxying configuration for matrix-nginx-proxy
2323
set_fact:
2424
matrix_synapse_admin_matrix_nginx_proxy_configuration: |
25-
rewrite ^{{ matrix_synapse_admin_public_endpoint }}$ $scheme://$server_name{{ matrix_synapse_admin_public_endpoint }}/ permanent;
25+
rewrite ^{{ matrix_synapse_admin_public_endpoint }}$ {{ matrix_nginx_proxy_x_forwarded_proto_value }}://$server_name{{ matrix_synapse_admin_public_endpoint }}/ permanent;
2626
2727
location ~ ^{{ matrix_synapse_admin_public_endpoint }}/(.*) {
2828
{% if matrix_nginx_proxy_enabled|default(False) %}

0 commit comments

Comments
 (0)