对网页访问nginx做地域限制:
对国家地区或者是城市的限制通过GeoIP模块进行处理
1. 先查看下nginx是否编辑有GeoIP模块:nginx -V 在输出界面看是否有--with-http_geoip_module
若没有 yum -y install geoip-devel GeoIP数据库会被安装在 /usr/share/GeoIP/GeoIP.dat
也可以从http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz 这里下载最新的GeoIP数据库文件。
2. 对国家进行限制: 参考:http://www.dnsdizhi.com/post-175.html
在nginx的配置文件http{}里面添加如下配置:
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default yes;
FK no;
FM no;
EH no;
}
####上面意思,除了FK,FM,EH三个地区的其他地区都可以访问
####只允许部分地区用户访问
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default no;
FK yes;
FM yes;
EH yes;
}
###上面是设置了一个变量allowed_country,要想完全实现限制,还需要在server {} 段添加内容
server {
if ($allowed_country = no) {
return 403;
}
}
###也可以对某个url进行限制:
location /special {
if ($allowed_country = no) {
return 403;
}
}
3. 对城市进行限制 参考:http://www.linuxhub.org/?p=3854
geoip_city 在http段配置
语法:geoip_city path/to/db.dat (geoip_city /usr/local/nginx/conf/GeoLiteCity.dat;)
参考变量:
$geoip_country_code - 两个字母的国家代码,如:”RU”, “US”。
$geoip_country_code3 - 三个字母的国家代码,如:”RUS”, “USA”。
$geoip_country_name - 国家的完整名称,如:”Russian Federation”, “United States”(如果可用)。
$geoip_region - 地区的名称(类似于省,地区,州,行政区,联邦土地等),如:”30”。 30代码就是广州的意思
$geoip_city - 城市名称,如”Guangzhou”, “ShangHai”(如果可用)。
$geoip_postal_code - 邮政编码。
$geoip_city_continent_code。
$geoip_latitude - 所在维度。
$geoip_longitude - 所在经度。
案例:
http {
geoip_country /usr/share/GeoIP/GeoIP.dat;
geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
map $geoip_city $allow_city{
default no;
hubei yes;
}
}
server {
listen 80;
server_name hb.weiyingjia.org;
location / {
root /server/www/bargain_hb/frontend/web;
index index.php;
if ($allow_city = no) {
return 401;
}
}
}
也可以在server段配置多条:
if ( $geoip_region = "22" ) {
#beijing
return 403;
}
if ( $geoip_region = "25" ) {
#shandong
return 403;
}
案例:
####配置 中国用户访问其他目录 nginx,在相关地方加上如下的配置就可以了:
# vi /etc/nginx/nginx.conf
http {
...
geoip_country /home/vpsee/GeoIP.dat;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
...
}
server {
...
location / {
root /home/vpsee/www;
if ($geoip_country_code = CN) {
root /home/vpsee/cn;
}
...
}
...
}
4. 添加日志格式,增加($geoip_country_name $geoip_region $geoip_city)
log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’
‘$geoip_country_name $geoip_region $geoip_city';
access_log /var/log/nginx/access.log access;
cat /var/log/nginx/access.log
113.105.222.45 - - [29/Nov/2017:11:04:51 +0800] "GET /index.php HTTP/1.0" 200 5 "-" "Wget/1.12 (linux-gnu)" -China 30 Guangzhou
120.24.1.157 - - [29/Nov/2017:11:06:50 +0800] "GET /index.php HTTP/1.0" 200 5 "-" "Wget/1.12 (linux-gnu)" -China 02 Hangzhou
5. 对nginx添加模块(非覆盖性安装) 参考:http://www.linuxhub.org/?p=3220
先nginx -V 查看已经安装的模块
需要添加模块:--add-module=/data/down/ngx_devel_kit-0.2.18 --add-module=/data/down/lua-nginx-module-0.9.18
重新编辑配置 ./configure --user=www --group=www --prefix=/usr/local/nginx......--add-module=/data/down/ngx_devel_kit-0.2.18 \
--add-module=/data/down/lua-nginx-module-0.9.18
编译但不安装 make
查看编译好的二进制文件:ldd objs/nginx |grep lua
替换nginx二进制文件:
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_back_2016.01.23
cp ./objs/nginx /usr/local/nginx/sbin/nginx
对国家地区或者是城市的限制通过GeoIP模块进行处理
1. 先查看下nginx是否编辑有GeoIP模块:nginx -V 在输出界面看是否有--with-http_geoip_module
若没有 yum -y install geoip-devel GeoIP数据库会被安装在 /usr/share/GeoIP/GeoIP.dat
也可以从http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz 这里下载最新的GeoIP数据库文件。
2. 对国家进行限制: 参考:http://www.dnsdizhi.com/post-175.html
在nginx的配置文件http{}里面添加如下配置:
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default yes;
FK no;
FM no;
EH no;
}
####上面意思,除了FK,FM,EH三个地区的其他地区都可以访问
####只允许部分地区用户访问
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default no;
FK yes;
FM yes;
EH yes;
}
###上面是设置了一个变量allowed_country,要想完全实现限制,还需要在server {} 段添加内容
server {
if ($allowed_country = no) {
return 403;
}
}
###也可以对某个url进行限制:
location /special {
if ($allowed_country = no) {
return 403;
}
}
3. 对城市进行限制 参考:http://www.linuxhub.org/?p=3854
geoip_city 在http段配置
语法:geoip_city path/to/db.dat (geoip_city /usr/local/nginx/conf/GeoLiteCity.dat;)
参考变量:
$geoip_country_code - 两个字母的国家代码,如:”RU”, “US”。
$geoip_country_code3 - 三个字母的国家代码,如:”RUS”, “USA”。
$geoip_country_name - 国家的完整名称,如:”Russian Federation”, “United States”(如果可用)。
$geoip_region - 地区的名称(类似于省,地区,州,行政区,联邦土地等),如:”30”。 30代码就是广州的意思
$geoip_city - 城市名称,如”Guangzhou”, “ShangHai”(如果可用)。
$geoip_postal_code - 邮政编码。
$geoip_city_continent_code。
$geoip_latitude - 所在维度。
$geoip_longitude - 所在经度。
案例:
http {
geoip_country /usr/share/GeoIP/GeoIP.dat;
geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
map $geoip_city $allow_city{
default no;
hubei yes;
}
}
server {
listen 80;
server_name hb.weiyingjia.org;
location / {
root /server/www/bargain_hb/frontend/web;
index index.php;
if ($allow_city = no) {
return 401;
}
}
}
也可以在server段配置多条:
if ( $geoip_region = "22" ) {
#beijing
return 403;
}
if ( $geoip_region = "25" ) {
#shandong
return 403;
}
案例:
####配置 中国用户访问其他目录 nginx,在相关地方加上如下的配置就可以了:
# vi /etc/nginx/nginx.conf
http {
...
geoip_country /home/vpsee/GeoIP.dat;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
...
}
server {
...
location / {
root /home/vpsee/www;
if ($geoip_country_code = CN) {
root /home/vpsee/cn;
}
...
}
...
}
4. 添加日志格式,增加($geoip_country_name $geoip_region $geoip_city)
log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’
‘$geoip_country_name $geoip_region $geoip_city';
access_log /var/log/nginx/access.log access;
cat /var/log/nginx/access.log
113.105.222.45 - - [29/Nov/2017:11:04:51 +0800] "GET /index.php HTTP/1.0" 200 5 "-" "Wget/1.12 (linux-gnu)" -China 30 Guangzhou
120.24.1.157 - - [29/Nov/2017:11:06:50 +0800] "GET /index.php HTTP/1.0" 200 5 "-" "Wget/1.12 (linux-gnu)" -China 02 Hangzhou
5. 对nginx添加模块(非覆盖性安装) 参考:http://www.linuxhub.org/?p=3220
先nginx -V 查看已经安装的模块
需要添加模块:--add-module=/data/down/ngx_devel_kit-0.2.18 --add-module=/data/down/lua-nginx-module-0.9.18
重新编辑配置 ./configure --user=www --group=www --prefix=/usr/local/nginx......--add-module=/data/down/ngx_devel_kit-0.2.18 \
--add-module=/data/down/lua-nginx-module-0.9.18
编译但不安装 make
查看编译好的二进制文件:ldd objs/nginx |grep lua
替换nginx二进制文件:
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_back_2016.01.23
cp ./objs/nginx /usr/local/nginx/sbin/nginx