仅允许某些国家的流量访问特定页面

dev*_*jim 9 system-administration nginx geoip server

--with-http_geoip_module用于识别流量.有些页面我只想让某个国家访问.这是配置:

对于 http

 http{
    geoip_country  /usr/share/GeoIP/GeoIP.dat; # the country IP database
    map $geoip_country_code $allowed_country {
            default 0;
            US 1;
            UK 1;
            HK 1; 

     }

  }
Run Code Online (Sandbox Code Playgroud)

location指令:

location = /testing {
            if ($allowed_country = 0) {

                    return 301 ;
              }
    }
Run Code Online (Sandbox Code Playgroud)

问题是当我使用US/ HKIP时,我收到404错误.我做错了什么?

UPDATE

这是我的完整conf档案:

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

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

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

geoip_country  /usr/share/GeoIP/GeoIP.dat; # the country IP database

map $geoip_country_code $allowed_country {
        default 0;
        US 1;
        UK 1;
        HK 1; 

 }
sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

index   index.html index.htm;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
    listen       80 default_server;
    server_name  localhost;
    root         /var/www/html;

    include /etc/nginx/default.d/*.conf;

    location / {
        index index.php index.cgi index.pl index.html index.xhtml index.htm index.shtml;
        try_files $uri $uri/ /index.php?$args;
    }

    # redirect server error pages to the static page /40x.html
    #
    error_page  404              /404.html;
    location = /40x.html {
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    }

    #ban specifc country
    location = /testing {
        if ($allowed_country = 0) {

                return 301 ;
          }
     }


    location ~ \.php$ {
        ### SET GEOIP Variables ###
        fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
        fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
        fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;

        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
        return 404;

        }
            try_files $uri $uri/ /index.php?$args;
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi.conf;
            include conf/site.conf;
    } 

}
 }
Run Code Online (Sandbox Code Playgroud)

基本上我想只从访客US,UKHK访问/testing页面.

NET*_*ing 22

我们从头开始吧.由于您没有说明您的操作系统是什么,所以下面的所有步骤都将针对Debian/Ubuntu和CentOS/Fedora/RHEL分开.

1.连接到服务器:

首先,通过终端/控制台(在linux - ssh username@server_ip)或Putty(在windows中)连接到您的服务器.


2.验证GEOIP模块:

因为你已经安装了NGINX,检查它是否用以下代码编译HttpGeoipModule:

CentOS/Fedora/RHEL和Debian/Ubuntu:

 nginx -V
Run Code Online (Sandbox Code Playgroud)

然后试着找--with-http_geoip_module.如果它存在,那么你可以继续,否则它意味着你没有编译NGINX GeoIP Module.


3.安装GEOIP数据库:

于Debian/Ubuntu:

sudo apt-get install geoip-database libgeoip1
Run Code Online (Sandbox Code Playgroud)

CentOS的/ Fedora的/ RHEL:

它位于EPEL存储库中,因此您应该首先启用它:

CENTOS 4:

32位:

rpm -Uvh http://download.fedoraproject.org/pub/epel/4/i386/epel-release-4-10.noarch.rpm
rpm –Uvh http://rpms.famillecollet.com/enterprise/remi-release-4.rpm
Run Code Online (Sandbox Code Playgroud)

64位:

rpm -Uvh http://download.fedoraproject.org/pub/epel/4/x86_64/epel-release-4-10.noarch.rpm
rpm –Uvh http://rpms.famillecollet.com/enterprise/remi-release-4.rpm
Run Code Online (Sandbox Code Playgroud)

CENTOS 5:

32位:

rpm -Uvh http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm –Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
Run Code Online (Sandbox Code Playgroud)

64位:

rpm -Uvh http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
rpm –Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
Run Code Online (Sandbox Code Playgroud)

CENTOS 6:

32位:

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 
Run Code Online (Sandbox Code Playgroud)

64位:

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 
Run Code Online (Sandbox Code Playgroud)

CENTOS 7:

64位:

rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm 
Run Code Online (Sandbox Code Playgroud)

然后:

yum install geoip geoip-devel -y
Run Code Online (Sandbox Code Playgroud)

4.更新GeoIP数据库

安装GeoIP模块后,数据库将被存储,/usr/share/GeoIP/GeoIP.dat但可能已过时.那么,让我们更新:

mv /usr/share/GeoIP/GeoIP.dat /usr/share/GeoIP/GeoIP.dat_bk

cd /usr/share/GeoIP/

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

gunzip GeoIP.dat.gz
Run Code Online (Sandbox Code Playgroud)

或者,您也可以手动下载数据库http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz,在计算机上解压缩并上传/usr/share/GeoIP/GeoIP.dat.GeoIP.dat如果你想在这里做,请不要忘记备份旧的.


5.使用GeoIP配置NGINX

打开/etc/nginx/nginx.conf(Ubuntu/Debian)或/etc/nginx/conf/nginx.conf(CentOS/Fedora/RHEL)并将其置于内部http {},之前include:

geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
    default no;
    US yes;
    UK yes;
    HK yes;
}
Run Code Online (Sandbox Code Playgroud)

这不会阻止国家.我们只设置$ allowed_country.

现在,请打开你virtualhost的configure(/etc/nginx/conf.d/YOURDOMAINHERE.conf) - 把它放在里面server {}:

location /testing/ {
    if ($allowed_country = no) {
        return 403;
    }
}
Run Code Online (Sandbox Code Playgroud)

/testing/ 是您的网站路径,可从美国,英国和香港访问.


6.重新启动NGINX

/etc/init.d/nginx reload
Run Code Online (Sandbox Code Playgroud)

它在CentOS和Debian VPS上都经过测试,它正在运行.

希望这会帮助你.