Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions contrib/compose/nginx_medium_rewrite_urls.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,29 @@ map $uri $needs_conversion {
default "no";
}

# Set up a JSON log format for better structured logging.
log_format json escape=json
'{'
'"timestamp": "$time_iso8601",'
'"client_ip": "$remote_addr",'
'"request_id": "$request_id",'
'"http_method": "$request_method",'
'"http_path": "$uri",'
'"protocol": "$server_protocol",'
'"user_agent": "$http_user_agent",'
'"referer": "$http_referer",'
'"status_code": $status,'
'"bytes_sent": $body_bytes_sent,'
'"request_time_secs": $request_time'
'}';

server {
listen 8069;
server_name localhost;

access_log logs/access.log json;
error_log logs/error.log;

client_max_body_size 100M;

# Remove the random ID that Medium appends to post URLs. This effectively does the same redirect recommended by Ghost here:
Expand Down
20 changes: 18 additions & 2 deletions nginx.conf.sigil
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,28 @@ map $uri $needs_conversion {
default "no";
}

# Set up a JSON log format for better structured logging.
log_format json escape=json
'{'
'"timestamp": "$time_iso8601",'
'"client_ip": "$remote_addr",'
'"request_id": "$request_id",'
'"http_method": "$request_method",'
'"http_path": "$uri",'
'"protocol": "$server_protocol",'
'"user_agent": "$http_user_agent",'
'"referer": "$http_referer",'
'"status_code": $status,'
'"bytes_sent": $body_bytes_sent,'
'"request_time_secs": $request_time'
'}';

server {
listen [::]:80;
listen 80;
server_name {{ .NOSSL_SERVER_NAME }};

access_log /var/log/nginx/{{ .APP }}-access.log;
access_log /var/log/nginx/{{ .APP }}-access.log json;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid mixed access log formats in same file

This server block now writes JSON to /var/log/nginx/{{ .APP }}-access.log, but the HTTPS server block below still writes to the same file without the json format. In deployments where HTTPS handles most traffic, the access log will therefore mix JSON and default log lines, which breaks JSON-only parsing (e.g., in Grafana) or drops entries. If the goal is to switch to JSON logs, the 443 access_log should use the same json format or a separate file.

Useful? React with 👍 / 👎.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OMG! This is actually a good catch. I forgot to change the format in the HTTPS block.

error_log /var/log/nginx/{{ .APP }}-error.log;
underscores_in_headers off;

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

access_log /var/log/nginx/{{ .APP }}-access.log;
access_log /var/log/nginx/{{ .APP }}-access.log json;
error_log /var/log/nginx/{{ .APP }}-error.log;

ssl_certificate {{ .APP_SSL_PATH }}/server.crt;
Expand Down