Skip to content
This repository was archived by the owner on Feb 13, 2023. It is now read-only.

Commit c73c462

Browse files
committed
Update to latest Nginx role version.
1 parent 576164d commit c73c462

File tree

7 files changed

+108
-5
lines changed

7 files changed

+108
-5
lines changed

provisioning/requirements.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
- src: geerlingguy.mysql
3939
version: 2.7.2
4040
- src: geerlingguy.nginx
41-
version: 2.2.0
41+
version: 2.2.1
4242
- src: geerlingguy.nodejs
4343
version: 4.1.0
4444
- src: geerlingguy.php

provisioning/roles/geerlingguy.nginx/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ A list of vhost definitions (server blocks) for Nginx virtual hosts. Each entry
2727
access_log: ""
2828
error_log: ""
2929
state: "present"
30+
template: "{{ nginx_vhost_template }}"
3031
extra_parameters: |
3132
location ~ \.php$ {
3233
fastcgi_split_path_info ^(.+\.php)(/.+)$;
@@ -121,6 +122,73 @@ Configures Nginx's [`log_format`](http://nginx.org/en/docs/http/ngx_http_log_mod
121122

122123
(For RedHat/CentOS only) Set this to `false` to disable the installation of the `nginx` yum repository. This could be necessary if you want the default OS stable packages, or if you use Satellite.
123124

125+
## Overriding configuration templates
126+
127+
If you can't customize via variables because an option isn't exposed, you can override the template used to generate the virtualhost configuration files or the `nginx.conf` file.
128+
129+
```yaml
130+
nginx_conf_template: "nginx.conf.j2"
131+
nginx_vhost_template: "vhost.j2"
132+
```
133+
134+
If necessary you can also set the template on a per vhost basis.
135+
136+
```yaml
137+
nginx_vhosts:
138+
- listen: "80 default_server"
139+
server_name: "site1.example.com"
140+
root: "/var/www/site1.example.com"
141+
index: "index.php index.html index.htm"
142+
template: "{{ playbook_dir }}/templates/site1.example.com.vhost.j2"
143+
- server_name: "site2.example.com"
144+
root: "/var/www/site2.example.com"
145+
index: "index.php index.html index.htm"
146+
template: "{{ playbook_dir }}/templates/site2.example.com.vhost.j2"
147+
```
148+
149+
You can either copy and modify the provided template, or extend it with [Jinja2 template inheritance](http://jinja.pocoo.org/docs/2.9/templates/#template-inheritance) and override the specific template block you need to change.
150+
151+
### Example: Configure gzip in nginx configuration
152+
153+
Set the `nginx_conf_template` to point to a template file in your playbook directory.
154+
155+
```yaml
156+
nginx_conf_template: "{{ playbook_dir }}/templates/nginx.conf.j2"
157+
```
158+
159+
Create the child template in the path you configured above and extend `geerlingguy.nginx` template file relative to your `playbook.yml`.
160+
161+
```
162+
{% extends 'roles/geerlingguy.nginx/templates/nginx.conf.j2' %}
163+
164+
{% block http_gzip %}
165+
gzip on;
166+
gzip_proxied any;
167+
gzip_static on;
168+
gzip_http_version 1.0;
169+
gzip_disable "MSIE [1-6]\.";
170+
gzip_vary on;
171+
gzip_comp_level 6;
172+
gzip_types
173+
text/plain
174+
text/css
175+
text/xml
176+
text/javascript
177+
application/javascript
178+
application/x-javascript
179+
application/json
180+
application/xml
181+
application/xml+rss
182+
application/xhtml+xml
183+
application/x-font-ttf
184+
application/x-font-opentype
185+
image/svg+xml
186+
image/x-icon;
187+
gzip_buffers 16 8k;
188+
gzip_min_length 512;
189+
{% endblock %}
190+
```
191+
124192
## Dependencies
125193
126194
None.

provisioning/roles/geerlingguy.nginx/defaults/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ nginx_ppa_version: stable
1212
# The name of the nginx apt/yum package to install.
1313
nginx_package_name: "nginx"
1414

15+
nginx_conf_template: "nginx.conf.j2"
16+
nginx_vhost_template: "vhost.j2"
17+
1518
nginx_worker_processes: "{{ ansible_processor_vcpus | default(ansible_processor_count) }}"
1619
nginx_worker_connections: "1024"
1720
nginx_multi_accept: "off"
@@ -62,6 +65,7 @@ nginx_vhosts: []
6265
# access_log: ""
6366
# error_log: ""
6467
# extra_parameters: "" # Can be used to add extra config blocks (multiline).
68+
# template: "" # Can be used to override the `nginx_vhost_template` per host.
6569
# state: "absent" # To remove the vhost configuration.
6670

6771
nginx_upstreams: []

provisioning/roles/geerlingguy.nginx/tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# Nginx setup.
3131
- name: Copy nginx configuration in place.
3232
template:
33-
src: nginx.conf.j2
33+
src: "{{ nginx_conf_template }}"
3434
dest: "{{ nginx_conf_file_path }}"
3535
owner: root
3636
group: "{{ root_group }}"

provisioning/roles/geerlingguy.nginx/tasks/vhosts.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
- name: Add managed vhost config files.
1616
template:
17-
src: vhost.j2
17+
src: "{{ item.template|default(nginx_vhost_template) }}"
1818
dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
1919
force: yes
2020
owner: root

provisioning/roles/geerlingguy.nginx/templates/nginx.conf.j2

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@ user {{ nginx_user }};
33
error_log {{ nginx_error_log }};
44
pid {{ nginx_pidfile }};
55

6+
{% block worker %}
67
worker_processes {{ nginx_worker_processes }};
8+
{% endblock %}
79

10+
{% block events %}
811
events {
912
worker_connections {{ nginx_worker_connections }};
1013
multi_accept {{ nginx_multi_accept }};
1114
}
15+
{% endblock %}
1216

1317
{% if nginx_extra_conf_options %}
1418
{{ nginx_extra_conf_options }}
1519
{% endif %}
1620

1721
http {
22+
{% block http_begin %}{% endblock %}
23+
24+
{% block http_basic %}
1825
include {{ nginx_mime_file_path }};
1926
default_type application/octet-stream;
2027

@@ -34,16 +41,20 @@ http {
3441
keepalive_requests {{ nginx_keepalive_requests }};
3542

3643
server_tokens {{ nginx_server_tokens }};
37-
#gzip on;
38-
3944
{% if nginx_proxy_cache_path %}
4045
proxy_cache_path {{ nginx_proxy_cache_path }};
4146
{% endif %}
47+
{% endblock %}
48+
49+
{% block http_gzip %}
50+
# gzip on;
51+
{% endblock %}
4252

4353
{% if nginx_extra_http_options %}
4454
{{ nginx_extra_http_options|indent(4, False) }}
4555
{% endif %}
4656

57+
{% block http_upstream %}
4758
{% for upstream in nginx_upstreams %}
4859
upstream {{ upstream.name }} {
4960
{% if upstream.strategy is defined %}
@@ -57,9 +68,14 @@ http {
5768
{% endif %}
5869
}
5970
{% endfor %}
71+
{% endblock %}
6072

73+
{% block http_includes %}
6174
include {{ nginx_conf_path }}/*.conf;
6275
{% if nginx_conf_path != nginx_vhost_path %}
6376
include {{ nginx_vhost_path }}/*;
6477
{% endif %}
78+
{% endblock %}
79+
80+
{% block http_end %}{% endblock %}
6581
}

provisioning/roles/geerlingguy.nginx/templates/vhost.j2

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
{% block server_redirect %}
2+
{% if item.server_name_redirect is defined %}
3+
listen {{ item.listen | default('80') }};
4+
server_name {{ item.server_name_redirect }};
5+
return 301 $scheme://{{ item.server_name.split(' ')[0] }}$request_uri;
6+
}
7+
{% endif %}
8+
{% endblock %}
9+
110
server {
11+
{% block server_begin %}{% endblock %}
12+
13+
{% block server_basic -%}
214
listen {{ item.listen | default('80') }};
315

416
{% if item.server_name is defined %}
@@ -24,6 +36,9 @@ server {
2436
{% if item.return is defined %}
2537
return {{ item.return }};
2638
{% endif %}
39+
{% endblock %}
40+
41+
{% block server_end %}{% endblock %}
2742

2843
{% if item.extra_parameters is defined %}
2944
{{ item.extra_parameters|indent(4) }}

0 commit comments

Comments
 (0)