Hardening an nginx server is half the job. The other half is noticing when something goes wrong: a burst of failed logins, a scanner walking through your paths, a config change nobody announced, an upstream that starts throwing errors. This guide covers practical nginx security monitoring for servers you run: which logs to enable and how to structure them, what deserves an alert, how to watch the config itself, and a short review routine. It assumes a standard Linux install and open-source nginx.

Step 1: make nginx security logs useful

The default combined log format is fine for humans but awkward for tools. A JSON format makes every field searchable in a SIEM or with jq. The escape=json parameter of log_format handles quoting for you:

log_format json_sec escape=json '{"time":"$time_iso8601",' '"client":"$remote_addr",' '"host":"$host",' '"method":"$request_method",' '"uri":"$uri",' '"status":$status,' '"bytes":$body_bytes_sent,' '"rt":$request_time,' '"upstream_rt":"$upstream_response_time",' '"tls":"$ssl_protocol",' '"ua":"$http_user_agent",' '"req_id":"$request_id"}'; access_log /var/log/nginx/access.json json_sec; 

A few choices worth explaining:

  • $uri logs the normalised path without the query string. Query strings often carry tokens, emails or reset codes, so log them only if you need them and protect the logs accordingly.
  • $request_id gives every request a unique ID. Pass it upstream (for example in an X-Request-ID header) so you can follow one request through nginx and your application.
  • $ssl_protocol shows which TLS versions clients actually use, which helps when you plan to drop old ones.
  • Behind a load balancer or CDN, make sure the realip module trusts only your proxy's addresses, or $remote_addr will either show the proxy or an address the client chose.

The error log

The error log records denied requests, oversized bodies, TLS handshake failures and upstream problems. The default level is error; warn adds useful context without much noise:

error_log /var/log/nginx/error.log warn; 

Messages worth knowing by sight include "access forbidden by rule" (an allow/deny rule matched), "client intended to send too large body" (a request hit client_max_body_size) and "upstream timed out" (your application did not answer in time).

Rate-limit events

If you use limit_req or limit_conn, make rejections easy to spot. By default nginx answers with 503, which blends in with real outages. Return 429 instead and log rejections at a level you keep:

limit_req_status 429; limit_req_log_level warn; limit_conn_status 429; 

Step 2: ship the logs off the server

Logs that live only on the web server disappear when the disk fills, the server is rebuilt, or someone with access cleans up after themselves. nginx can send access and error logs straight to syslog:

access_log syslog:server=10.0.0.5:514,tag=nginx_access json_sec; error_log syslog:server=10.0.0.5:514,tag=nginx_error warn; 

You can keep the local file too by listing both access_log lines. Plain syslog over UDP is not encrypted, so for anything leaving a private network, ship through a local agent (rsyslog, Vector, Fluent Bit, the Elastic or Splunk forwarders) that supports TLS. Set a retention period: IP addresses in logs are personal data in many jurisdictions, so check your local rules.

Step 3: decide what to alert on

Alerting on every 404 teaches people to ignore alerts. Alert on changes in patterns instead. A practical starting set:

Signal Example rule Why it matters
Auth failures 401/403 on login paths above a baseline, per client Password guessing or broken clients
Rate-limit hits Many 429s from one source or on one route Abuse, or limits set too tight
Sensitive path requests Hits on /.git/, /.env, backup file names Someone looking for exposed files; confirm they return 404
5xx spike 502/504 rate above normal Upstream failure or resource exhaustion
Unusual methods Methods your app does not use Probing or misrouted traffic
Large responses Response size far above normal for a route Possible data exposure or a bug
Config change nginx files modified or reloaded Changes should match a ticket

Most of these are thresholds over a time window, which any SIEM or log platform can do. On a single server, fail2ban ships filters such as nginx-limit-req and nginx-http-auth that read the error log and can block repeat offenders at the firewall.

Step 4: nginx audit logging for the config itself

Many incidents start with a change, not an attack: a location block that suddenly exposes a directory, or a header removed by mistake. Watch the configuration directory with auditd on Linux:

sudo auditctl -w /etc/nginx/ -p wa -k nginx_config sudo ausearch -k nginx_config --start today 

Add the rule to /etc/audit/rules.d/ to keep it across reboots. Better still, keep the config in Git and deploy it from there, so every change has an author and a diff. Check what is actually running with nginx -T, which prints the full effective configuration, and compare it with the repository from time to time.

Step 5: a weekly 15-minute review

Alerts catch spikes; a short review catches slow drifts. With JSON logs, jq answers most questions:

jq -r 'select(.status == 404) | .uri' /var/log/nginx/access.json \ | sort | uniq -c | sort -rn | head -20 jq -r 'select(.status >= 500) | "\(.status) \(.uri)"' /var/log/nginx/access.json \ | sort | uniq -c | sort -rn | head 

Each week, look at:

  1. Top 404 paths. Attempts on files that should not exist are normal background noise; a 200 on one of them is not.
  2. Top 5xx routes. Errors often point at code that also mishandles input.
  3. Rate-limit rejections. Are limits stopping abuse, or blocking real users?
  4. TLS versions in use. Is anything still using a protocol you plan to retire?
  5. Config changes. Does every change in the audit log match a planned change?
  6. Log health. Are logs still arriving at the central store, and is the disk healthy?

Monitoring from the outside too

Logs show what reaches your server. They do not show what the server looks like from the internet: an expiring certificate, a new virtual host someone published, or a missing security header. Pair log monitoring with regular authorised external checks. CerberusGuard watches the outside view for the domains in your signed scan scope: it discovers new hosts through DNS and certificate transparency, flags expiring or untrusted certificates, and runs web scans with Nuclei and OWASP ZAP. Findings can be sent to your SIEM over syslog (CEF or LEEF 2.0), Splunk HEC or signed webhooks, next to your nginx logs. Keep an eye on your exposure with CerberusGuard.

Originally published at CerberusGuard blog.