Skip to content

Commit bf8bca2

Browse files
authored
Add map and split_clients directives (#235)
1 parent d2204fb commit bf8bca2

File tree

6 files changed

+131
-3
lines changed

6 files changed

+131
-3
lines changed

CHANGELOG.md

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

1111
* Bump the Ansible `community.general` collection to `4.6.1` and `community.docker` collection to `2.2.1`.
1212
* Add labels to loops in `tasks/config/template-config.yml` to reduce amount of output data.
13+
* Add the `map` and `split_clients` directives into the `http` core template.
1314

1415
BUG FIXES:
1516

defaults/main/template.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,17 @@ nginx_config_http_template:
630630
inactive: 20s
631631
min_uses: 2 # Number
632632
valid: 1m
633+
map: # Configure maps -- Available only in the 'http' context
634+
hash_bucket_size: 64
635+
hash_max_size: 2048
636+
mappings: # Dictionary or list of dictionaries
637+
string: $remote_addr # Required
638+
variable: $upstream # Required
639+
hostnames: false # Boolean
640+
volatile: false # Boolean
641+
content: # Dictionary or list of dictionaries
642+
- value: default
643+
new_value: 0
633644
rewrite: # Configure rewrite directives
634645
return: # Can also be set to a return URL or code directly -- Not available in the 'http' context
635646
code: 200 # Required -- You have to set either 'code' or 'url'
@@ -644,6 +655,14 @@ nginx_config_http_template:
644655
- variable: $var # Required
645656
value: var # Required
646657
uninitialized_variable_warn: true # Boolean
658+
split_clients: # Configure split_clients -- Available only in the 'http' context
659+
string: $remote_addr # Required
660+
variable: $upstream # Required
661+
content: # List of dictionaries -- Percentages must add up to 100%
662+
- percentage: 20% # Required
663+
value: appv2 # Required
664+
- percentage: "*" # Required
665+
value: app # Required
647666
sub_filter: # Configure sub_filter directives
648667
sub_filters: # Dictionary or a list of dictionaries
649668
- string: server_hostname # Required

molecule/default/converge.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,35 @@
484484
inactive: 20s
485485
min_uses: 2
486486
valid: 1m
487+
map:
488+
hash_bucket_size: 128
489+
hash_max_size: 4096
490+
mappings:
491+
- string: $http_host
492+
variable: $name
493+
hostnames: true
494+
volatile: true
495+
content:
496+
- value: example.com
497+
new_value: 1
498+
- string: $http_user_agent
499+
variable: $isblockaccess_ua
500+
content:
501+
- value: "default"
502+
new_value: 0
503+
- value: '"~jndi:ldap"'
504+
new_value: 1
487505
rewrite:
488506
log: false
489507
uninitialized_variable_warn: false
508+
split_clients:
509+
string: $remote_addr
510+
variable: $upstream_split
511+
content:
512+
- percentage: 20%
513+
value: appv2
514+
- percentage: "*"
515+
value: app
490516
sub_filter:
491517
sub_filters:
492518
- string: server_hostname

molecule/plus/converge.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,35 @@
278278
inactive: 20s
279279
min_uses: 2
280280
valid: 1m
281+
map:
282+
hash_bucket_size: 128
283+
hash_max_size: 4096
284+
mappings:
285+
- string: $http_host
286+
variable: $name
287+
hostnames: true
288+
volatile: true
289+
content:
290+
- value: example.com
291+
new_value: 1
292+
- string: $http_user_agent
293+
variable: $isblockaccess_ua
294+
content:
295+
- value: "default"
296+
new_value: 0
297+
- value: '"~jndi:ldap"'
298+
new_value: 1
281299
rewrite:
282300
log: false
283301
uninitialized_variable_warn: false
302+
split_clients:
303+
string: $remote_addr
304+
variable: $upstream_split
305+
content:
306+
- percentage: 20%
307+
value: appv2
308+
- percentage: "*"
309+
value: app
284310
sub_filter:
285311
sub_filters:
286312
- string: server_hostname

templates/http/default.conf.j2

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,18 @@
8080
{% from 'http/modules.j2' import log with context %}
8181
{{ log(item['config']['log']) }}
8282
{%- endif %}
83+
{% if item['config']['map'] is defined %}
84+
{% from 'http/modules.j2' import map with context %}
85+
{{ map(item['config']['map']) }}
86+
{%- endif %}
8387
{% if item['config']['rewrite'] is defined %}
8488
{% from 'http/modules.j2' import rewrite with context %}
8589
{{ rewrite(item['config']['rewrite']) }}
8690
{%- endif %}
91+
{% if item['config']['split_clients'] is defined %}
92+
{% from 'http/modules.j2' import split_clients with context %}
93+
{{ split_clients(item['config']['split_clients']) }}
94+
{%- endif %}
8795
{% if item['config']['sub_filter'] is defined %}
8896
{% from 'http/modules.j2' import sub_filter with context %}
8997
{{ sub_filter(item['config']['sub_filter']) }}

templates/http/modules.j2

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ match {{ match['name'] }} {
124124

125125
{# NGINX HTTP Keyval -- ngx_http_keyval_module #}
126126
{# Available only in NGINX Plus #}
127-
{% macro keyval(keyval) %}
128-
{% if keyval['keyvals'] is defined %}{# 'keyval' directive is only available in the 'http' context #}
127+
{% macro keyval(keyval) %}{# 'keyval' module is only available in the 'http' context #}
128+
{% if keyval['keyvals'] is defined %}
129129
{% for keyval in keyval['keyvals'] %}
130130
keyval {{ keyval['key'] }} {{ keyval['variable'] }} {{ 'zone=' + keyval['zone'] | string }};
131131
{% endfor %}
132132
{% endif %}
133-
{% if keyval['zones'] is defined %}{# 'keyval_zone' directive is only available in the 'http' context #}
133+
{% if keyval['zones'] is defined %}
134134
{% for zone in keyval['zones'] %}
135135
keyval_zone {{ 'zone=' + zone['name'] | string + ':' + zone['size'] | string }}{{ (' state=' + zone['state'] | string) if zone['state'] is defined }}{{ (' timeout=' + zone['timeout'] | string) if zone['timeout'] is defined }}{{ (' type=' + zone['type'] | string) if zone['type'] is defined and zone['type'] in ['string', 'ip', 'prefix'] }}{{ ' sync' if zone['sync'] is defined and zone['sync'] is boolean and zone['sync'] | bool }};
136136
{% endfor %}
@@ -193,6 +193,38 @@ open_log_file_cache {{ 'off' if not log['open_log_file_cache'] else ('max=' + lo
193193

194194
{% endmacro %}
195195

196+
{# NGINX HTTP Map -- ngx_http_map_module #}
197+
{% macro map(map) %}{# 'map' module is only available in the 'http' context #}
198+
{% if map['hash_bucket_size'] is defined %}
199+
map_hash_bucket_size {{ map['hash_bucket_size'] }};
200+
{% endif %}
201+
{% if map['hash_max_size'] is defined %}
202+
map_hash_max_size {{ map['hash_max_size'] }};
203+
{% endif %}
204+
{% if map['mappings'] is defined %}
205+
{% for map_data in map['mappings'] %}
206+
{% if map_data['string'] is defined and map_data['variable'] is defined %}
207+
map {{ map_data['string'] }} {{ map_data['variable'] }} {
208+
{% if map_data['hostnames'] is defined and map_data['hostnames'] is boolean and map_data['hostnames'] | bool %}
209+
hostnames;
210+
{% endif %}
211+
{% if map_data['volatile'] is defined and map_data['volatile'] is boolean and map_data['volatile'] | bool %}
212+
volatile;
213+
{% endif %}
214+
{% if map_data['content'] is defined %}
215+
{% for content_line in map_data['content'] if map_data['content'] is not mapping %}
216+
{{ content_line['value'] }} {{ content_line['new_value'] }};
217+
{% else %}
218+
{{ map_data['content']['value'] }} {{ map_data['content']['new_value'] }};
219+
{% endfor %}
220+
{% endif %}
221+
}
222+
{% endif %}
223+
{% endfor %}
224+
{% endif %}
225+
226+
{% endmacro %}
227+
196228
{# NGINX HTTP Rewrite -- ngx_http_rewrite_module #}
197229
{% macro rewrite(rewrite) %}
198230
{% if rewrite['return'] is defined %}{# 'return' directive is not available in the 'http' context #}
@@ -221,6 +253,22 @@ uninitialized_variable_warn {{ rewrite['uninitialized_variable_warn'] | ternary(
221253

222254
{% endmacro %}
223255

256+
{# NGINX HTTP Split Clients -- ngx_http_split_clients_module #}
257+
{% macro split_clients(split_clients) %}{# 'split_clients' directive is only available in the 'http' context #}
258+
{% if split_clients['string'] is defined and split_clients['variable'] is defined %}
259+
split_clients {{ split_clients['string'] }} {{ split_clients['variable'] }} {
260+
{% if split_clients['content'] is defined %}
261+
{% for content_line in split_clients['content'] if split_clients['content'] is not mapping %}
262+
{{ content_line['percentage'] }} {{ content_line['value'] }};
263+
{% else %}
264+
{{ split_clients['content']['percentage'] }} {{ split_clients['content']['value'] }};
265+
{% endfor %}
266+
{% endif %}
267+
}
268+
{% endif %}
269+
270+
{% endmacro %}
271+
224272
{# NGINX HTTP Sub Filter -- ngx_http_sub_module #}
225273
{% macro sub_filter(sub_filter) %}
226274
{% if sub_filter['sub_filters'] is defined %}

0 commit comments

Comments
 (0)