Skip to content
This repository was archived by the owner on Aug 21, 2022. It is now read-only.

Commit bae62e7

Browse files
committed
feat: websocket proxy
If an application provides a WebSocket endpoint, its path needs to be proxied separately. Set the paths in expose configuration "proxy_websocket_locations". Example: ``` expose: - internal_port: 8096 external_port: 80 service: jellyfin proxy_websocket_locations: ['/socket'] ```
1 parent 18dec05 commit bae62e7

File tree

7 files changed

+45
-17
lines changed

7 files changed

+45
-17
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% extends "location.j2" %}
2+
3+
{% block main_location %}
4+
5+
return 301 https://{{ domainConfig.domain }}$request_uri;
6+
{% endblock %}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
{% extends "serverblock.https.j2" %}
1+
{% extends "location.j2" %}
22

33
{% block main_location %}
4+
45
proxy_pass http://localhost:{~ docker_ports[{{ port_index }}] ~};
6+
proxy_http_version 1.1;
57
proxy_redirect off;
68
proxy_set_header X-Scheme $scheme;
79
proxy_set_header Host $host;
810
proxy_set_header Referer $http_referer;
911
proxy_set_header X-Real-IP $remote_addr;
1012
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
13+
proxy_set_header X-Forwarded-Proto $scheme;
14+
proxy_set_header X-Forwarded-Protocol $scheme;
15+
proxy_set_header X-Forwarded-Host $http_host;
16+
1117
add_header Strict-Transport-Security 'max-age=15552000; includeSubDomains' always;
18+
19+
{% if ns.path in expose.proxy_websocket_locations|d([]) %}
20+
# Websocket proxy
21+
proxy_set_header Upgrade $http_upgrade;
22+
proxy_set_header Connection "upgrade";
23+
{% endif %}
1224
{% endblock %}

templates/nginx/location.j2

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% set locations = expose.proxy_websocket_locations|d([]) + ["/"] %}
2+
3+
{%- set ns = namespace (path = '') %}
4+
{% for path in locations %}
5+
{%- set ns.path = path %}
6+
location {{ path }} {
7+
{%- block main_location %}{%- endblock %}
8+
9+
{% block authentication %}
10+
{% if domainConfig.security.authentication is defined and domainConfig.security.authentication|selectattr("type", "equalto", "basic")|list | length > 0 %}
11+
# Authentication
12+
auth_basic "{{ nginx_basicauth_title }}";
13+
auth_basic_user_file "{{ nginx_htpasswd_path }}/.{{ domainConfig.domain }}";
14+
{% endif %}
15+
{% endblock %}
16+
17+
}
18+
{% endfor %}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% set port = port|default(80) %}
2+
{% set location_file = location_file|d("location-https-redirect.j2") %}
23
{% extends "serverblock.j2" %}
34

45
{% block location %}
@@ -10,7 +11,3 @@
1011
}
1112
}
1213
{% endblock %}
13-
14-
{% block main_location %}
15-
return 301 https://{{ domainConfig.domain }}$request_uri;
16-
{% endblock %}

templates/nginx/serverblock.https.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% set port = port|default(443) %}
2+
{% set location_file = location_file|d("location-reverseproxy.j2") %}
23
{% extends "serverblock.j2" %}
34

45
{% block listen_params %}

templates/nginx/serverblock.j2

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% set location_file = location_file|d("location.j2") %}
12
{% set listen_params_v %}{% block listen_params %}{% endblock %}{% endset %}
23
server {
34
listen {{ port }} {{ listen_params_v }};
@@ -11,12 +12,5 @@ server {
1112

1213
# Locations
1314
{% block location %}{% endblock %}
14-
location / {
15-
{% block main_location %}{% endblock %}
16-
# Authentication
17-
{% block authentication %}{% if domainConfig.security.authentication is defined and domainConfig.security.authentication|selectattr("type", "equalto", "basic")|list | length > 0 %}
18-
auth_basic "{{ nginx_basicauth_title }}";
19-
auth_basic_user_file "{{ nginx_htpasswd_path }}/.{{ domainConfig.domain }}";
20-
{% endif %}{% endblock %}
21-
}
22-
}
15+
{%- include location_file with context %}
16+
}

templates/terraform/nginx_server_block_docker.tf.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ resource "nginx_server_block" "nginx-{{ project_name }}" {
1818
{%- for nginx_expose in domainConfig.expose if nginx_expose.external_port != 443 %}
1919
{% set port_index = all_ports|selectattr('service', 'equalto', nginx_expose.service)|selectattr('internal_port', 'equalto', nginx_expose.internal_port)|map(attribute='index')|first %}
2020
{% if nginx_expose.external_port == 80 %}
21-
{{ lookup('template', "{{ module_role_path | default(role_path) }}/templates/nginx/serverblock.http.j2", template_vars=dict(domainConfig=ns.domainCfg)) }}
22-
{{ lookup('template', "{{ module_role_path | default(role_path) }}/templates/nginx/serverblock.container-reverseproxy.j2", template_vars=dict(port_index=port_index,domainConfig=ns.domainCfg)) }}
21+
{{ lookup('template', "{{ module_role_path | default(role_path) }}/templates/nginx/serverblock.http.j2", template_vars=dict(domainConfig=ns.domainCfg,expose=nginx_expose)) }}
22+
{{ lookup('template', "{{ module_role_path | default(role_path) }}/templates/nginx/serverblock.https.j2", template_vars=dict(port_index=port_index,expose=nginx_expose,domainConfig=ns.domainCfg)) }}
2323
{% else %}
24-
{{ lookup('template', "{{ module_role_path | default(role_path) }}/templates/nginx/serverblock.container-reverseproxy.j2", template_vars=dict(port_index=port_index,port=nginx_expose.external_port,domainConfig=ns.domainCfg)) }}
24+
{{ lookup('template', "{{ module_role_path | default(role_path) }}/templates/nginx/serverblock.https.j2", template_vars=dict(port_index=port_index,expose=nginx_expose,port=nginx_expose.external_port,domainConfig=ns.domainCfg)) }}
2525
{% endif %}
2626
{%- endfor %}
2727
{%- endfor %}

0 commit comments

Comments
 (0)