提示:以下是本篇文章正文内容,下面案例可供参考
一、http_geoip_module使用场景
一、区别国内外作HTTP访问规则 二、区别国内城市地域作HTTP访问规则
二、使用步骤
1.使用脚本编译安装nginx,一键部署
#!/bin/bash
id xiongxue || useradd xiongxue -s /sbin/nologin
mkdir /xiongxue99 -p
cd /xiongxue99
wget http://nginx.org/download/nginx-1.21.1.tar.gz
tar xf nginx-1.21.1.tar.gz
cd nginx-1.21.1
if (( $? != 0));then
exit
fi
make -j 2
make install
echo "PATH=$PATH:/usr/local/scxiongxue99/sbin" >>/root/.bashrc
source /root/.bashrc
service firewalld stop
systemctl disable firewalld
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
chmod +x /etc/rc.d/rc.local
echo "/usr/local/scxiongxue99/sbin/nginx" >>/etc/rc.local
sed -i '/worker_processes/ s/1/2/' /usr/local/scxiongxue99/conf/nginx.conf
sed -i '/worker_connections/ s/1024/2048/' /usr/local/scxiongxue99/conf/nginx.conf
sed -i -r '36c \\tlisten 80;' /usr/local/scxiongxue99/conf/nginx.conf
sed -i -r '37c \\tserver_name www.xiongxue.com;' /usr/local/scxiongxue99/conf/nginx.conf
2.下载安装 MaxMind 的 GeoIP 库
下载链接:链接:https://pan.baidu.com/s/1aEogiu9clrJTNZQmFkI_Ag 提取码:123r 下载好后进行解压
[root@bnginx sc]
[root@bnginx sc]
Archive: geoip.zip
inflating: GeoIPCityv6.dat
inflating: GeoIPv6.dat
inflating: GeoLite2-City.mmdb
inflating: GeoLite2-Country.mmdb
inflating: GeoLiteASNum.dat
inflating: GeoLiteASNumv6.dat
inflating: GeoLiteCity.dat
inflating: GeoLiteCityv6.dat
inflating: GeoLiteCountry.dat
inflating: readme.txt
inflating: GeoIP.dat
inflating: GeoIPASNum.dat
inflating: GeoIPASNumv6.dat
inflating: GeoIPCity.dat
3.将之前解压的所需数据库放入nginx的配置文件目录中
[root@bnginx sc]
[root@bnginx sc]
4.修改nginx.conf文件的配置
http {
geoip_country /usr/local/scxiongxue99/conf/GeoIP.dat;
geoip_city /usr/local/scxiongxue99/conf/GeoLiteCity.dat;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.xiongxue.com;
if ($geoip_city != 'Changsha'){
return 403;
}
if ($geoip_country_code != 'CN'){
return 404;
}
location / {
root html;
index index.html index.htm;
}
5.语法检测及启动nginx
[root@bnginx conf]
nginx: the configuration file /usr/local/scxiongxue99/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/scxiongxue99/conf/nginx.conf test is successful
[root@bnginx conf]
[root@bnginx conf]
root 11955 0.0 2.2 69744 22476 ? Ss 19:40 0:00 nginx: master process /usr/local/scxiongxue99/sbin/nginx
xiongxue 11956 0.0 2.3 70620 23496 ? S 19:40 0:00 nginx: worker process
xiongxue 11957 0.0 2.3 70620 23496 ? S 19:40 0:00 nginx: worker process
root 11959 0.0 0.0 112824 984 pts/1 S+ 19:40 0:00 grep --color=autonginx
[root@bnginx conf]
6.在服务器上配置多个ip地址进行测试
[root@bnginx scxiongxue99]
[root@bnginx sc]
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@bnginx scxiongxue99]
[root@bnginx conf]
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.21.1</center>
</body>
</html>
[root@bnginx scxiongxue99]
[root@bnginx conf]
[root@bnginx conf]
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.1</center>
</body>
</html>
注意:再测试国外的时候先注释限制城市访问的。
总结
至此,nginx实现了geoip模块的功能。
|