Home > database >  Sharing a nginx directive across sites
Sharing a nginx directive across sites

Time:04-26

I have around 30 sites in a server. I am trying to add an IP blocker to each of the site without altering the respective site file in sites_enabled.

ie. I need to share this block to all the websites in the server.

location / {
    if ($allowed_country = no) {
         return 418;         
    }
    try_files $uri $uri/ /index.php?$query_string;
}

So far all my research says that I cannot do this. Can someone please confirm it? is there anyway I can share it across sites and make sure it doesn't get overriden in the sites nginx configuration file?

CodePudding user response:

Have you already tried to add the if directive in server-block (before all those location statements) and break after that?

server {
    if ($allowed_country = no) {
         return 418;
         break; 
    }
}

if Directives in location are considered as evil due to nginx` strange declaration rules. They're doing most of the time strange things, so try to avoid it.

  • Related