Skip to content

Commit c6c1336

Browse files
authored
Add templating options to logs (#169)
Resolves #152
1 parent 3b0e39f commit c6c1336

File tree

5 files changed

+67
-13
lines changed

5 files changed

+67
-13
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,21 @@ nginx_main_template:
336336
user: nginx
337337
worker_processes: auto
338338
#worker_rlimit_nofile: 1024
339-
error_level: warn
339+
error_log:
340+
location: /var/log/nginx/error.log
341+
level: warn
340342
worker_connections: 1024
341343
http_enable: true
342344
http_settings:
345+
access_log_format:
346+
- name: main
347+
format: |-
348+
'$remote_addr - $remote_user [$time_local] "$request" '
349+
'$status $body_bytes_sent "$http_referer" '
350+
'"$http_user_agent" "$http_x_forwarded_for"'
351+
access_log_location:
352+
- name: main
353+
location: /var/log/nginx/access.log
343354
keepalive_timeout: 65
344355
cache: false
345356
rate_limit: false
@@ -368,6 +379,12 @@ nginx_http_template:
368379
server_name: localhost
369380
include_files: []
370381
error_page: /usr/share/nginx/html
382+
access_log:
383+
- name: main
384+
location: /var/log/nginx/access.log
385+
error_log:
386+
location: /var/log/nginx/error.log
387+
level: warn
371388
root: /usr/share/nginx/html
372389
https_redirect: false
373390
autoindex: false
@@ -472,9 +489,9 @@ nginx_http_template:
472489
#rewrite: /foo(.*) /$1 break
473490
#proxy_pass_request_body: off
474491
#allows:
475-
# - 192.168.1.0/24
492+
# - 192.168.1.0/24
476493
#denies:
477-
# - all
494+
# - all
478495
proxy_set_header:
479496
header_host:
480497
name: Host
@@ -566,9 +583,6 @@ nginx_http_template:
566583
location: /
567584
code: 301
568585
value: http://$host$request_uri
569-
return404:
570-
location: /setup
571-
code: 404
572586

573587
# Enable NGINX status data.
574588
# Will enable 'stub_status' in NGINX Open Source and 'status' in NGINX Plus.

defaults/main.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,21 @@ nginx_main_template:
147147
user: nginx
148148
worker_processes: auto
149149
#worker_rlimit_nofile: 1024
150-
error_level: warn
150+
error_log:
151+
location: /var/log/nginx/error.log
152+
level: warn
151153
worker_connections: 1024
152154
http_enable: true
153155
http_settings:
156+
access_log_format:
157+
- name: main
158+
format: |-
159+
'$remote_addr - $remote_user [$time_local] "$request" '
160+
'$status $body_bytes_sent "$http_referer" '
161+
'"$http_user_agent" "$http_x_forwarded_for"'
162+
access_log_location:
163+
- name: main
164+
location: /var/log/nginx/access.log
154165
keepalive_timeout: 65
155166
cache: false
156167
rate_limit: false
@@ -179,6 +190,12 @@ nginx_http_template:
179190
server_name: localhost
180191
include_files: []
181192
error_page: /usr/share/nginx/html
193+
access_log:
194+
- name: main
195+
location: /var/log/nginx/access.log
196+
error_log:
197+
location: /var/log/nginx/error.log
198+
level: warn
182199
root: /usr/share/nginx/html
183200
https_redirect: false
184201
autoindex: false

molecule/template_module/playbook.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@
1313
conf_file_location: /etc/nginx/
1414
user: nginx
1515
worker_processes: auto
16-
error_level: warn
16+
error_log:
17+
location: /var/log/nginx/error.log
18+
level: warn
1719
worker_connections: 1024
1820
http_enable: true
1921
http_settings:
22+
access_log_format:
23+
- name: main
24+
format: |
25+
'$remote_addr - $remote_user [$time_local] "$request" '
26+
'$status $body_bytes_sent "$http_referer" '
27+
'"$http_user_agent" "$http_x_forwarded_for"'
28+
access_log_location:
29+
- name: main
30+
location: /var/log/nginx/access.log
2031
keepalive_timeout: 65
2132
cache: false
2233
rate_limit: false

templates/http/default.conf.j2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,14 @@ server {
380380
root {{ item.value.error_page }};
381381
}
382382
{% endif %}
383+
384+
{% if item.value.access_log is defined %}
385+
{% for access_log in item.value.access_log %}
386+
access_log {{ access_log.location }} {{ access_log.name }};
387+
{% endfor %}
388+
{% endif %}
389+
{% if item.value.error_log is defined %}
390+
error_log {{ item.value.error_log.location }} {{ item.value.error_log.level }};
391+
{% endif %}
392+
383393
}

templates/nginx.conf.j2

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ worker_processes {{ nginx_main_template.worker_processes }};
3838
worker_rlimit_nofile {{ nginx_main_template.worker_rlimit_nofile }};
3939
{% endif %}
4040

41-
error_log /var/log/nginx/error.log {{ nginx_main_template.error_level }};
41+
error_log {{ nginx_main_template.error_log.location }} {{ nginx_main_template.error_log.level }};
4242
pid /var/run/nginx.pid;
4343

4444

@@ -51,11 +51,13 @@ http {
5151
include /etc/nginx/mime.types;
5252
default_type application/octet-stream;
5353

54-
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
55-
'$status $body_bytes_sent "$http_referer" '
56-
'"$http_user_agent" "$http_x_forwarded_for"';
54+
{% for access_log in nginx_main_template.http_settings.access_log_format %}
55+
log_format {{ access_log.name }} {{ access_log.format }};
56+
{% endfor %}
5757

58-
access_log /var/log/nginx/access.log main;
58+
{% for access_log in nginx_main_template.http_settings.access_log_location %}
59+
access_log {{ access_log.location }} {{ access_log.name }};
60+
{% endfor %}
5961

6062
sendfile on;
6163
#tcp_nopush on;

0 commit comments

Comments
 (0)