Skip to content

Commit 8fb5e8b

Browse files
authored
Merge pull request #11 from CodeForAfrica/use-json-format-for-nginx-logs
Switch to JSON format for Nginx logs
2 parents c31f75b + b0a39ae commit 8fb5e8b

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

contrib/compose/nginx_medium_rewrite_urls.conf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,29 @@ map $uri $needs_conversion {
3333
default "no";
3434
}
3535

36+
# Set up a JSON log format for better structured logging.
37+
log_format json escape=json
38+
'{'
39+
'"timestamp": "$time_iso8601",'
40+
'"client_ip": "$remote_addr",'
41+
'"request_id": "$request_id",'
42+
'"http_method": "$request_method",'
43+
'"http_path": "$uri",'
44+
'"protocol": "$server_protocol",'
45+
'"user_agent": "$http_user_agent",'
46+
'"referer": "$http_referer",'
47+
'"status_code": $status,'
48+
'"bytes_sent": $body_bytes_sent,'
49+
'"request_time_secs": $request_time'
50+
'}';
51+
3652
server {
3753
listen 8069;
3854
server_name localhost;
3955

56+
access_log logs/access.log json;
57+
error_log logs/error.log;
58+
4059
client_max_body_size 100M;
4160

4261
# Remove the random ID that Medium appends to post URLs. This effectively does the same redirect recommended by Ghost here:

nginx.conf.sigil

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,28 @@ map $uri $needs_conversion {
3838
default "no";
3939
}
4040

41+
# Set up a JSON log format for better structured logging.
42+
log_format json escape=json
43+
'{'
44+
'"timestamp": "$time_iso8601",'
45+
'"client_ip": "$remote_addr",'
46+
'"request_id": "$request_id",'
47+
'"http_method": "$request_method",'
48+
'"http_path": "$uri",'
49+
'"protocol": "$server_protocol",'
50+
'"user_agent": "$http_user_agent",'
51+
'"referer": "$http_referer",'
52+
'"status_code": $status,'
53+
'"bytes_sent": $body_bytes_sent,'
54+
'"request_time_secs": $request_time'
55+
'}';
56+
4157
server {
4258
listen [::]:80;
4359
listen 80;
4460
server_name {{ .NOSSL_SERVER_NAME }};
4561

46-
access_log /var/log/nginx/{{ .APP }}-access.log;
62+
access_log /var/log/nginx/{{ .APP }}-access.log json;
4763
error_log /var/log/nginx/{{ .APP }}-error.log;
4864
underscores_in_headers off;
4965

@@ -70,7 +86,7 @@ server {
7086
listen 443 ssl http2;
7187
{{ if .SSL_SERVER_NAME }}server_name {{ .SSL_SERVER_NAME }}; {{ end }}
7288

73-
access_log /var/log/nginx/{{ .APP }}-access.log;
89+
access_log /var/log/nginx/{{ .APP }}-access.log json;
7490
error_log /var/log/nginx/{{ .APP }}-error.log;
7591

7692
ssl_certificate {{ .APP_SSL_PATH }}/server.crt;

0 commit comments

Comments
 (0)