I use Oauth2 password grant to generate access tokens for my clients. That means the user's password is sent as a query param:
example.com/oauth/token?grant_type=password&username=MyUsername&password=MyPassword
I use the following log_filter in order to mask the password for authentication attempts from the Nginx access_log:
http {
log_format hide_password '$remote_addr $remote_user [$time_local] "$request_without_password" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
...
server {
set $request_without_password $request;
if ($request_without_password ~ (.*)password=[^&]*(.*)) {
set $request_without_password $1password=****$2;
}
access_log /var/log/nginx/access.log hide_password;
....
}
}
I have now introduced rate limiting using Nginx's limit_req. All rate limited requests are logged to the Nginx error_log. The problem is that It does not seem possible to use a log_filter on the error_log, meaning a rate limited authentication request will be logged with the user's password visible, which of course is unacceptable. Since I want to keep track of rate limited requests, I don't want to disable the error_log.
Is there a way to log rate limited requests in Nginx without the password visible as a query param?