如何在不使用if的情况下阻止某些国家(geoip2)的页面? [英] How to block a page for certain countries (geoip2) without if?

查看:186
本文介绍了如何在不使用if的情况下阻止某些国家(geoip2)的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在nginx中使用--with_geoip2_module模块(动态)不使用 if 但使用 map 来阻止所有国家/地区(除了所选国家和地区之外)选择,使其像这样工作:

How it is possible without using if but with map in nginx with the module --with_geoip2_module (dynamic) to block all countries except the chosen and all ip except the chosen, to make it work like this:

**if ($lan = yes) {
              set $allowed_country yes;
      }

if ($allowed_country = no) {
       return 503;
  }**

但没有 if

Centos 7

这是我的/etc/nginx/nginx.conf

It`s my /etc/nginx/nginx.conf

load_module modules/ngx_http_geoip2_module.so;

user nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

#block country

 geoip2 /etc/nginx/geoip/GeoLite2-Country/GeoLite2-Country.mmdb {

auto_reload 60m;
$geoip2_data_country_code country iso_code;
$geoip2_data_country_name country names en;
}  

geo $lan {
 default no;
 123.224.55.2 yes;

  }


map $geoip2_data_country_code $allowed_country {
    
    default no;
    UA yes;
    BG yes;
    RO yes;
}

   log_format geoip_main '$remote_addr - $geoip2_data_country_code - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent "$host" '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

access_log  /var/log/nginx/access.log geoip_main;

access_log /var/log/nginx/geoip_country.log geoip_main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;

access_log /var/log/nginx/geoip_country.log geoip_main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
 include include/etc/nginx/modules/*.so;

}

我非常感谢您的帮助!

推荐答案

NGINX官方文章

An official NGINX article says:


在位置上下文中,可能在内部完成的唯一100%安全的事情是:

The only 100% safe things which may be done inside if in a location context are:

return ...;
rewrite ... last;


所以您的 if(...){返回503 ; } 块是完全安全的。要排除第一个 if 块,请使用

So your if (...) { return 503; } block is completely safe. To exclude first one if block, use

geo $lan {
    default no;
    123.224.55.2 yes;
}
map $geoip2_data_country_code $allowed_country {
    default no;
    UA yes;
    BG yes;
    RO yes;
}
map $lan$allowed_country $deny {
    ~yes "";
    default 1;
}
...
server {
    ...
    if ($deny) {
        return 503;
    }
    ...
}

更新

此版本具有相同的功能,并且由于未使用正则表达式匹配,因此应稍快一些:

This version have the same functionality and should be slightly faster since it doesn't use a regex matching:

geo $lan {
    default  0;
    123.224.55.2  1;
    ...
}
map $geoip2_data_country_code $allowed_country {
    default  0;
    UA  1;
    BG  1;
    RO  1;
}
map $lan$allowed_country $deny {
    default  0;
    00  1;
}
...
server {
    ...
    if ($deny) {
        return 503;
    }
    ...
}

这篇关于如何在不使用if的情况下阻止某些国家(geoip2)的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