nginx 之高級模塊

 

secure_link_module 模塊

做用:html

  • 制定並容許檢查請求的連接的真實性以及保護資源免遭未經受權的訪問
  • 限制連接生效週期

 

配置語法nginx

  • Syntax:secure_link expression 
  • default : - 
  • Context: http、server、location
  • Syntax:secure_link_md5 expression; 
  • default : - 
  • Context:http、server、location

 

secure_link模塊實現請求資源驗證express

 首先確認安裝的時候已經編譯了此模塊瀏覽器

 

 

 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下準備一個文件用於下載app

 

找一個md5加密的文件放在/opt/work下,這裏若是沒有的openssl命令話須要用yum安裝加密

 md5url.shurl

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

 

訪問3d

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

把代理切換到國外後訪問

 

相關文章
相關標籤/搜索