Skip to content

Commit bcc3f1e

Browse files
committed
Improve templating
* Rename load_balancer block to reverse_proxy * Clarify that the redirect variable refers to a http to https redirect * Allow setting the listen port when using SSL * Improve SSL defaults * Allow setting http or https server locations in proxy_pass
1 parent c1c3a37 commit bcc3f1e

File tree

3 files changed

+57
-60
lines changed

3 files changed

+57
-60
lines changed

README.md

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ nginx_main_template:
321321
# Enable creating dynamic templated NGINX HTTP configuration files.
322322
# Defaults will not produce a valid configuration. Instead they are meant to showcase
323323
# the options available for templating. Each key represents a new configuration file.
324-
# Comment out load_balancer or web_server depending on whether you wish to create a web server
324+
# Comment out reverse_proxy or web_server depending on whether you wish to create a web server
325325
# or load balancer configuration file.
326326
nginx_http_template_enable: false
327327
nginx_http_template:
@@ -332,7 +332,7 @@ nginx_http_template:
332332
port: 8081
333333
server_name: localhost
334334
error_page: /usr/share/nginx/html
335-
redirect: false
335+
https_redirect: false
336336
autoindex: false
337337
ssl:
338338
cert: ssl/default.crt
@@ -345,11 +345,11 @@ nginx_http_template:
345345
html_file_name: index.html
346346
autoindex: false
347347
http_demo_conf: false
348-
load_balancer:
348+
reverse_proxy:
349349
locations:
350350
backend:
351351
location: /
352-
proxy_pass: backend
352+
proxy_pass: http://backend
353353
health_check_plus: false
354354
upstreams:
355355
upstream1:
@@ -410,9 +410,6 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
410410
nginx_http_template_enable: true
411411
nginx_http_template:
412412
default:
413-
template_file: http/default.conf.j2
414-
conf_file_name: default.conf
415-
conf_file_location: /etc/nginx/conf.d/
416413
port: 80
417414
server_name: localhost
418415
error_page: /usr/share/nginx/html
@@ -423,7 +420,6 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
423420
location: /
424421
html_file_location: /usr/share/nginx/html
425422
html_file_name: index.html
426-
autoindex: false
427423
```
428424

429425
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.
@@ -437,39 +433,40 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
437433
vars:
438434
nginx_http_template_enable: true
439435
nginx_http_template:
440-
load_balancer:
441-
locations:
442-
frontend:
443-
location: /
444-
proxy_pass: frontend_servers
445-
backend:
446-
location: /backend
447-
proxy_pass: backend_servers
448-
upstreams:
449-
upstream_1:
450-
name: frontend_servers
451-
lb_method: least_conn
452-
zone_name: frontend
453-
zone_size: 64k
454-
sticky_cookie: false
455-
servers:
456-
frontend_server_1:
457-
address: localhost
458-
port: 80
459-
weight: 1
460-
health_check: max_fails=3 fail_timeout=5s
461-
upstream_2:
462-
name: backend_servers
463-
lb_method: least_conn
464-
zone_name: backend
465-
zone_size: 64k
466-
sticky_cookie: false
467-
servers:
468-
backend_server_1:
469-
address: localhost
470-
port: 8080
471-
weight: 1
472-
health_check: max_fails=3 fail_timeout=5s
436+
default:
437+
reverse_proxy:
438+
locations:
439+
frontend:
440+
location: /
441+
proxy_pass: http://frontend_servers
442+
backend:
443+
location: /backend
444+
proxy_pass: http://backend_servers
445+
upstreams:
446+
upstream_1:
447+
name: frontend_servers
448+
lb_method: least_conn
449+
zone_name: frontend
450+
zone_size: 64k
451+
sticky_cookie: false
452+
servers:
453+
frontend_server_1:
454+
address: localhost
455+
port: 80
456+
weight: 1
457+
health_check: max_fails=3 fail_timeout=5s
458+
upstream_2:
459+
name: backend_servers
460+
lb_method: least_conn
461+
zone_name: backend
462+
zone_size: 64k
463+
sticky_cookie: false
464+
servers:
465+
backend_server_1:
466+
address: localhost
467+
port: 8080
468+
weight: 1
469+
health_check: max_fails=3 fail_timeout=5s
473470
```
474471

475472

