ansible-role-nginx/templates/vhost.conf.j2
2021-07-11 04:08:39 +02:00

107 lines
3.1 KiB
Django/Jinja

#jinja2:lstrip_blocks: True
{% set vhost = {}|combine(nginx_vhosts_defaults, item.value, recursive=True) %}
{% set vhost_name = item.key %}
{% set vhost_headers = {}|combine(nginx.add_headers, vhost.add_headers) %}
{% macro nginx_listen(ips, port, options) %}
{% for ip in ips %}
listen {{ ip }}:{{ port }} {{ options|join(' ') }}{% if vhost.default_server %} default_server{% endif %};
{% endfor %}
{% endmacro %}
server {
{% if vhost.servername|length > 0 %}
server_name {{ vhost.servername|join(' ') }};
{% endif %}
{% if vhost.listen.ssl %}
{% if vhost.listen.v4 %}{{ nginx_listen(vhost.listen.v4_ip, vhost.listen.ssl_port, ["ssl", "http2"]) }}{% endif %}
{% if vhost.listen.v6 %}{{ nginx_listen(vhost.listen.v6_ip, vhost.listen.ssl_port, ["ssl", "http2"]) }}{% endif %}
{% endif %}
{% if vhost.listen.nossl %}
{% if vhost.listen.v4 %}{{ nginx_listen(vhost.listen.v4_ip, vhost.listen.nossl_port, []) }}{% endif %}
{% if vhost.listen.v6 %}{{ nginx_listen(vhost.listen.v6_ip, vhost.listen.nossl_port, []) }}{% endif %}
{% endif %}
{% for i in vhost.listen.custom %}
listen {{ i }};
{% endfor %}
{% for header in vhost_headers if header %}
add_header {{ header }} "{{ vhost_headers[header] }}";
{% endfor %}
{% for c in vhost.custom|default([]) %}
{{ c }};
{% endfor %}
{% if vhost.backend %}
location / {
proxy_pass {{ vhost.backend }};
# add proxy headers
proxy_set_header Host {{ vhost.host }};
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Ssl $https;
proxy_set_header X-Url-Scheme $scheme;
# add custom proxy headers
{% for header in vhost.add_proxy_headers if header %}
proxy_set_header {{ header }} "{{ vhost.add_proxy_headers[header] }}";
{% endfor %}
# Websockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# remove custom proxy headers
{% for header in vhost.hide_proxy_headers if header %}
proxy_hide_header {{ header }};
{% endfor %}
# hide downstream headers for security reasons
proxy_hide_header X-Powered-By;
proxy_hide_header Server;
proxy_hide_header X-AspNetMvc-Version;
proxy_hide_header X-AspNet-Version;
# no double headers
proxy_hide_header Strict-Transport-Security;
}
{% endif %}
{% for location in vhost.locations %}
location {{ location.match }} {
{% if "alias" in location %}
alias {{ location.alias }};
{% endif %}
{% for c in location.custom|default([]) %}
{{ c }};
{% endfor %}
}
{% endfor %}
{% if vhost.auth.enable %}
auth_basic "restricted area";
auth_basic_user_file {{ vhost.auth.path }};
satisfy {{ vhost.auth.satisfy }};
{% endif %}
{% for include in vhost.includes %}
include {{ include }};
{% endfor %}
{% if vhost.letsencrypt %}
ssl_certificate /etc/ssl/nginx_{{ vhost_name }}.chain.crt;
ssl_certificate_key /etc/ssl/private/nginx_{{ vhost_name }}.key;
{% elif vhost.crt and vhost.key %}
ssl_certificate {{ vhost.crt }};
ssl_certificate_key {{ vhost.key }};
{% endif %}
}