Nginx – Reject requests with special unicode chars in Nginx

nginx

I'd like to block/deny all requests that have one or more non-ascii chars in it.

For instance, I'm getting requests like these:

/\ufffd?s=p&z=\ufffd&g=\ufffd&o=2

Where \ufffd is the "replacement character".

This is causing my web app to email me errors because my database isn't yet UTF-8 (long story short).

I know I should fix the problem, not hide it, but until I do, blocking all weird requests from getting to the application will prevent my mail box from filling with errors.

I also know that all URLs in this particular website do not contain non-ascii chars, so this quick fix won't break anything.

Thanks!

Best Answer

You can match such a url by using the replacement character itself in the location directive:

location /� {
  return 400; # Or whatever you would like to do.
}