defaults/main.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,25 @@ nginx_rest_api_dashboard: false
102102
# Upload the main NGINX configuration file.
103103
nginx_main_upload_enable: false
104104
nginx_main_upload_src: conf/nginx.conf
105-
nginx_main_upload_dest: /etc/nginx
105+
nginx_main_upload_dest: /etc/nginx/nginx.conf
106106
# Upload HTTP NGINX configuration files.
107107
nginx_http_upload_enable: false
108108
nginx_http_upload_src: conf/http/*.conf
109-
nginx_http_upload_dest: /etc/nginx/conf.d
109+
nginx_http_upload_dest: /etc/nginx/conf.d/*.conf
110110
# Upload Stream NGINX configuration files.
111111
nginx_stream_upload_enable: false
112112
nginx_stream_upload_src: conf/stream/*.conf
113-
nginx_stream_upload_dest: /etc/nginx/conf.d
113+
nginx_stream_upload_dest: /etc/nginx/conf.d/*.conf
114114
# Upload HTML files.
115115
nginx_html_upload_enable: false
116116
nginx_html_upload_src: www/*
117117
nginx_html_upload_dest: /usr/share/nginx/html
118118
# Upload SSL certificates and keys.
119119
nginx_ssl_upload_enable: false
120120
nginx_ssl_crt_upload_src: ssl/*.crt
121-
nginx_ssl_crt_upload_dest: /etc/ssl/certs/
121+
nginx_ssl_crt_upload_dest: /etc/ssl/certs/*.crt
122122
nginx_ssl_key_upload_src: ssl/*.key
123-
nginx_ssl_key_upload_dest: /etc/ssl/private/
123+
nginx_ssl_key_upload_dest: /etc/ssl/private/*.key
124124

125125
# Enable creating dynamic templated NGINX HTML demo websites.
126126
nginx_html_demo_template_enable: false
@@ -165,11 +165,11 @@ nginx_http_template:
165165
port: 8081
166166
server_name: localhost
167167
error_page: /usr/share/nginx/html
168-
redirect: false
168+
https_redirect: false
169169
autoindex: false
170170
ssl:
171-
cert: ssl/default.crt
172-
key: ssl/default.key
171+
cert: /etc/ssl/certs/default.crt
172+
key: /etc/ssl/private/default.key
173173
web_server:
174174
locations:
175175
default:
@@ -178,11 +178,11 @@ nginx_http_template:
178178
html_file_name: index.html
179179
autoindex: false
180180
http_demo_conf: false
181-
load_balancer:
181+
reverse_proxy:
182182
locations:
183183
backend:
184184
location: /
185-
proxy_pass: backend
185+
proxy_pass: http://backend
186186
health_check_plus: false
187187
upstreams:
188188
upstream1:

templates/http/default.conf.j2

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ upstream {{ item.value.upstreams[upstream].name }} {
1818

1919
server {
2020
{% if item.value.ssl is defined %}
21-
listen 443 ssl;
22-
ssl_certificate {{ nginx_ssl_crt_upload_dest }}/{{ item.value.ssl.cert }};
23-
ssl_certificate_key {{ nginx_ssl_key_upload_dest }}/{{ item.value.ssl.key }};
21+
listen {{ item.value.port }} ssl;
22+
ssl_certificate {{ item.value.ssl.cert }};
23+
ssl_certificate_key {{ item.value.ssl.key }};
2424
{% else %}
2525
listen {{ item.value.port }};
2626
{% endif %}
2727
server_name {{ item.value.server_name }};
2828
{% if item.value.autoindex is defined and item.value.autoindex %}
2929
autoindex on;
3030
{% endif %}
31-
{% if item.value.redirect is defined and item.value.redirect %}
31+
{% if item.value.https_redirect is defined and item.value.https_redirect %}
3232
return 301 https://{{ item.value.server_name }}$request_uri;
3333
{% endif%}
34-
{% if item.value.load_balancer is defined %}
35-
{% for location in item.value.load_balancer.locations %}
36-
location {{ item.value.load_balancer.locations[location].location }} {
37-
proxy_pass http://{{ item.value.load_balancer.locations[location].proxy_pass }};
38-
{% if item.value.load_balancer.health_check_plus is defined and item.value.load_balancer.health_check_plus %}
34+
{% if item.value.reverse_proxy is defined %}
35+
{% for location in item.value.reverse_proxy.locations %}
36+
location {{ item.value.reverse_proxy.locations[location].location }} {
37+
proxy_pass {{ item.value.reverse_proxy.locations[location].proxy_pass }};
38+
{% if item.value.reverse_proxy.health_check_plus is defined and item.value.reverse_proxy.health_check_plus %}
3939
health_check;
4040
{% endif %}
4141
proxy_set_header Host $host;

0 commit comments

Comments
 (0)