nginx 之高级模块

匿名 (未验证) 提交于 2019-12-02 22:10:10

secure_link_module 模块

作用:

  • 制定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问
  • 限制链接生效周期

配置语法

  • Context:http、server、location

secure_link模块实现请求资源验证

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安装

#!/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 

 

基于IP地址匹配MaxMind GeoIP二进制文件,读取IP所在地域信息

使用场景

  • 区别国内国外作HTTP访问规则
  • 区别国内城市地域作HTTP访问规则

nginx 默认没有安装这个模块,需要手动安装

yum install nginx-module-geoip 

  

安装完成后,会在/etc/nginx/modules/下生成对应文件

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

把代理切换到国外后访问

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!