Skip to content

Commit f29d9c3

Browse files
0x28dgdzien
authored andcommitted
Allow setting basic parameters for proxy cache (#94)
* Add parameters for proxy cache
1 parent 80f6157 commit f29d9c3

File tree

5 files changed

+204
-5
lines changed

5 files changed

+204
-5
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,54 @@ nginx_http_template:
348348
auth_basic_file: null
349349
http_demo_conf: false
350350
reverse_proxy:
351+
proxy_cache_path:
352+
- path: /var/cache/nginx/proxy/backend
353+
keys_zone:
354+
name: backend_proxy_cache
355+
size: 10m
356+
levels: "1:2"
357+
max_size: 10g
358+
inactive: 60m
359+
use_temp_path: true
360+
proxy_temp_path:
361+
path: /var/cache/nginx/proxy/temp
362+
proxy_cache_lock: true
363+
proxy_cache_min_uses: 5
364+
proxy_cache_revalidate: true
365+
proxy_cache_use_stale:
366+
- error
367+
- timeout
368+
proxy_ignore_headers:
369+
- Expires
351370
locations:
352371
backend:
353372
location: /
354373
proxy_pass: http://backend
374+
proxy_cache: frontend_proxy_cache
375+
proxy_temp_path:
376+
path: /var/cache/nginx/proxy/backend/temp
377+
proxy_cache_lock: false
378+
proxy_cache_min_uses: 3
379+
proxy_cache_revalidate: false
380+
proxy_cache_use_stale:
381+
- http_403
382+
- http_404
383+
proxy_ignore_headers:
384+
- Vary
385+
- Cache-Control
355386
websocket: false
356387
auth_basic: null
357388
auth_basic_file: null
358389
health_check_plus: false
390+
proxy_cache_enable: false
391+
proxy_cache:
392+
proxy_cache_path:
393+
path: /var/cache/nginx
394+
keys_zone:
395+
name: one
396+
size: 10m
397+
proxy_temp_path:
398+
path: /var/cache/nginx/proxy
359399
upstreams:
360400
upstream1:
361401
name: backend

defaults/main.yml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,58 @@ nginx_http_template:
179179
auth_basic_file: null
180180
http_demo_conf: false
181181
reverse_proxy:
182+
proxy_cache_path:
183+
- path: /var/cache/nginx/proxy/backend
184+
keys_zone:
185+
name: backend_proxy_cache
186+
size: 10m
187+
levels: "1:2"
188+
max_size: 10g
189+
inactive: 60m
190+
use_temp_path: true
191+
proxy_temp_path:
192+
path: /var/cache/nginx/proxy/temp
193+
proxy_cache_lock: true
194+
proxy_cache_min_uses: 5
195+
proxy_cache_revalidate: true
196+
proxy_cache_use_stale:
197+
- error
198+
- timeout
199+
proxy_ignore_headers:
200+
- Expires
182201
locations:
183202
backend:
184203
location: /
185204
proxy_pass: http://backend
205+
proxy_cache: frontend_proxy_cache
206+
proxy_temp_path:
207+
path: /var/cache/nginx/proxy/backend/temp
208+
proxy_cache_lock: false
209+
proxy_cache_min_uses: 3
210+
proxy_cache_revalidate: false
211+
proxy_cache_use_stale:
212+
- http_403
213+
- http_404
214+
proxy_ignore_headers:
215+
- Vary
216+
- Cache-Control
186217
websocket: false
187218
auth_basic: null
188219
auth_basic_file: null
189220
health_check_plus: false
221+
proxy_cache:
222+
proxy_cache_path:
223+
path: /var/cache/nginx
224+
keys_zone:
225+
name: one
226+
size: 10m
227+
proxy_temp_path:
228+
path: /var/cache/nginx/proxy
190229
upstreams:
191230
upstream1:
192231
name: backend
193232
lb_method: least_conn
194-
zone_name: backend
233+
zone_name: backend_mem_zone
195234
zone_size: 64k
196235
sticky_cookie: false
197236
servers:

tasks/conf/template-config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
with_dict: "{{ nginx_http_template }}"
2929
when: nginx_http_template_enable
3030

31+
- name: "(Setup: All NGINX) Ensure NGINX Proxy Cache Directories Exists"
32+
file:
33+
path: "{{ item.1.path }}"
34+
state: directory
35+
owner: "{{ nginx_main_template.user }}"
36+
with_subelements:
37+
- "{{ nginx_http_template }}"
38+
- reverse_proxy.proxy_cache_path
39+
- skip_missing: true
40+
when: nginx_http_template_enable
41+
3142
- name: "(Setup: All NGINX) Dynamically Generate NGINX HTTP Configuration Files"
3243
template:
3344
src: "{{ item.value.template_file }}"

templates/http/default.conf.j2

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@ upstream {{ item.value.upstreams[upstream].name }} {
1515
{% endfor %}
1616
{% endif %}
1717

18+
{% if item.value.reverse_proxy is defined %}
19+
{% if item.value.reverse_proxy.proxy_cache_path is defined %}
20+
{% for proxy_cache_path in item.value.reverse_proxy.proxy_cache_path %}
21+
proxy_cache_path {{ proxy_cache_path.path }} keys_zone={{ proxy_cache_path.keys_zone.name }}:{{ proxy_cache_path.keys_zone.size }}
22+
levels={{ proxy_cache_path.levels }} max_size={{ proxy_cache_path.max_size }}
23+
inactive={{ proxy_cache_path.inactive }} use_temp_path={{ proxy_cache_path.use_temp_path | ternary("on", "off") }};
24+
{% endfor %}
25+
26+
{% if item.value.reverse_proxy.proxy_cache_background_update is defined %}
27+
proxy_cache_background_update {{ item.value.reverse_proxy.proxy_cache_background_update | ternary("on", "off") }};
28+
{% endif %}
29+
{% if item.value.reverse_proxy.proxy_cache_lock is defined %}
30+
proxy_cache_lock {{ item.value.reverse_proxy.proxy_cache_lock | ternary("on", "off") }};
31+
{% endif %}
32+
{% if item.value.reverse_proxy.proxy_cache_min_uses is defined %}
33+
proxy_cache_min_uses {{ item.value.reverse_proxy.proxy_cache_min_uses }};
34+
{% endif %}
35+
{% if item.value.reverse_proxy.proxy_cache_revalidate is defined %}
36+
proxy_cache_revalidate {{ item.value.reverse_proxy.proxy_cache_revalidate | ternary("on", "off") }};
37+
{% endif %}
38+
{% if item.value.reverse_proxy.proxy_cache_use_stale is defined %}
39+
proxy_cache_use_stale {{ item.value.reverse_proxy.proxy_cache_use_stale | join(" ") }};
40+
{% endif %}
41+
{% if item.value.reverse_proxy.proxy_ignore_headers is defined %}
42+
proxy_ignore_headers {{ item.value.reverse_proxy.proxy_ignore_headers | join(" ") }};
43+
{% endif %}
44+
{% if item.value.reverse_proxy.proxy_temp_path is defined %}
45+
proxy_temp_path {{ item.value.reverse_proxy.proxy_temp_path.path }} {{ item.value.reverse_proxy.proxy_temp_path.level_1 | default("") }} {{ item.value.reverse_proxy.proxy_temp_path.level_2 | default("") }} {{ item.value.reverse_proxy.proxy_temp_path.level_3 | default("") }};
46+
{% endif %}
47+
{% endif %}
48+
{% endif %}
49+
1850
server {
1951
{% if item.value.ssl is defined %}
2052
listen {{ item.value.port }} ssl;
@@ -30,6 +62,7 @@ server {
3062
{% if item.value.https_redirect is defined and item.value.https_redirect %}
3163
return 301 https://{{ item.value.server_name }}$request_uri;
3264
{% endif%}
65+
3366
{% if item.value.reverse_proxy is defined %}
3467
{% for location in item.value.reverse_proxy.locations %}
3568
location {{ item.value.reverse_proxy.locations[location].location }} {
@@ -40,9 +73,35 @@ server {
4073
auth_basic_user_file {{ item.value.reverse_proxy.locations[location].auth_basic_file }};
4174
{% endif %}
4275
proxy_pass {{ item.value.reverse_proxy.locations[location].proxy_pass }};
43-
{% if item.value.reverse_proxy.health_check_plus is defined and item.value.reverse_proxy.health_check_plus %}
76+
77+
{% if item.value.reverse_proxy.locations[location].proxy_cache is defined %}
78+
proxy_cache {{ item.value.reverse_proxy.locations[location].proxy_cache }};
79+
{% endif %}
80+
{% if item.value.reverse_proxy.locations[location].proxy_cache_background_update is defined %}
81+
proxy_cache_background_update {{ item.value.reverse_proxy.locations[location].proxy_cache_background_update | ternary("on", "off") }};
82+
{% endif %}
83+
{% if item.value.reverse_proxy.locations[location].proxy_cache_lock is defined %}
84+
proxy_cache_lock {{ item.value.reverse_proxy.locations[location].proxy_cache_lock | ternary("on", "off") }};
85+
{% endif %}
86+
{% if item.value.reverse_proxy.locations[location].proxy_cache_min_uses is defined %}
87+
proxy_cache_min_uses {{ item.value.reverse_proxy.locations[location].proxy_cache_min_uses }};
88+
{% endif %}
89+
{% if item.value.reverse_proxy.locations[location].proxy_cache_revalidate is defined %}
90+
proxy_cache_revalidate {{ item.value.reverse_proxy.locations[location].proxy_cache_revalidate | ternary("on", "off") }};
91+
{% endif %}
92+
{% if item.value.reverse_proxy.locations[location].proxy_cache_use_stale is defined %}
93+
proxy_cache_use_stale {{ item.value.reverse_proxy.locations[location].proxy_cache_use_stale | join(" ") }};
94+
{% endif %}
95+
{% if item.value.reverse_proxy.locations[location].proxy_temp_path is defined %}
96+
proxy_temp_path {{ item.value.reverse_proxy.locations[location].proxy_temp_path.path }} {{ item.value.reverse_proxy.locations[location].proxy_temp_path.level_1 | default("") }} {{ item.value.reverse_proxy.locations[location].proxy_temp_path.level_2 | default("") }} {{ item.value.reverse_proxy.locations[location].proxy_temp_path.level_3 | default("") }};
97+
{% endif %}
98+
{% if item.value.reverse_proxy.locations[location].proxy_ignore_headers is defined %}
99+
proxy_ignore_headers {{ item.value.reverse_proxy.locations[location].proxy_ignore_headers | join(" ") }};
100+
{% endif %}
101+
{% if (item.value.reverse_proxy.health_check_plus is defined) and item.value.reverse_proxy.health_check_plus %}
44102
health_check;
45103
{% endif %}
104+
46105
proxy_set_header Host $host;
47106
proxy_set_header X-Real-IP $remote_addr;
48107
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@@ -52,9 +111,9 @@ server {
52111
proxy_set_header Connection "Upgrade";
53112
{% endif %}
54113
}
55-
56114
{% endfor %}
57115
{% endif %}
116+
58117
{% if item.value.web_server is defined %}
59118
{% for location in item.value.web_server.locations %}
60119
location {{ item.value.web_server.locations[location].location }} {

tests/playbooks/nginx-http-template.yml

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
roles:
66
- ansible-role-nginx
77
vars:
8+
nginx_debug_output: true
89
nginx_http_template_enable: true
910
nginx_http_template:
1011
app:
@@ -15,18 +16,67 @@
1516
server_name: localhost
1617
error_page: /usr/share/nginx/html
1718
reverse_proxy:
19+
proxy_cache_path:
20+
- path: /var/cache/nginx/proxy/frontend
21+
keys_zone:
22+
name: frontend_proxy_cache
23+
size: 5m
24+
levels: "1:2"
25+
max_size: 5g
26+
inactive: 30m
27+
use_temp_path: true
28+
- path: /var/cache/nginx/proxy/backend
29+
keys_zone:
30+
name: backend_proxy_cache
31+
size: 10m
32+
levels: "1:2"
33+
max_size: 10g
34+
inactive: 60m
35+
use_temp_path: true
36+
proxy_temp_path:
37+
path: /var/cache/nginx/proxy/temp
38+
proxy_cache_lock: true
39+
proxy_cache_min_uses: 5
40+
proxy_cache_revalidate: true
41+
proxy_cache_use_stale:
42+
- error
43+
- timeout
44+
proxy_ignore_headers:
45+
- Expires
1846
locations:
1947
frontend:
2048
location: /
2149
proxy_pass: http://frontend_servers/
50+
proxy_cache: frontend_proxy_cache
51+
proxy_temp_path:
52+
path: /var/cache/nginx/proxy/frontend/temp
53+
proxy_cache_lock: false
54+
proxy_cache_min_uses: 3
55+
proxy_cache_revalidate: false
56+
proxy_cache_use_stale:
57+
- http_403
58+
- http_404
59+
proxy_ignore_headers:
60+
- Vary
61+
- Cache-Control
2262
backend:
2363
location: /backend
2464
proxy_pass: http://backend_servers/
65+
proxy_cache: backend_proxy_cache
66+
proxy_temp_path:
67+
path: /var/cache/nginx/proxy/backend/temp
68+
proxy_cache_lock: true
69+
proxy_cache_min_uses: 2
70+
proxy_cache_revalidate: true
71+
proxy_cache_use_stale:
72+
- http_500
73+
- http_502
74+
- http_503
2575
upstreams:
2676
frontend_upstream:
2777
name: frontend_servers
2878
lb_method: least_conn
29-
zone_name: frontend
79+
zone_name: frontend_mem_zone
3080
zone_size: 64k
3181
sticky_cookie: false
3282
servers:
@@ -38,7 +88,7 @@
3888
backend_upstream:
3989
name: backend_servers
4090
lb_method: least_conn
41-
zone_name: backend
91+
zone_name: backend_mem_zone
4292
zone_size: 64k
4393
sticky_cookie: false
4494
servers:

0 commit comments

Comments
 (0)