IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> nginx实现geoip模块的实验 -> 正文阅读

[系统运维]nginx实现geoip模块的实验


提示:以下是本篇文章正文内容,下面案例可供参考

一、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模块的功能。

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 08:14:35  更:2021-07-28 08:15:14 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/6 19:08:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码