Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 1 | # nginx templates/vhost.conf.j2 - {{ ansible_managed }} |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org> |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | {% if item.aliases is defined %} |
| 7 | # Redirection of aliases to canonical URL |
| 8 | server { |
| 9 | server_name {{ item.aliases | join(" ") }}; |
| 10 | |
| 11 | listen {{ item.insecure_port | default("80") }}; |
| 12 | listen [::]:{{ item.insecure_port | default("80") }}; |
| 13 | {% if item.tls is defined and item.tls %} |
| 14 | listen {{ item.secure_port | default("443") }} ssl http2; |
| 15 | listen [::]:{{ item.secure_port | default("443") }} ssl http2; |
| 16 | |
| 17 | ssl_certificate {{ certificate_dir }}/{{ item.cert_name | default(item.name) }}/fullchain.pem; |
| 18 | ssl_certificate_key {{ certificate_dir }}/{{ item.cert_name | default(item.name) }}/privkey.pem; |
| 19 | {% endif %} |
| 20 | |
| 21 | # serve ACME Challenges |
| 22 | location /.well-known/acme-challenge { |
| 23 | root {{ acme_challenge_dir }}; |
| 24 | } |
| 25 | |
| 26 | {% if item.strip_request_uri is defined and item.strip_request_uri %} |
Zack Williams | 7aa0caf | 2021-08-02 16:01:06 -0700 | [diff] [blame] | 27 | {% if item.extra_config is defined and item.extra_config %} |
| 28 | # extra config |
| 29 | {{ item.extra_config | indent(2) }} |
| 30 | |
| 31 | {% endif %} |
| 32 | |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 33 | {% set uri = "" %} |
| 34 | {% else %} |
| 35 | {% set uri = "$request_uri" %} |
| 36 | {% endif %} |
| 37 | location / { |
| 38 | return 301 {{ item.redirect_url | default("https://" ~ item.name) }}{{ uri }}; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | {% endif %} |
| 43 | {% if item.redirect_url is not defined %} |
| 44 | {% if item.tls is defined and item.tls %} |
| 45 | # HTTP -> HTTPS redirect |
| 46 | server { |
| 47 | server_name {{ item.name }}; |
| 48 | |
| 49 | listen {{ item.insecure_port | default("80") }}{% if item.default_server is defined and item.default_server %} default_server{% endif %}; |
| 50 | listen [::]:{{ item.insecure_port | default("80") }}{% if item.default_server is defined and item.default_server %} default_server{% endif %}; |
| 51 | |
| 52 | # serve ACME Challenges |
| 53 | location /.well-known/acme-challenge { |
| 54 | root {{ acme_challenge_dir }}; |
| 55 | } |
| 56 | |
| 57 | location / { |
| 58 | return 301 https://{{ item.name }}$request_uri; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | {% endif %} |
| 63 | # Server with content |
| 64 | server { |
| 65 | server_name {{ item.name }}; |
| 66 | |
| 67 | # Listening ports |
| 68 | {% if item.tls is defined and item.tls %} |
| 69 | listen {{ item.secure_port | default("443") }} ssl http2 {% if item.default_server is defined and item.default_server %} default_server{% endif %}; |
| 70 | listen [::]:{{ item.secure_port | default("443") }} ssl http2 {% if item.default_server is defined and item.default_server %} default_server{% endif %}; |
| 71 | |
| 72 | ssl_certificate {{ certificate_dir }}/{{ item.cert_name | default(item.name) }}/fullchain.pem; |
| 73 | ssl_certificate_key {{ certificate_dir }}/{{ item.cert_name | default(item.name) }}/privkey.pem; |
| 74 | {% else %} |
| 75 | listen {{ item.insecure_port | default("80") }}{% if item.default_server is defined and item.default_server %} default_server{% endif %}; |
| 76 | listen [::]:{{ item.insecure_port | default("80") }}{% if item.default_server is defined and item.default_server %} default_server{% endif %}; |
| 77 | |
| 78 | # serve ACME Challenges |
| 79 | location /.well-known/acme-challenge { |
| 80 | root {{ acme_challenge_dir }}; |
| 81 | } |
| 82 | {% endif %} |
| 83 | |
| 84 | # logfile locations |
| 85 | access_log {{ nginx_log_dir }}/{{ item.name }}_access.log; |
| 86 | error_log {{ nginx_log_dir }}/{{ item.name }}_error.log; |
| 87 | |
Zack Williams | ed7880c | 2020-10-09 10:55:10 -0700 | [diff] [blame] | 88 | # user agent (webscraper) blocks |
| 89 | if ($http_user_agent ~* {{ blocked_user_agents }}) { |
| 90 | return 403; |
| 91 | } |
| 92 | |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 93 | {% if item.extra_config is defined and item.extra_config %} |
| 94 | # extra config |
| 95 | {{ item.extra_config | indent(2) }} |
| 96 | |
| 97 | {% endif %} |
Zack Williams | 59e1345 | 2020-10-14 00:24:21 -0700 | [diff] [blame] | 98 | {% if item.proxy_pass is not defined or not item.proxy_pass %} |
| 99 | {% if item.custom_root is defined and item.custom_root %} |
| 100 | root {{ item.custom_root }}; |
| 101 | {% else %} |
| 102 | root {{ nginx_static_dir }}/{{ item.name }}; |
| 103 | {% endif %} |
| 104 | {% if item.php is defined and item.php %} |
| 105 | index index.php; |
| 106 | {% endif %} |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 107 | location / { |
| 108 | {% if item.auth_scope is defined and item.auth_scope %} |
| 109 | auth_basic "{{ item.auth_scope }}"; |
| 110 | auth_basic_user_file "{{ nginx_auth_basic_dir }}/{{ item.auth_scope }}.htpasswd"; |
| 111 | {% endif %} |
Zack Williams | 59e1345 | 2020-10-14 00:24:21 -0700 | [diff] [blame] | 112 | {% if item.php is defined and item.php %} |
| 113 | # PHP site configuration |
| 114 | try_files $uri $uri/ /index.php?$args; |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 115 | {% else %} |
Zack Williams | 59e1345 | 2020-10-14 00:24:21 -0700 | [diff] [blame] | 116 | # Static site indexes |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 117 | index index.html index.htm; |
Zack Williams | 59e1345 | 2020-10-14 00:24:21 -0700 | [diff] [blame] | 118 | {% endif %} |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 119 | {% if item.autoindex is defined and item.autoindex %} |
| 120 | autoindex on; |
| 121 | autoindex_exact_size on; |
| 122 | {% endif %} |
Zack Williams | 59e1345 | 2020-10-14 00:24:21 -0700 | [diff] [blame] | 123 | {% if item.php is defined and item.php %} |
| 124 | } |
| 125 | # PHP configuration |
| 126 | location ~ [^/]\.php(/|$) { |
| 127 | |
| 128 | fastcgi_split_path_info ^(.+?\.php)(/.*)$; |
| 129 | if (!-f $document_root$fastcgi_script_name) { |
| 130 | return 404; |
| 131 | } |
| 132 | fastcgi_param HTTP_PROXY ""; |
| 133 | fastcgi_pass unix:/var/run/php/php-fpm.sock; |
| 134 | fastcgi_index index.php; |
| 135 | |
| 136 | # include the fastcgi_param setting |
| 137 | include fastcgi_params; |
| 138 | {% endif %} |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 139 | {% else %} |
Zack Williams | 59e1345 | 2020-10-14 00:24:21 -0700 | [diff] [blame] | 140 | location / { |
| 141 | {% if item.auth_scope is defined and item.auth_scope %} |
| 142 | auth_basic "{{ item.auth_scope }}"; |
| 143 | auth_basic_user_file "{{ nginx_auth_basic_dir }}/{{ item.auth_scope }}.htpasswd"; |
| 144 | {% endif %} |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 145 | # Proxy configuration |
| 146 | proxy_pass {{ item.proxy_pass }}; |
| 147 | proxy_buffering off; |
| 148 | proxy_http_version 1.1; |
| 149 | proxy_read_timeout 60; |
| 150 | proxy_connect_timeout 90; |
Zack Williams | c8ea0a4 | 2021-02-03 10:51:59 -0700 | [diff] [blame] | 151 | proxy_request_buffering off; |
Zack Williams | b313bae | 2020-04-22 22:00:53 -0700 | [diff] [blame] | 152 | |
| 153 | proxy_set_header Host $host; |
| 154 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 155 | proxy_set_header X-Forwarded-Proto $scheme; |
| 156 | proxy_set_header X-Real-IP $remote_addr; |
| 157 | proxy_set_header Accept-Encoding ""; |
| 158 | {% endif %} |
| 159 | } |
| 160 | } |
| 161 | {% endif %} |