Skip to content

Commit 39dd5a2

Browse files
authored
Merge pull request #72 from 0x28d/master
Allow to generate custom locations in load_balancer mode
2 parents 728555c + 164a6bd commit 39dd5a2

File tree

4 files changed

+107
-14
lines changed

4 files changed

+107
-14
lines changed

README.md

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,18 @@ nginx_http_template:
331331
cert: ssl/default.crt
332332
key: ssl/default.key
333333
web_server:
334-
html_file_location: /usr/share/nginx/html
335-
html_file_name: index.html
334+
locations:
335+
default:
336+
location: /
337+
html_file_location: /usr/share/nginx/html
338+
html_file_name: index.html
336339
http_demo_conf: false
337340
load_balancer:
338-
proxy_pass: backend
339-
health_check_plus: false
341+
locations:
342+
location1:
343+
location: /
344+
proxy_pass: backend
345+
health_check_plus: false
340346
upstreams:
341347
upstream1:
342348
name: backend
@@ -384,6 +390,79 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role to a
384390
- role: nginxinc.nginx
385391
```
386392

393+
This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and installing the open source version of NGINX as a simple web server.
394+
395+
```yaml
396+
---
397+
- hosts: localhost
398+
become: true
399+
roles:
400+
- ansible-role-nginx
401+
vars:
402+
nginx_http_template_enable: true
403+
nginx_http_template:
404+
default:
405+
template_file: http/default.conf.j2
406+
conf_file_name: default.conf
407+
conf_file_location: /etc/nginx/conf.d/
408+
port: 80
409+
server_name: localhost
410+
error_page: /usr/share/nginx/html
411+
web_server:
412+
locations:
413+
default:
414+
location: /
415+
html_file_location: /usr/share/nginx/html
416+
html_file_name: index.html
417+
```
418+
419+
This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and installing the open source version of NGINX as a reverse proxy.
420+
421+
```yaml
422+
---
423+
- hosts: localhost
424+
become: true
425+
roles:
426+
- nginxinc.nginx
427+
vars:
428+
nginx_http_template_enable: true
429+
nginx_http_template:
430+
load_balancer:
431+
locations:
432+
frontend:
433+
location: /
434+
proxy_pass: frontend_servers
435+
backend:
436+
location: /backend
437+
proxy_pass: backend_servers
438+
upstreams:
439+
upstream_1:
440+
name: frontend_servers
441+
lb_method: least_conn
442+
zone_name: frontend
443+
zone_size: 64k
444+
sticky_cookie: false
445+
servers:
446+
frontend_server_1:
447+
address: localhost
448+
port: 80
449+
weight: 1
450+
health_check: max_fails=3 fail_timeout=5s
451+
upstream_2:
452+
name: backend_servers
453+
lb_method: least_conn
454+
zone_name: backend
455+
zone_size: 64k
456+
sticky_cookie: false
457+
servers:
458+
backend_server_1:
459+
address: localhost
460+
port: 8080
461+
weight: 1
462+
health_check: max_fails=3 fail_timeout=5s
463+
```
464+
465+
387466
This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and installing NGINX Plus.
388467

389468
```yaml

defaults/main.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,17 @@ nginx_http_template:
164164
cert: ssl/default.crt
165165
key: ssl/default.key
166166
web_server:
167-
html_file_location: /usr/share/nginx/html
168-
html_file_name: index.html
167+
locations:
168+
default:
169+
location: /
170+
html_file_location: /usr/share/nginx/html
171+
html_file_name: index.html
169172
http_demo_conf: false
170173
load_balancer:
171-
proxy_pass: backend
174+
locations:
175+
backend:
176+
location: /
177+
proxy_pass: backend
172178
health_check_plus: false
173179
upstreams:
174180
upstream1:

templates/http/default.conf.j2

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ server {
2424
{% endif %}
2525
server_name {{ item.value.server_name }};
2626
{% if item.value.load_balancer is defined %}
27-
location / {
28-
proxy_pass http://{{ item.value.load_balancer.proxy_pass }};
27+
{% for location in item.value.load_balancer.locations %}
28+
location {{ item.value.load_balancer.locations[location].location }} {
29+
proxy_pass http://{{ item.value.load_balancer.locations[location].proxy_pass }};
2930
{% if item.value.load_balancer.health_check_plus %}
3031
health_check;
3132
{% endif %}
@@ -34,12 +35,16 @@ server {
3435
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3536
proxy_set_header X-Forwarded-Proto $scheme;
3637
}
38+
39+
{% endfor %}
3740
{% endif %}
3841
{% if item.value.web_server is defined %}
39-
location / {
40-
root {{ item.value.web_server.html_file_location }};
41-
index {{ item.value.web_server.html_file_name }};
42+
{% for location in item.value.web_server.locations %}
43+
location {{ item.value.web_server.locations[location].location }} {
44+
root {{ item.value.web_server.locations[location].html_file_location }};
45+
index {{ item.value.web_server.locations[location].html_file_name }};
4246
}
47+
{% endfor %}
4348
{% if item.value.web_server.http_demo_conf %}
4449
sub_filter_once off;
4550
sub_filter 'server_hostname' '$hostname';

tests/playbooks/nginx-template.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
server_name: localhost
1616
error_page: /usr/share/nginx/html
1717
web_server:
18-
html_file_location: /usr/share/nginx/html
19-
html_file_name: index.html
18+
locations:
19+
default:
20+
location: /
21+
html_file_location: /usr/share/nginx/html
22+
html_file_name: index.html
2023
http_demo_conf: false

0 commit comments

Comments
 (0)