這里介紹一些最新或者理解起來有一些難度的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
把代理切換到國外后訪問