Skip to content

Commit 500cd24

Browse files
authored
nginx variables for configuration and owner (#578)
Signed-off-by: [email protected] <[email protected]> Signed-off-by: [email protected] <[email protected]>
1 parent 3835b2a commit 500cd24

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

roles/nginx_hardening/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ It works with the following nginx-roles, including, but not limited to:
7575
- [nginx_dh_size][]
7676
- Default: `2048`
7777
- Description: Specifies the length of DH parameters for EDH ciphers.
78+
- [nginx_configuration_dir][]
79+
- default: "/etc/nginx"
80+
- Description: The main location for all nginx configuration files
81+
- [nginx_configuration_hardening_dir][]
82+
- default: "/etc/nginx"
83+
- Description: The location for the nginx hardening configuration file (Could be different e.g. when used in jails)
84+
- [nginx_owner_user][]
85+
- default: "root"
86+
- Description: The owner user of the nginx configuration files
87+
- [nginx_owner_group][]
88+
- default: "root"
89+
- Description: The owner group of the nginx configuration files
7890

7991
## Example Playbook
8092

roles/nginx_hardening/defaults/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ nginx_client_header_timeout: "10"
1111
nginx_send_timeout: "10"
1212
nginx_limit_conn_zone: "$binary_remote_addr zone=default:10m"
1313
nginx_limit_conn: "default 5"
14+
nginx_configuration_dir: "/etc/nginx"
15+
nginx_configuration_hardening_dir: "/etc/nginx"
16+
nginx_owner_user: "root"
17+
nginx_owner_group: "root"
1418
nginx_add_header:
1519
# avoid clickjacking
1620
- X-Frame-Options SAMEORIGIN

roles/nginx_hardening/tasks/main.yml

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,76 @@
22
- name: Create additional configuration
33
template:
44
src: "hardening.conf.j2"
5-
dest: "/etc/nginx/conf.d/90.hardening.conf"
5+
dest: "{{ nginx_configuration_dir }}/conf.d/90.hardening.conf"
66
mode: '0600'
7-
owner: "root"
8-
group: "root"
7+
owner: "{{ nginx_owner_user }}"
8+
group: "{{ nginx_owner_group }}"
99
notify: Restart nginx
1010

1111
- name: Change configuration in main nginx.conf
1212
lineinfile:
13-
dest: "/etc/nginx/nginx.conf"
13+
dest: "{{ nginx_configuration_dir }}/nginx.conf"
1414
regexp: '^\s*server_tokens'
1515
line: " server_tokens {{ nginx_server_tokens }};"
1616
insertafter: "http {"
1717
mode: '0640'
18-
owner: "root"
19-
group: "root"
18+
owner: "{{ nginx_owner_user }}"
19+
group: "{{ nginx_owner_group }}"
2020
notify: Restart nginx
2121

2222
- name: Change ssl_protocols in main nginx.conf
2323
lineinfile:
24-
dest: "/etc/nginx/nginx.conf"
24+
dest: "{{ nginx_configuration_dir }}/nginx.conf"
2525
regexp: '^\s*ssl_protocols'
2626
line: " ssl_protocols {{ nginx_ssl_protocols }};"
2727
insertafter: "http {"
2828
mode: '0640'
29-
owner: "root"
30-
group: "root"
29+
owner: "{{ nginx_owner_user }}"
30+
group: "{{ nginx_owner_group }}"
3131
notify: Restart nginx
3232

3333
- name: Change ssl_prefer_server_ciphers in main nginx.conf
3434
lineinfile:
35-
dest: "/etc/nginx/nginx.conf"
35+
dest: "{{ nginx_configuration_dir }}/nginx.conf"
3636
regexp: '^\s*ssl_prefer_server_ciphers'
3737
line: " ssl_prefer_server_ciphers {{ nginx_ssl_prefer_server_ciphers }};"
3838
insertafter: "http {"
3939
mode: '0640'
40-
owner: "root"
41-
group: "root"
40+
owner: "{{ nginx_owner_user }}"
41+
group: "{{ nginx_owner_group }}"
4242
notify: Restart nginx
4343

4444
- name: Change client_max_body_size in main nginx.conf
4545
lineinfile:
46-
dest: "/etc/nginx/nginx.conf"
46+
dest: "{{ nginx_configuration_dir }}/nginx.conf"
4747
regexp: '^\s*client_max_body_size'
4848
line: " client_max_body_size {{ nginx_client_max_body_size }};"
4949
insertafter: "http {"
5050
mode: '0640'
51-
owner: "root"
52-
group: "root"
51+
owner: "{{ nginx_owner_user }}"
52+
group: "{{ nginx_owner_group }}"
5353
notify: Restart nginx
5454

5555
- name: Change client_body_buffer_size in main nginx.conf
5656
lineinfile:
57-
dest: "/etc/nginx/nginx.conf"
57+
dest: "{{ nginx_configuration_dir }}/nginx.conf"
5858
regexp: '^\s*client_body_buffer_size'
5959
line: " client_body_buffer_size {{ nginx_client_body_buffer_size }};"
6060
insertafter: "http {"
6161
mode: '0640'
62-
owner: "root"
63-
group: "root"
62+
owner: "{{ nginx_owner_user }}"
63+
group: "{{ nginx_owner_group }}"
6464
notify: Restart nginx
6565

6666
- name: Change keepalive_timeout in main nginx.conf
6767
lineinfile:
68-
dest: "/etc/nginx/nginx.conf"
68+
dest: "{{ nginx_configuration_dir }}/nginx.conf"
6969
regexp: '^\s*keepalive_timeout'
7070
line: " keepalive_timeout {{ nginx_keepalive_timeout }};"
7171
insertafter: "http {"
7272
mode: '0640'
73-
owner: "root"
74-
group: "root"
73+
owner: "{{ nginx_owner_user }}"
74+
group: "{{ nginx_owner_group }}"
7575
notify: Restart nginx
7676

7777
- name: Remove default.conf
@@ -81,14 +81,14 @@
8181
when: nginx_remove_default_site
8282
notify: Restart nginx
8383
loop:
84-
- "/etc/nginx/conf.d/default.conf"
85-
- "/etc/nginx/sites-enabled/default"
84+
- "{{ nginx_configuration_dir }}/conf.d/default.conf"
85+
- "{{ nginx_configuration_dir }}/sites-enabled/default"
8686

8787
- name: Generate dh group
8888
openssl_dhparam:
89-
path: "/etc/nginx/dh{{ nginx_dh_size }}.pem"
89+
path: "{{ nginx_configuration_dir }}/dh{{ nginx_dh_size }}.pem"
9090
size: "{{ nginx_dh_size }}"
9191
mode: '0640'
92-
owner: "root"
93-
group: "root"
92+
owner: "{{ nginx_owner_user }}"
93+
group: "{{ nginx_owner_group }}"
9494
notify: Restart nginx

roles/nginx_hardening/templates/hardening.conf.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ limit_conn_zone {{ nginx_limit_conn_zone }};
1111
limit_conn {{ nginx_limit_conn }};
1212
ssl_ciphers '{{ nginx_ssl_ciphers }}';
1313
ssl_session_tickets {{ nginx_ssl_session_tickets }};
14-
ssl_dhparam /etc/nginx/dh{{ nginx_dh_size }}.pem;
14+
ssl_dhparam {{ nginx_configuration_hardening_dir }}/dh{{ nginx_dh_size }}.pem;
1515
{% for header in nginx_add_header %}
1616
add_header {{ header }};
1717
{% endfor %}

0 commit comments

Comments
 (0)