Redact ?token= from nginx access logs with map (tested)
Keep ?token= values out of nginx access logs with a map variable in log_format. Nine test requests show what still leaks, including the error log.
The default combined format logs $request, query string included. If links authenticate with ?token=…, the value lands in the access log, and on errors such as 404 in error.log too. A map that builds a redacted variable, used in log_format instead of $request, fixes the access log. Below, nine requests carry a different marker in each token slot (SECRET01 to SECRET10) so the leaks can be counted. If the goal is that secrets never reach the log and you do not need the query, consider [5] (drop the query) first: it was the only method that leaked nothing.
[1] Add a map to the http block that builds the redacted URI. Only the value after token= becomes REDACTED; other parameters stay. It runs twice to handle a repeated parameter.
http { map $request_uri $uri_mask1 { "~*^(?<p1>[^?]*\?(?:.*?&)??token=)[^&]*(?<s1>.*)$" "${p1}REDACTED${s1}"; default $request_uri; } map $uri_mask1 $uri_mask2 { "~*^(?<p2>[^?]*\?(?:.*?&)??token=[^&]*&(?:.*?&)??token=)[^&]*(?<s2>.*)$" "${p2}REDACTED${s2}"; default $uri_mask1; } log_format mask '$remote_addr [$time_local] "$request_method $uri_mask2 $server_protocol" $status'; access_log /var/log/nginx/access.log mask;}~* ignores case, so Token= matches too. The first map masks the value of the first token=, the second masks the second token= that follows it; the trailing ?? in (?:.*?&)?? makes each pattern take the earliest one. Neither checks whether the value starts with REDACTED, so a real value shaped like REDACTED-… is replaced too (skipping "already masked" spots with (?!REDACTED) would leave such a real value in the log). Per the documentation, named captures from a map regex can be used in the resulting value, and the value is computed when the variable is used.
[2] Drop $request from the format. It carries the original query, so log $request_method, the masked URI and $server_protocol separately. If you log $http_referer, build a second masked variable for it; in the test a ?token= inside the Referer was logged verbatim otherwise.
[3] Back up the configuration before editing, then test and reload. The access log format changes, so if a collector or analyzer reads that log, confirm it still parses the new format.
$ sudo cp -a /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak$ sudo nginx -t && sudo systemctl reload nginxTo roll back, run sudo cp -a /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf and the same nginx -t && systemctl reload nginx.
[4] Count the markers left behind. Each request used a different value, so grep -o lists exactly what leaked. The unmodified format kept all ten:
$ grep -o "SECRET[0-9][0-9]" plain.log | sort | tr "\n" " "; echoSECRET01 SECRET02 SECRET03 SECRET04 SECRET05 SECRET06 SECRET07 SECRET08 SECRET09 SECRET10The configuration above (mask) left two:
$ grep -o "SECRET[0-9][0-9]" mask.log | sort | tr "\n" " "; echoSECRET04 SECRET07| Request | Original format | map once | map twice | Query dropped |
|---|---|---|---|---|
/?token=SECRET01 | Leaked | Masked | Masked | Gone |
/index.html?a=1&token=SECRET02&b=2 | Leaked | Masked | Masked | Gone |
/?Token=SECRET03 | Leaked | Masked | Masked | Gone |
/?access_token=SECRET04 | Leaked | Leaked | Leaked | Gone |
/?token=SECRET05&token=SECRET06 | Both leaked | 05 leaked | Masked | Gone |
/?%74oken=SECRET07 | Leaked | Leaked | Leaked | Gone |
/missing?token=SECRET08 (404) | Leaked | Masked | Masked | Gone |
Referer …/cb?token=SECRET09 | Leaked | Masked (Referer map) | Referer not logged | Gone |
/?token=REDACTED-SECRET10 | Leaked | Masked | Masked | Gone |
access_token= is a different name, so leaking it is expected; widen token to (?:access_)?token if you need it masked. %74oken is t percent-encoded. $request_uri is the raw, undecoded line, so the regex does not match. A parameter repeated three or more times leaves the third value even with two passes.
[5] If you do not need the query, drop it entirely. That was the only method that left nothing in the access log. log_format only defines a format, so the access_log line must name noquery for it to take effect.
map $request_uri $uri_noquery { "~^(?<path>[^?]*)" $path;}log_format noquery '$remote_addr [$time_local] "$request_method $uri_noquery $server_protocol" $status';access_log /var/log/nginx/access.log noquery;Apply it with sudo nginx -t && sudo systemctl reload nginx as in [3].
$ grep -o "SECRET[0-9][0-9]" noquery.log | sort | tr "\n" " "; echo The empty line is the result. The trade-off: the log no longer tells you which parameters a request used.
[6] Treat the error log separately. Its format cannot be changed, and error lines end with the original request line. In the same test as the masked access log, the 404 request's value showed up in error.log:
2026/10/27 05:08:45 [error] 29#29: *7 open() "/usr/share/nginx/html/missing" failed (2: No such file or directory), client: 203.0.113.7, server: , request: "GET /missing?token=SECRET08 HTTP/1.1", host: "example.com"log_not_found off; turns off missing-file errors, and error.log had 0 lines in the second run. With it, broken links and wrong paths can no longer be traced in error.log (the 404 status still appears in the access log). To undo it, delete the line or set log_not_found on; (the default), then nginx -t and reload. Other error lines, such as upstream connection failures, carry the same request: field (reading 502 errors). Give the error log the same access control and retention as the access log.
[7] Logs already written stay as they are. The new format applies from the reload on. Size up what older and rotated files (access.log.1, *.gz) hold with zgrep -c 'token=' /var/log/nginx/access.log*; for tokens that were exposed, revoking them in the application is more reliable than deleting log files.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.