Skip to content

Commit a749a49

Browse files
authored
Implement RealIP module (#240)
1 parent fdf15e2 commit a749a49

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +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.
13+
* Implement the `map`, `realip` and `split_clients` modules into the `http` core template.
1414
* Streamline configuring SELinux.
1515

1616
BUG FIXES:

defaults/main/template.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ nginx_config_http_template:
641641
content: # Dictionary or list of dictionaries
642642
- value: default
643643
new_value: 0
644+
realip: # Configure RealIP directives
645+
set_real_ip_from: 0.0.0.0
646+
real_ip_header: X-Real-IP
647+
real_ip_recursive: false # Boolean
644648
rewrite: # Configure rewrite directives
645649
return: # Can also be set to a return URL or code directly -- Not available in the 'http' context
646650
code: 200 # Required -- You have to set either 'code' or 'url'

molecule/default/converge.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@
502502
new_value: 0
503503
- value: '"~jndi:ldap"'
504504
new_value: 1
505+
realip:
506+
set_real_ip_from: 0.0.0.0
507+
real_ip_header: X-Real-IP
508+
real_ip_recursive: false
505509
rewrite:
506510
log: false
507511
uninitialized_variable_warn: false

molecule/plus/converge.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@
296296
new_value: 0
297297
- value: '"~jndi:ldap"'
298298
new_value: 1
299+
realip:
300+
set_real_ip_from: 0.0.0.0
301+
real_ip_header: X-Real-IP
302+
real_ip_recursive: false
299303
rewrite:
300304
log: false
301305
uninitialized_variable_warn: false

templates/http/default.conf.j2

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
{% from 'http/modules.j2' import map with context %}
8585
{{ map(item['config']['map']) }}
8686
{%- endif %}
87+
{% if item['config']['realip'] is defined %}
88+
{% from 'http/modules.j2' import realip with context %}
89+
{{ realip(item['config']['realip']) }}
90+
{%- endif %}
8791
{% if item['config']['rewrite'] is defined %}
8892
{% from 'http/modules.j2' import rewrite with context %}
8993
{{ rewrite(item['config']['rewrite']) }}
@@ -218,6 +222,12 @@ server {
218222
{{ log(server['log']) }}
219223
{%- endfilter %}
220224
{% endif %}
225+
{% if server['realip'] is defined %}
226+
{% from 'http/modules.j2' import realip with context %}
227+
{% filter indent(4) %}
228+
{{ realip(server['realip']) }}
229+
{%- endfilter %}
230+
{% endif %}
221231
{% if server['rewrite'] is defined %}
222232
{% from 'http/modules.j2' import rewrite with context %}
223233
{% filter indent(4) %}
@@ -348,6 +358,12 @@ server {
348358
{{ log(location['log']) }}
349359
{%- endfilter %}
350360
{% endif %}
361+
{% if location['realip'] is defined %}
362+
{% from 'http/modules.j2' import realip with context %}
363+
{% filter indent(8) %}
364+
{{ realip(location['realip']) }}
365+
{%- endfilter %}
366+
{% endif %}
351367
{% if location['rewrite'] is defined %}
352368
{% from 'http/modules.j2' import rewrite with context %}
353369
{% filter indent(8) %}

templates/http/modules.j2

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ map {{ map_data['string'] }} {{ map_data['variable'] }} {
225225

226226
{% endmacro %}
227227

228+
{# NGINX HTTP RealIP -- ngx_http_realip_module #}
229+
{% macro realip(realip) %}
230+
{% if realip['set_real_ip_from'] is defined %}
231+
set_real_ip_from {{ realip['set_real_ip_from'] }};
232+
{% endif %}
233+
{% if realip['real_ip_header'] is defined %}
234+
real_ip_header {{ realip['real_ip_header'] }};
235+
{% endif %}
236+
{% if realip['real_ip_recursive'] is defined and realip['real_ip_recursive'] is boolean %}
237+
real_ip_recursive {{ realip['real_ip_recursive'] | ternary('on', 'off') }};
238+
{% endif %}
239+
240+
{% endmacro %}
241+
228242
{# NGINX HTTP Rewrite -- ngx_http_rewrite_module #}
229243
{% macro rewrite(rewrite) %}
230244
{% if rewrite['return'] is defined %}{# 'return' directive is not available in the 'http' context #}

0 commit comments

Comments
 (0)