Using nginx args helper to search for a URL param

Using nginx args helper to quickly check for a param in a request

Recently I needed an efficient way to quickly shut down a network request if the request contained a parameter of ?search=blah. This was for one of our favourite clients.

The value of the parameter didn’t matter, if the parameter existed in the request, I wanted to return a 401 Unauthorised. This was to help with a spam problem.

The project uses an nginx proxy service, so I needed a way to check the request and return a 401 as soon as possible, I didn’t want to waste the proxy’s time and I certainly didn’t want to bother the server with anything.

Enter $arg_search

I had no idea nginx let’s you write if $arg_blah and if whatever is after the _ exists in the request the it will return true.

That means we can do something like the below, if ?search is in the request, we return a 403.

server {
  listen 8080;
  listen [::]:8080;

  server_name myawesomesite.com;

  # We want to forbid any traffic to /?search=blah
  if ($arg_search) {
    return 403;
  }
}

Here is the (one line) entry in nginx docs.


Recent posts View all

Web Dev

Tips For Effectively Managing Part Time Software Developers

Tips that will help you to build a productive software development team and manage it effectively

Ruby

Use to_sql without slashes

Do you hate slashes in your to_sql output? Then this post is for you!