海运的博客

Nginx模块GeoIP匹配处理IP所在国家、城市

发布时间:July 1, 2012 // 分类:Nginx // 5 Comments

Nginx可配合GeoIP模块定位IP所在物理位置并做相应处理,支持多个条件匹配:

#https://www.haiyun.me
$geoip_country_code #国家代码2位,如CN
$geoip_country_code3 #国家代码3位,如CHN
$geoip_country_name #国家完整名称,如China
$geoip_region #所在地区
$geoip_city #所在城市,如BeiJing
$geoip_postal_code #邮政编码
$geoip_city_continent_code #所在洲,如AS
$geoip_latitude #纬度
$geoip_longitude #经度

编译安装Nginx并添加GeoIP模块:

yum install geoip-devel #安装GeoIP解析库
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz 
cd nginx-1.0.15
 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_geoip_module
make 
make install

下载GeoIP城市国家数据库:

#https://www.haiyun.me
mkdir -p /usr/local/nginx/geoip
cd /usr/local/nginx/geoip
wget http://www.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoIP.dat.gz 
gunzip GeoLiteCity.dat.gz 

编辑Nginx配置文件加载GeoIP数据库:

http
    [...]
geoip_country  /usr/local/nginx/geoip/GeoIP.dat; #国家数据库
geoip_city     /usr/local/nginx/geoip/GeoLiteCity.dat; #城市数据库
[...]

如需Nginx传递变量给PHP,编辑fastcgi_params添加:

fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

应用示例,Nginx判断如果访问者IP所在国家为美国或中国,返回404错误

server
    [...]
                if ($geoip_country_code ~* (US|CN)) {
                #return 404;
                }
[...]

新建PHP程序测试GeoIP:

<html>
<head>
 <title>IP地址检测,我的IP地址是多少?</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
        $ipaddress = getenv(REMOTE_ADDR);
        echo "<br>您的代理IP地址是: ".$pipaddress. " (via $ipaddress) " ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "<br>您的IP地址是 : $ipaddress";
    }
  $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
  $geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3);
  $geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME);
  $geoip_region = getenv(GEOIP_REGION);
  $geoip_city = getenv(GEOIP_CITY);
  $geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
  $geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE);
  $geoip_latitude = getenv(GEOIP_LATITUDE);
  $geoip_longitude = getenv(GEOIP_LONGITUDE);
  echo "<br>国家 : $geoip_city_country_name ( $geoip_city_country_code3 , $geoip_city_country_code ) ";
  echo "<br>地区 :  $geoip_region";
  echo "<br>城市 :  $geoip_city ";
  echo "<br>邮政编码 :  $geoip_postal_code";
  echo "<br>所在洲 :  $geoip_city_continent_code";
  echo "<br>纬度 :  $geoip_latitude ";
  echo "<br>经度 :   $geoip_longitude ";

?>
</body>
</html>

Nginx与PHP变量传递fastcgi_params

发布时间:July 1, 2012 // 分类:Nginx,PHP // No Comments

Nginx配置Fastcgi解析PHP时会调用fastcgi_params配置文件来传递服务器变量,默认内容如下:

#参数设定       #传递为PHP变量名    #Nginx自有变量,可自定义
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

可以修改配置文件设定自定义变量传递到php-cgi端:

fastcgi_param  haiyun      "Hello haiun";

新建php文件通过$_SERVER[""]变量测试:

<?
echo $_SERVER["haiyun"];
?>
curl www.haiyun.me/test.php
Hello haiyun

13-12-25更新,不推荐使用这种方式传递变量,会增加连接开销,可修改PHP-FPM配置文件传递变量:

env[haiyun] = test

Nginx判断accept_language禁止英文浏览器/操作系统访问

发布时间:June 27, 2012 // 分类:Nginx // No Comments

HTTP协议发送请求时会附加accept_language,通过它可知道浏览器所支持的语言,一般也为操作系统默认的语言。
列举一些accept_language标识:

zh 中文
zh-cn 大陆
zh-tw 台湾
zh-hk 香港
en 英文

配置Nginx允许中文用户访问,拒绝其它语言用户访问。

if ( $http_accept_language ~* ^[^zh])
{
return 404; #非中文用户访问网站返回404
}

Curl测试:

curl -I -H "Accept-Language:en"  www.haiyun.me
HTTP/1.1 404 Not Found
Date: Wed, 20 Jun 2012 18:38:33 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

Apache/PHP/Mysql/Nginx编译参数查看

发布时间:June 20, 2012 // 分类:Apache,Nginx,PHP,数据库 // No Comments

查看Nginx编译参数:

/usr/local/nginx/sbin/nginx -V

查看Apache编译参数:

cat /usr/local/apache/build/config.nice

查看PHP编译参数:

/usr/local/php/bin/php -i |grep configure

查看Mysql编译参数:

cat /usr/local/mysql/bin/mysqlbug|grep configure

Nginx设置防止恶意域名指向IP

发布时间:June 2, 2012 // 分类:Nginx // No Comments

Nginx默认虚拟主机在用户通过IP访问,或者通过未设置的域名访问(比如有人把他自己的域名指向了你的ip)的时候生效,默认为第一个Server段的虚拟主机,可通过设置指定默认虚拟主机:

server {
listen     80 default; #default表示默认虚拟主机,未知域名或通过IP访问 
rewrite ^(.*) https://www.haiyun.me permanent; #301重定向到自己的网站
#return 404; #返回相应的状态码
#return 403;
#return 500;
}
分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...