Skip to content

Commit c32d373

Browse files
authored
Add support for NGINX GZIP directives (#58)
* Add support for NGINX GZIP directives (resolves #42) * Fix GRPC directives placement within `defaults/main/template.yml`
1 parent ff4d407 commit c32d373

File tree

6 files changed

+159
-44
lines changed

6 files changed

+159
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
ENHANCEMENTS:
66

77
* Add support for NGINX GRPC directives.
8+
* Add support for NGINX GZIP directives.
89
* Added support for upstream server `backup` parameter in http and stream template.
910
* Only run GitHub actions Galaxy CI/CD workflow when a new release is published.
1011
* Update list of supported platforms.

defaults/main/template.yml

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ nginx_config_main_template:
7878
# ssl_trusted_certificate: fileLocation # Optional
7979
# ssl_verify: false # Optional
8080
# ssl_verify_depth: 1 # Optional
81+
# gzip: # Optional -- Configure GZIP
82+
# enable: true # Optional
83+
# buffers: # Optional
84+
# number: 32 # Required
85+
# size: 4k # Required
86+
# comp_level: 1 # Optional
87+
# disable: [] # Optional list
88+
# http_version: 1.1 # Optional -- One of '1.0' or '1.1'
89+
# min_length: 20 # Optional
90+
# proxied: [] # Optional list
91+
# types: [] # Optional list
92+
# vary: false # Optional
8193
access_log_format:
8294
- name: main
8395
format: |-
@@ -167,6 +179,18 @@ nginx_config_http_template:
167179
# ssl_trusted_certificate: fileLocation # Optional
168180
# ssl_verify: false # Optional
169181
# ssl_verify_depth: 1 # Optional
182+
# gzip: # Optional -- Configure GZIP
183+
# enable: true # Optional
184+
# buffers: # Optional
185+
# number: 32 # Required
186+
# size: 4k # Required
187+
# comp_level: 1 # Optional
188+
# disable: [] # Optional list
189+
# http_version: 1.1 # Optional -- One of '1.0' or '1.1'
190+
# min_length: 20 # Optional
191+
# proxied: [] # Optional list
192+
# types: [] # Optional list
193+
# vary: false # Optional
170194
ssl:
171195
cert: /etc/ssl/certs/default.crt
172196
key: /etc/ssl/private/default.key
@@ -232,40 +256,18 @@ nginx_config_http_template:
232256
# security_log: # Optional
233257
# path: path # Required
234258
# destination: dest # Required
235-
# grpc_global: # Optional -- Configure GRPC
236-
# bind: # Optional -- Set to 'false' and remove/comment nested variables to disable grpc_bind
237-
# address: $remote_addr # Required
238-
# transparent: true # Optional
239-
# buffer_size: 4k # Optional
240-
# connect_timeout: 60s # Optional
241-
# hide_header: [] # Optional list
242-
# ignore_headers: [] # Optional list -- 'X-Accel-Redirect' or 'X-Accel-Charset'
243-
# intercept_errors: false # Optional
244-
# next_upstream: [] # Optional list
245-
# next_upstream_timeout: 0 # Optional
246-
# next_upstream_tries: 0 # Optional
247-
# pass_header: [] # Optional list
248-
# read_timeout: 60s # Optional
249-
# send_timeout: 60s # Optional
250-
# set_header: # Optional
251-
# - field: Accept-Encoding # Required
252-
# value: '""' # Required
253-
# socket_keepalive: false # Optional
254-
# ssl_certificate: fileLocation # Optional
255-
# ssl_certificate_key: fileLocation # Optional
256-
# ssl_ciphers: DEFAULT # Optional
257-
# ssl_conf_command: command # Optional
258-
# ssl_crl: fileLocation # Optional
259-
# ssl_name: serverName # Optional
260-
# ssl_password_file: fileLocation # Optional
261-
# ssl_protocols: [] # Optional list
262-
# ssl_server_name: false # Optional
263-
# ssl_session_reuse: true # Optional
264-
# ssl_trusted_certificate: fileLocation # Optional
265-
# ssl_verify: false # Optional
266-
# ssl_verify_depth: 1 # Optional
267-
# grpc: # Optional -- Configure GRPC
268-
# pass: localhost:9000 # Optional
259+
# gzip: # Optional -- Configure GZIP
260+
# enable: true # Optional
261+
# buffers: # Optional
262+
# number: 32 # Required
263+
# size: 4k # Required
264+
# comp_level: 1 # Optional
265+
# disable: [] # Optional list
266+
# http_version: 1.1 # Optional -- One of '1.0' or '1.1'
267+
# min_length: 20 # Optional
268+
# proxied: [] # Optional list
269+
# types: [] # Optional list
270+
# vary: false # Optional
269271
include_files: []
270272
proxy_hide_headers: [] # A list of headers which shouldn't be passed to the application
271273
add_headers:
@@ -314,6 +316,52 @@ nginx_config_http_template:
314316
# security_log: # Optional
315317
# path: path # Required
316318
# destination: dest # Required
319+
# grpc_global: # Optional -- Configure GRPC
320+
# bind: # Optional -- Set to 'false' and remove/comment nested variables to disable grpc_bind
321+
# address: $remote_addr # Required
322+
# transparent: true # Optional
323+
# buffer_size: 4k # Optional
324+
# connect_timeout: 60s # Optional
325+
# hide_header: [] # Optional list
326+
# ignore_headers: [] # Optional list -- 'X-Accel-Redirect' or 'X-Accel-Charset'
327+
# intercept_errors: false # Optional
328+
# next_upstream: [] # Optional list
329+
# next_upstream_timeout: 0 # Optional
330+
# next_upstream_tries: 0 # Optional
331+
# pass_header: [] # Optional list
332+
# read_timeout: 60s # Optional
333+
# send_timeout: 60s # Optional
334+
# set_header: # Optional
335+
# - field: Accept-Encoding # Required
336+
# value: '""' # Required
337+
# socket_keepalive: false # Optional
338+
# ssl_certificate: fileLocation # Optional
339+
# ssl_certificate_key: fileLocation # Optional
340+
# ssl_ciphers: DEFAULT # Optional
341+
# ssl_conf_command: command # Optional
342+
# ssl_crl: fileLocation # Optional
343+
# ssl_name: serverName # Optional
344+
# ssl_password_file: fileLocation # Optional
345+
# ssl_protocols: [] # Optional list
346+
# ssl_server_name: false # Optional
347+
# ssl_session_reuse: true # Optional
348+
# ssl_trusted_certificate: fileLocation # Optional
349+
# ssl_verify: false # Optional
350+
# ssl_verify_depth: 1 # Optional
351+
# grpc: # Optional -- Configure GRPC
352+
# pass: localhost:9000 # Optional
353+
# gzip: # Optional -- Configure GZIP
354+
# enable: true # Optional
355+
# buffers: # Optional
356+
# number: 32 # Required
357+
# size: 4k # Required
358+
# comp_level: 1 # Optional
359+
# disable: [] # Optional list
360+
# http_version: 1.1 # Optional -- One of '1.0' or '1.1'
361+
# min_length: 20 # Optional
362+
# proxied: [] # Optional list
363+
# types: [] # Optional list
364+
# vary: false # Optional
317365
include_files: []
318366
proxy_hide_headers: [] # A list of headers which shouldn't be passed to the application
319367
add_headers:

molecule/default/converge.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@
5151
- field: Accept-Encoding
5252
value: '""'
5353
socket_keepalive: false
54+
gzip:
55+
enable: true
56+
buffers:
57+
number: 32
58+
size: 4k
59+
comp_level: 1
60+
disable:
61+
- '"msie6"'
62+
http_version: 1.1
63+
min_length: 20
64+
proxied:
65+
- expired
66+
types:
67+
- text/html
68+
vary: false
5469
default_type: application/octet-stream
5570
access_log_format:
5671
- name: main

templates/http/default.conf.j2

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ server {
9595
{{ grpc_global(item.value.servers[server].grpc_global) }}
9696
{% endfilter %}
9797
{% endif %}
98+
{% if item.value.servers[server].gzip is defined %}
99+
{% from 'http/gzip.j2' import gzip with context %}
100+
{% filter indent(4) %}
101+
{{ gzip(item.value.servers[server].gzip) }}
102+
{% endfilter %}
103+
{% endif %}
98104
{% if item.value.servers[server].ssl is defined and item.value.servers[server].ssl %}
99105
ssl_certificate {{ item.value.servers[server].ssl.cert }};
100106
ssl_certificate_key {{ item.value.servers[server].ssl.key }};
@@ -221,6 +227,12 @@ server {
221227
{{ grpc_local(item.value.servers[server].reverse_proxy.locations[location].grpc) }}
222228
{% endfilter %}
223229
{% endif %}
230+
{% if item.value.servers[server].reverse_proxy.locations[location].gzip is defined %}
231+
{% from 'http/gzip.j2' import gzip with context %}
232+
{% filter indent(8) %}
233+
{{ gzip(item.value.servers[server].reverse_proxy.locations[location].gzip) }}
234+
{% endfilter %}
235+
{% endif %}
224236
{% if item.value.servers[server].reverse_proxy.locations[location].include_files is defined and item.value.servers[server].reverse_proxy.locations[location].include_files | length %}
225237
{% for file in item.value.servers[server].reverse_proxy.locations[location].include_files %}
226238
include "{{ file }}";
@@ -426,8 +438,14 @@ server {
426438
location {{ item.value.servers[server].web_server.locations[location].location }} {
427439
{% if item.value.servers[server].web_server.locations[location].app_protect is defined %}
428440
{% from 'app_protect.j2' import app_protect_local with context %}
429-
{% filter indent(12) %}
430-
{{ app_protect_local(item.value.servers[server].web_server.locations[location].app_protect) }}
441+
{% filter indent(8) %}
442+
{{ app_protect_local(item.value.servers[server].web_server.locations[location].app_protect) }}
443+
{% endfilter %}
444+
{% endif %}
445+
{% if item.value.servers[server].web_server.locations[location].gzip is defined %}
446+
{% from 'http/gzip.j2' import gzip with context %}
447+
{% filter indent(8) %}
448+
{{ gzip(item.value.servers[server].web_server.locations[location].gzip) }}
431449
{% endfilter %}
432450
{% endif %}
433451
{% if item.value.servers[server].web_server.locations[location].html_file_location is defined %}
@@ -497,7 +515,6 @@ server {
497515
{% if item.value.servers[server].web_server.locations[location].sub_filter.types is defined and item.value.servers[server].web_server.locations[location].sub_filter.types %}
498516
sub_filter_types {{ item.value.servers[server].web_server.locations[location].sub_filter.types }};
499517
{% endif %}
500-
501518
}
502519
{% endfor %}
503520
{% if item.value.servers[server].web_server.http_demo_conf is defined and item.value.servers[server].web_server.http_demo_conf %}
@@ -548,7 +565,6 @@ server {
548565
{% if item.value.servers[server].error_log is defined %}
549566
error_log {{ item.value.servers[server].error_log.location }} {{ item.value.servers[server].error_log.level }};
550567
{% endif %}
551-
552568
}
553569
{% endfor %}
554570
{% endif %}

templates/http/gzip.j2

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{{ ansible_managed | comment }}
2+
{# NGINX GZIP template -- Add directives under its corresponding block #}
3+
{# Format is as follows #}
4+
{# Left side (context module_name) -- Right side (module name) #}
5+
{# HTTP NGINX GZIP -- ngx_http_gzip_module #}
6+
7+
{% macro gzip(gzip) %}
8+
{% if gzip['enable'] is defined %}
9+
gzip {{ gzip['enable'] | ternary('on', 'off') }};
10+
{% endif %}
11+
{% if gzip['buffers'] is defined %}
12+
gzip_buffers {{ gzip['buffers']['number'] }} {{ gzip['buffers']['size'] }};
13+
{% endif %}
14+
{% if gzip['comp_level'] is defined %}
15+
gzip_comp_level {{ gzip['comp_level'] }};
16+
{% endif %}
17+
{% if gzip['disable'] is defined %}
18+
gzip_disable {{ gzip['disable'] | join(' ') }};
19+
{% endif %}
20+
{% if gzip['http_version'] is defined %}
21+
gzip_http_version {{ gzip['http_version'] }};
22+
{% endif %}
23+
{% if gzip['min_length'] is defined %}
24+
gzip_min_length {{ gzip['min_length'] }};
25+
{% endif %}
26+
{% if gzip['proxied'] is defined %}
27+
gzip_proxied {{ 'off' if not gzip['proxied'] }}{{ gzip['proxied'] | join(' ') if gzip['proxied'] | type_debug == 'list' }};
28+
{% endif %}
29+
{% if gzip['types'] is defined %}
30+
gzip_types {{ gzip['types'] | join(' ') }};
31+
{% endif %}
32+
{% if gzip['vary'] is defined %}
33+
gzip_vary {{ gzip['vary'] | ternary('on', 'off') }};
34+
{% endif %}
35+
{% endmacro %}

templates/nginx.conf.j2

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ http {
6767
{% endfor %}
6868
{% endif %}
6969
{% endif %}
70-
7170
sendfile on;
72-
7371
{% if nginx_config_main_template.http_settings.tcp_nopush is defined and nginx_config_main_template.http_settings.tcp_nopush %}
7472
tcp_nopush on;
7573
{% endif %}
7674
{% if nginx_config_main_template.http_settings.tcp_nodelay is defined and nginx_config_main_template.http_settings.tcp_nodelay %}
7775
tcp_nodelay on;
7876
{% endif %}
79-
8077
{% if nginx_config_main_template.http_settings.server_tokens is defined and nginx_config_main_template.http_settings.server_tokens | length %}
8178
server_tokens {{ nginx_config_main_template.http_settings.server_tokens }};
8279
{% endif %}
83-
8480
keepalive_timeout {{ nginx_config_main_template.http_settings.keepalive_timeout }};
85-
86-
#gzip on;
81+
{% if nginx_config_main_template.http_settings.gzip is defined %}
82+
{% from 'http/gzip.j2' import gzip with context %}
83+
{% filter indent(4) %}
84+
{{ gzip(nginx_config_main_template.http_settings.gzip) }}
85+
{% endfilter %}
86+
{% endif %}
8787
{% if nginx_config_main_template.http_settings.cache %}
8888
proxy_cache_path /tmp/cache keys_zone=one:10m;
8989
{% endif %}

0 commit comments

Comments
 (0)