Use map, map $request $allowonly { default 1; ~*addmedia\.upload([a-zA-Z0-9-_=&]) 0; 1; } inside location {} if ($allowonly) { return 404; } Untested but should give you enough to test with. Posted at Nginx Forum: http://forum.nginx.org/read.php?2,256336,256337#msg-256337