文章目錄
- 一、http_geoip_module使用場景
- 二、使用步驟
- 1.使用腳本編譯安裝nginx,一鍵部署
- 2.下載安裝 MaxMind 的 GeoIP 庫
- 3.將之前解壓的所需資料庫放入nginx的組態檔目錄中
- 4.修改nginx.conf檔案的配置
- 5.語法檢測及啟動nginx
- 6.在服務器上配置多個ip地址進行測驗
- 總結
提示:以下是本篇文章正文內容,下面案例可供參考
一、http_geoip_module使用場景
一、區別國內外作HTTP訪問規則
二、區別國內城市地域作HTTP訪問規則
二、使用步驟
1.使用腳本編譯安裝nginx,一鍵部署
#!/bin/bash
#解決軟體的依賴關系,需要安裝的軟體包
#新建luogan用戶和組
id xiongxue || useradd xiongxue -s /sbin/nologin
#下載nginx軟體
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
#修改PATH變數
echo "PATH=$PATH:/usr/local/scxiongxue99/sbin" >>/root/.bashrc
#執行修改了環境變數的腳本
source /root/.bashrc
#firewalld and selinux
#stop firewall和設定下次開機不啟動firewalld
service firewalld stop
systemctl disable firewalld
#臨時停止selinux和永久停止selinux
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
#修改nginx.conf的配置,例如:埠號,worker行程數,執行緒數,服務域名
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
#killall nginx行程
#killall -9 nginx
#啟動nginx
#/usr/local/scxiongxue99/sbin/nginx
2.下載安裝 MaxMind 的 GeoIP 庫
下載鏈接:鏈接:https://pan.baidu.com/s/1aEogiu9clrJTNZQmFkI_Ag
提取碼:123r
下載好后進行解壓
[root@bnginx sc]# yum install unzip -y
[root@bnginx sc]# unzip geoip.zip
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]# cp GeoIP.dat GeoLiteCountry.dat /usr/local/scxiongxue99/conf/
[root@bnginx sc]# cp GeoLiteCity.dat /usr/local/scxiongxue99/conf/
4.修改nginx.conf檔案的配置
http {
geoip_country /usr/local/scxiongxue99/conf/GeoIP.dat;#在http塊里添加的
geoip_city /usr/local/scxiongxue99/conf/GeoLiteCity.dat; #在http塊里添加的
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.xiongxue.com;
if ($geoip_city != 'Changsha'){
return 403;
}#城市的檢查
if ($geoip_country_code != 'CN'){
return 404;
}#國家的檢查--在server塊里添加
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
5.語法檢測及啟動nginx
[root@bnginx conf]# /usr/local/scxiongxue99/sbin/nginx -t
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]# /usr/local/scxiongxue99/sbin/nginx
[root@bnginx conf]# ps aux|grep nginx
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]# /usr/local/scxiongxue99/sbin/nginx -s reload
6.在服務器上配置多個ip地址進行測驗
[root@bnginx scxiongxue99]# ip add add 175.8.132.15/24 dev ens33 #在網卡里添加長沙的ip地址
[root@bnginx sc]# curl 175.8.132.15 #能成功訪問
<!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]# ip add add 120.227.122.105/24 dev ens33 #添加岳陽的ip
[root@bnginx conf]# curl 120.227.122.105
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.21.1</center>
</body>
</html>
#回傳403 被禁止訪問
[root@bnginx scxiongxue99]# ip add add 8.8.8.8/8 dev ens33
#添加國外的ip
[root@bnginx conf]# /usr/local/scxiongxue99/sbin/nginx -s reload
[root@bnginx conf]# curl 8.8.8.8
<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>
#回傳404
注意:再測驗國外的時候先注釋限制城市訪問的,
總結
至此,nginx實作了geoip模塊的功能,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/290623.html
標籤:其他
