: 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
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?
More posts by @Goswami781
2 Comments
Sorted by latest first Latest Oldest Best
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;
}
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
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.