/
location.conf
224 lines (223 loc) · 11.3 KB
/
location.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
location {{ location.matchtype }} {{ location.urlpattern }} {
{% if location.honeypot == '1' %}
return 418;
{% else %}
{% if location.enable_secrules is defined and location.enable_secrules == '1' %}
SecRulesEnabled;
{% endif %}
{% if location.enable_learning_mode is defined and location.enable_learning_mode == '1' %}
LearningMode;
{% endif %}
{% if location.xss_block_score is defined %}
LibInjectionXss;
CheckRule "$LIBINJECTION_XSS >= {{ location.xss_block_score }}" BLOCK;
{% endif %}
BasicRule wl:19;
{% set added_policies = [] %}
{% if location.custom_policy is defined %}
{% for custom_policy_uuid in location.custom_policy.split(',') %}
{% set custom_policy = helpers.getUUID(custom_policy_uuid) %}
{% if custom_policy is defined %}
{% set naxsi_ruletype = 'basic' %}
{% include "OPNsense/Nginx/naxsirule.conf" ignore missing with context %}
CheckRule "$policy{{ custom_policy_uuid.replace('-', '') }} {{ custom_policy.operator }} {{ custom_policy.value
}}" {{ custom_policy.action }};
{% endif %}
{% endfor %}
{% endif %}
{% if location.rewrites is defined %}
{% for rewrite_uuid in location.rewrites.split(',') %}
{% set rewrite = helpers.getUUID(rewrite_uuid) %}
rewrite {{ rewrite.source }} {{ rewrite.destination }}{% if rewrite.flag is defined%} {{ rewrite.flag }}{% endif %};
{% endfor %}
{% endif %}
{% if location.sqli_block_score is defined %}
LibInjectionSql;
CheckRule "$LIBINJECTION_SQL >= {{ location.sqli_block_score }}" BLOCK;
{% endif %}
{% if location.secrules_errorpage is defined and location.secrules_errorpage != '' %}
DeniedUrl "/error_{{ location.secrules_errorpage.replace('-', '') }}.html";
{% else %}
DeniedUrl "/waf_denied.html";
{% endif %}
{% if location.errorpages is defined and location.errorpages != '' %}
{% for errorpage_uuid in location.errorpages.split(',') %}
{% set errorpage = helpers.getUUID(errorpage_uuid) %}
error_page {{ errorpage.statuscodes.replace(',', ' ') }} {% if errorpage.response is defined and errorpage.response != '' %}={{ errorpage.response }} {% endif %}{% if errorpage.redirect is defined and errorpage.redirect != '' %}{{ errorpage.redirect }}{% else %}/error_{{ errorpage_uuid.replace('-', '') }}.html{% endif %};
{% endfor %}
{% endif %}
{% if location.force_https is defined and location.force_https == '1' %}
if ($scheme != "https") {
return 302 https://$host$request_uri;
}
{% endif %}
{% if location.ip_acl is defined %}
{% set ip_acl = location.ip_acl %}
{% include "OPNsense/Nginx/ipacl.conf" %}
{% endif %}
{% if location.root is defined %}
root {{ location.root }};
{% endif %}
{% if location.max_body_size is defined %}
client_max_body_size {{ location.max_body_size }};
{% endif %}
{% if location.body_buffer_size is defined %}
client_body_buffer_size {{ location.body_buffer_size }};
{% endif %}
{% if location.satisfy is defined %}
satisfy {{ location.satisfy }};
{% endif %}
{% if location.index is defined %}
index {{ location.index.replace(",", " ") }};
{% endif %}
{% if location.autoindex is defined and location.autoindex == '1' %}
autoindex on;
{% else %}
autoindex off;
{% endif %}
{% if location.limit_request_connections is defined %}
{% set limit_request = location.limit_request_connections %}
{% include "OPNsense/Nginx/httprequestlimit.conf" ignore missing with context %}
{% endif %}
{% if location.authbasic is defined and location.authbasicuserfile is defined %}
auth_basic "{{location.authbasic}}";
auth_basic_user_file /var/db/nginx/auth/{{ location.authbasicuserfile }};
{% else %}
{% if location.advanced_acl is defined and location.advanced_acl == '1' %}
auth_request /opnsense-auth-request;
{% endif %}
{% endif %}
http2_push_preload {% if location.http2_push_preload is defined and location.http2_push_preload == '1' %}on{% else %}off{% endif %};
{% if location.php_enable is defined and location.php_enable == '1' %}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param TLS-Cipher $ssl_cipher;
fastcgi_param TLS-Protocol $ssl_protocol;
fastcgi_param TLS-SNI-Host $ssl_server_name;
fastcgi_param TLS-Client-Intercepted $tls_intercepted;
{% if server.zero_rtt == '1' %}
fastcgi_param Early-Data $ssl_early_data;
{% endif %}
fastcgi_intercept_errors off;
{% if location.upstream is not defined %}
fastcgi_pass unix:/var/run/php-www.socket;
{% if location.php_override_scriptname is defined and location.php_override_scriptname != '' %}
fastcgi_param SCRIPT_FILENAME $document_root/{{ location.php_override_scriptname }};
{% else %}
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
{% endif %}
{% else %}
fastcgi_pass upstream{{ location.upstream.replace('-','') }};
fastcgi_connect_timeout 10s;
{% if location.php_override_scriptname is defined and location.php_override_scriptname != '' %}
fastcgi_param SCRIPT_FILENAME {{ location.php_override_scriptname }};
{% else %}
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
{% endif %}
{% endif %}
{% endif%}
{% if location.upstream is defined and (location.php_enable is not defined or location.php_enable != '1') %}
{% set upstream = helpers.getUUID(location.upstream) %}
proxy_set_header Host $host{% if upstream.host_port is defined and upstream.host_port != '' %}:{{ upstream.host_port }}{% endif %};
{% if location.websocket is defined and location.websocket == '1' %}
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
{% endif %}
{% if location.proxy_buffer_size is defined and location.proxy_buffer_size != '' %}
proxy_buffer_size {{ location.proxy_buffer_size }}k;
{% endif %}
{% if location.proxy_buffers_count is defined and location.proxy_buffers_count != '' and location.proxy_buffers_size is defined and location.proxy_buffers_size != '' %}
proxy_buffers {{ location.proxy_buffers_count }} {{ location.proxy_buffers_size }}k;
{% endif %}
{% if location.proxy_busy_buffers_size is defined and location.proxy_busy_buffers_size != '' %}
proxy_busy_buffers_size {{ location.proxy_busy_buffers_size }}k;
{% endif %}
{% if location.cache_path is defined and location.cache_path != '' %}
proxy_cache {{ location.cache_path.replace('-', '') }};
{% if location.cache_use_stale is defined and location.cache_use_stale != '' %}
proxy_cache_use_stale {{ location.cache_use_stale.replace(',', ' ') }};
{% endif %}
proxy_cache_min_uses {{ location.cache_min_uses|default('1') }};
proxy_cache_background_update {% if location.cache_background_update is defined and location.cache_background_update == '1' %}on{% else %}off{% endif %};
proxy_cache_lock {% if location.cache_lock is defined and location.cache_lock == '1'%}on{% else %}off{% endif %};
proxy_cache_revalidate {% if location.cache_revalidate is defined and location.cache_revalidate == '1' %}on{% else %}off{% endif %};
proxy_cache_methods GET HEAD{% if location.cache_methods is defined and location.cache_methods != '' %} {{ location.cache_methods.replace(',', ' ') }}{% endif %};
{% endif %}
proxy_set_header X-TLS-Cipher $ssl_cipher;
proxy_set_header X-TLS-Protocol $ssl_protocol;
proxy_set_header X-TLS-SNI-Host $ssl_server_name;
# proxy headers for backend server
{% if server.verify_client != 'off' %}
proxy_set_header X-Client-Dn $ssl_client_s_dn;
proxy_set_header X-Client-Verify $ssl_client_verify;
{% endif %}
{% if server.verify_client == 'optional_no_ca' %}
proxy_set_header X-Client-Certificate $ssl_client_escaped_cert;
{% endif %}
{% if server.zero_rtt == '1' %}
proxy_set_header Early-Data $ssl_early_data;
{% endif %}
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-Port $server_port;
proxy_set_header X-Forwarded-Host {% if upstream.x_forwarded_host_verbatim is defined and upstream.x_forwarded_host_verbatim == '1'%}$http_host{% else %}$host{% endif %};
proxy_set_header X-TLS-Client-Intercepted $tls_intercepted;
{% if location.proxy_read_timeout is defined and location.proxy_read_timeout != '' %}
proxy_read_timeout {{ location.proxy_read_timeout }}s;
{% endif %}
{% if location.proxy_send_timeout is defined and location.proxy_send_timeout != '' %}
proxy_send_timeout {{ location.proxy_send_timeout }}s;
{% endif %}
proxy_ignore_client_abort {% if location.proxy_ignore_client_abort == '1' %}on{% else %}off{% endif %};
proxy_request_buffering {% if location.proxy_request_buffering == '1' %}on{% else %}off{% endif %};
proxy_max_temp_file_size {% if location.proxy_max_temp_file_size is defined %}{{ location.proxy_max_temp_file_size }}{% else %}1024{% endif %}m;
proxy_buffering {% if location.proxy_buffering == '1' %}on{% else %}off{% endif %};
{% if location.path_prefix is defined and location.path_prefix != '' %}
proxy_pass http{% if upstream.tls_enable == '1' %}s{% endif %}://upstream{{ location.upstream.replace('-','') }}{{ location.path_prefix }};
{% else %}
proxy_pass http{% if upstream.tls_enable == '1' %}s{% endif %}://upstream{{ location.upstream.replace('-','') }};
{% endif %}
{% if upstream.tls_enable == '1' %}
{% if upstream.tls_client_certificate is defined and upstream.tls_client_certificate != '' %}
proxy_ssl_certificate_key /usr/local/etc/nginx/key/{{ upstream.tls_client_certificate }}.key;
proxy_ssl_certificate /usr/local/etc/nginx/key/{{ upstream.tls_client_certificate }}.pem;
{% endif %}
{% if location.proxy_ssl_server_name is defined and location.proxy_ssl_server_name == '1' %}
proxy_ssl_server_name on;
{% else %}
proxy_ssl_server_name off;
{% endif %}
{% if upstream.tls_name_override is defined and upstream.tls_name_override != '' %}
proxy_ssl_name {{ upstream.tls_name_override }};
{% elif location.proxy_ssl_server_name is defined and location.proxy_ssl_server_name == '1' %}
proxy_ssl_name $upstream_sni_name;
{% endif %}
{% if upstream.tls_protocol_versions is defined and upstream.tls_protocol_versions != '' %}
proxy_ssl_protocols {{ upstream.tls_protocol_versions.replace(',', ' ') }};
{% endif %}
{% if upstream.tls_session_reuse is defined %}
proxy_ssl_session_reuse {% if upstream.tls_session_reuse == '1' %}on{% else %}off{% endif %};
{% endif %}
{% if upstream.tls_trusted_certificate is defined and upstream.tls_trusted_certificate != '' %}
proxy_ssl_trusted_certificate /usr/local/etc/nginx/key/trust_upstream_{{ location.upstream }}.pem;
{% else %}
proxy_ssl_trusted_certificate /etc/ssl/cert.pem;
{% endif %}
proxy_ssl_verify {% if upstream.tls_verify == '1' %}on{% else %}off{% endif %};
{% if upstream.tls_verify_depth is defined and upstream.tls_verify_depth != '' %}
proxy_ssl_verify_depth {{ upstream.tls_verify_depth }};
{% endif %}
{% if upstream.store is defined and upstream.store != '' %}
proxy_store {% if upstream.store == '1' %}on{% else %}off{% endif %};
{% endif %}
{% endif %}
{% for our_header in our_headers %}
proxy_hide_header {{ our_header }};
{% endfor %}
{% endif %}
{% endif %}{# honeypot #}
include {{ location['@uuid'] }}_post/*.conf;
}