1. 程式人生 > >Nginx詳解十九:Nginx深度學習篇之進階高級模塊

Nginx詳解十九:Nginx深度學習篇之進階高級模塊

cat max 地址 index 信息 access gin 切換 以及

這裏介紹一些最新或者理解起來有一些難度的Nginx模塊

一、secure_link_module模塊作用原理:
1、制定並允許檢查請求的鏈接的真實性以及保護資源免遭未經授權的訪問
2、限制鏈接生效周期

技術分享圖片

技術分享圖片

配置語法:secure_link expression;
默認狀態:-
配置方法:http、server、location

配置語法:secure_link_md5 expression;
默認狀態:-
配置方法:http、server、location

二、secure_link模塊實現請求資源驗證

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

技術分享圖片

準備好一個配置文件

技術分享圖片

server {

listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
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安裝

技術分享圖片

技術分享圖片

執行文件,並把生成的鏈接用瀏覽器打開:

技術分享圖片

文件開始下載

技術分享圖片

技術分享圖片

如果傳的錯誤的參數時,會返回403

技術分享圖片

geoip_module模塊
基於IP地址匹配MaxMind GeoIP二進制文件,讀取IP所在地域信息

使用場景
1、區別國內國外作HTTP訪問規則

2、區別國內城市地域作HTTP訪問規則


需要yum安裝:yum install nginx-module-geoip

技術分享圖片

安裝完成後,會在/etc/nginx/modules/下生成對應文件

geoip讀取地域信息場景展示

執行命令下載國家和城市ip地址信息,下載完後解壓

wget http://geolite.maxmind.com/dowmload/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/dowmload/geoip/database/GeoLiteCity.dat.gz

準備一個conf文件

技術分享圖片

geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

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

把代理切換到國外後訪問

技術分享圖片

Nginx詳解十九:Nginx深度學習篇之進階高級模塊