Mobile app version of vmapp.org
Login or Join
Goswami781

: Share a configuration among different "server" directive I'm trying to share a variable across different "server" blocks, http { set $bad_ua "..."; server { if ($http_user_agent

@Goswami781

Posted in: #Nginx

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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Goswami781

2 Comments

Sorted by latest first Latest Oldest Best

 

@Martha676

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;
}

10% popularity Vote Up Vote Down


 

@Gloria169

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.
wiki.nginx.org/HttpLuaModule blog.cloudflare.com/pushing-nginx-to-its-limit-with-lua openresty.org/#Download

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme