5

I'm using nginx-1.6.2,2 and I'd like return error 410 for URL that matches /browse.php?u=http, so requests such as this will get 410:

162.218.208.16 - - [21/Nov/2014:12:35:40 -0500] "GET /browse.php?u=http%3A%2F%2Fwww.bing.com%2Fsearch%3Fq%3DBroke%2C%2BUSA%2Bfiletype%3Apdf%26first%3D0%26count%3D20%26format%3Drss&b=156&f=norefer HTTP/1.1" 403 570 "http://ww.thespacesurf.com/browse.php?u=http%3A%2F%2Fwww.bing.com%2Fsearch%3Fq%3DBroke%2C%2BUSA%2Bfiletype%3Apdf%26first%3D0%26count%3D20%26format%3Drss&b=156&f=norefer" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)"

I don't really need regex, so something like this, but it's not working:

location = /browse.php?u=http {
        return 410;
}

couple of days later, I grep -c for 410 in /var/log/nginx-access.log

$ bzip2 -cd /var/log/nginx-access.log.0.bz2 | grep -c ' 410 '
5665
$ 

and that made me feel warm and fuzzy) thanks again!

0

That's because location is /browse.php, not the one you specified.

There are several ways to do it. For example, something like this should do:

location = /browse.php {
        if ($query_string ~ ^u=http) {
                return 410;
        }
}

P.S.: or use ~* instead of ~ if u=http is case-insensitive.

4

You can't match the query string in the declaration of a location statement so you need something like this :

location = /browse.php {
    if ($arg_u ~ "^http") { 
        return 410;
    }
}
4
  • while I like your answer is better then @nochkin, I'm trying to stay away from regex, so I'll vote your answer up! Thanks again!
    – alexus
    Nov 21 '14 at 18:25
  • @alexus It won't match the example you gave so. Nov 21 '14 at 18:28
  • 1
    right, I was just testing it and I came up to that conclusion too.
    – alexus
    Nov 21 '14 at 18:32
  • Just for completeness, the $arg_u matches the query parameter u. nginx.org/en/docs/http/ngx_http_core_module.html#variables
    – Rick-777
    Jul 7 '15 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.