nginx 之高级模块
secure_link_module 模块
作用:
- 制定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问
- 限制链接生效周期

配置语法
- Syntax:secure_link expression
- default : -
- Context: http、server、location
- Syntax:secure_link_md5 expression;
- default : -
- Context:http、server、location
secure_link模块实现请求资源验证
首先确认安装的时候已经编译了此模块
 
test_safe_down.conf
server {
    listen       80;
    server_name  www.zhangbiao.com;
    root /opt/app/code;
    location / {
        secure_link $arg_md5,$arg_expires;
        secure_link_md5 "$secure_link_expires$uri imooc";
        if ($secure_link = "") {
            return 403;
        }
        if ($secure_link = "0") {
            return 410;
        }
    }
    }
}
在/opt/app/code/download下准备一个文件用于下载
 
找一个md5加密的文件放在/opt/work下,这里如果没有的openssl命令话需要用yum安装
md5url.sh
#!/bin/sh
#
#Auth:www.zhangbiao.com
servername="www.zhangbiao.com"
download_file="/download/test.img"
time_num=$(date -d "2019-7-18 00:00:00" +%s)
secret_num="imooc"
res=$(echo -n "${time_num}${download_file} ${secret_num}"|openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)
echo "http://${servername}${download_file}?md5=${res}&expires=${time_num}"
 
访问
http://www.zhangbiao.com/download/?md5=v5W0ZVlg&expires=1563379200
 
访问一个错误的

geoip_module 模块
基于IP地址匹配MaxMind GeoIP二进制文件,读取IP所在地域信息
使用场景
- 区别国内国外作HTTP访问规则
- 区别国内城市地域作HTTP访问规则
nginx 默认没有安装这个模块,需要手动安装
yum install nginx-module-geoip
安装完成后,会在/etc/nginx/modules/下生成对应文件
 
geoip读取地域信息场景展示
在 /etc/nginx/geoip 路径下 执行以下命令下载 国家和城市数据并解压
wget http://geolite.maxmind.com/dowmload/geoip/database/GeoLiteCountry/GeoIP.dat.gz wget http://geolite.maxmind.com/dowmload/geoip/database/GeoLiteCity.dat.gz
编写location
/etc/nginx/conf.d/test_geo.conf
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
server {
    listen       80;
    server_name  localhost;
    location / {
        if ($geoip_country_code != CN) {
            return 403;
        }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
   location /myip {
        default_type text/plain;
        return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
   }
    }
}
浏览器访问服务器地址,就可以看到当前出口的地址,有代理是,展示的是配置后代理的地址
 
上面已经配置了,当ip不是CN(中国)的时候,返回403
把代理切换到国外后访问
 

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号