2

I'm trying to share a variable across different "server" blocks,

http {
   set $bad_ua "...";

   server {
       if ($http_user_agent ~ $bad_ua) {
            ...
       }
   }
}

But nginx says set directive not allowed here, so how am I supposed to share the $bad_ua variable?

3

I think that you want to filter traffic by user agent and do something like this, in order to prevent access on some specific user agents:

if ($http_user_agent ~* ^EirGrabber|PHPCrawl|PHP/5|^EmailSiphon|^$) {
    return 410;
}

Solution

Create a separate file that will contain the above code or any other filtering common code, and include this file on each server configuration.

server {
    server_name server2;
    include /etc/nginx/filters.conf;
}

server {
    server_name server2;
    include /etc/nginx/filters.conf;
}
| improve this answer | |
1

Does this variable ever change? If not it would be simpler to place in each server block. Otherwise you could use Lua to store the bad user agent in mysql (cached in memcached) and have each server block check for it there.

http://wiki.nginx.org/HttpLuaModule

http://blog.cloudflare.com/pushing-nginx-to-its-limit-with-lua

http://openresty.org/#Download

| improve this answer | |

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.