Skip to content

Commit

Permalink
feat: Enable nginx access logging in json format (#1086)
Browse files Browse the repository at this point in the history
* Enable json access logformat in nginx
* Add required variables to the main log format
* Update nginx chapter
  • Loading branch information
jometzner authored Mar 29, 2022
1 parent b432c11 commit 099af1f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .vscode/intershop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,5 @@ foobars
asdf
qwertz
appr

logformat
2 changes: 2 additions & 0 deletions docs/concepts/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ kb_sync_latest_only
## nginx

The nginx image providing [PageSpeed](https://www.modpagespeed.com/) and multi-channel configuration uses the default logging capabilities of [nginx](https://www.nginx.com/).
You can enable `json` formatted logging by passing environment variable `LOGFORMAT=json` to the container.
When no `LOGFORMAT` variable is passed the container uses `main` as its default format.

- [Configure Logging](https://docs.nginx.com/nginx/admin-guide/monitoring/logging/)
- [Debugging Nginx Configuration](https://easyengine.io/tutorials/nginx/debugging/)
Expand Down
1 change: 1 addition & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ENV DEVICE_DETECTION=on
ENV SSR=on
ENV CACHE_DURATION_NGINX_OK=10m
ENV CACHE_DURATION_NGINX_NF=60m
ENV LOGFORMAT=main

EXPOSE 80 443 9113

Expand Down
47 changes: 43 additions & 4 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,52 @@ events {
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format json escape=json '{'
'"time_local": "$time_local", '
'"core":'
'{'
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"request": "$request", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"http_referer": "$http_referer", '
'"http_user_agent": "$http_user_agent" ,'
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"request_method": "$request_method", '
'"request_time": "$request_time", '
'"host": "$host", '
'"server_name": "$server_name"'
'},'
'"ssl":'
'{'
'"ssl_cipher": "$ssl_cipher",'
'"ssl_client_cert": "$ssl_client_cert",'
'"ssl_client_fingerprint": "$ssl_client_fingerprint",'
'"ssl_client_i_dn": "$ssl_client_i_dn",'
'"ssl_client_raw_cert": "$ssl_client_raw_cert",'
'"ssl_client_s_dn": "$ssl_client_s_dn",'
'"ssl_client_serial": "$ssl_client_serial",'
'"ssl_client_verify": "$ssl_client_verify",'
'"ssl_protocol": "$ssl_protocol",'
'"ssl_server_name": "$ssl_server_name",'
'"ssl_session_id": "$ssl_session_id",'
'"ssl_session_reused": "$ssl_session_reused"'
'},'
'"upstream":'
'{'
'"address": "$upstream_addr", '
'"status": "$upstream_status", '
'"response_time": "$upstream_response_time", '
'"response_length": "$upstream_response_length", '
'"cache_status": "$upstream_cache_status"'
'}'
'}';

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx-access.log main;
access_log /dev/stdout;
'"$http_user_agent" "$http_x_forwarded_for" '
'$upstream_response_time "$upstream_cache_status"';

sendfile on;

Expand Down
3 changes: 3 additions & 0 deletions nginx/templates/multi-channel.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ server {
listen 80;
{{- end }}

access_log /var/log/nginx-access.log {{ getenv "LOGFORMAT" }};
access_log /dev/stdout {{ getenv "LOGFORMAT" }};

include /etc/nginx/conf.d/cache-blacklist.conf;


Expand Down

0 comments on commit 099af1f

Please sign in to comment.