Nginx判斷使用者IP設定通路跳轉
第一種方法
根據$remote_addr用戶端IP位址判斷,判斷成功即傳回301跳轉,可以寫正則,如果有大量不規則IP就很頭疼了。
if ($remote_addr = 192.168.1.123) {
return 301 https://blog.whsir.com;
}
第二種方法
nginx通過lua實作,這方法是
孔大神給的,将需要做301跳轉的IP,直接寫到/tmp/ip檔案中,支援網段,一行一個,添加後不需要重新開機nginx,即時生效。
注意nginx要
編譯lua子產品才可以使用,我的
whsir一鍵包nginx已內建lua。
rpm -ivh http://mirrors.whsir.com/centos/whsir-release-centos.noarch.rpm
yum install wnginx -y
set_by_lua $info '
local opt = ngx.var.remote_addr
local file = io.popen("ip=" ..opt.. ";if grep -q $ip /tmp/ip;then echo $ip; exit 0;fi ; for net in $(grep / /tmp/ip);do [ $(ipcalc -n $ip/${net#*/}) = $(ipcalc -n $net) ] && echo $ip && break; done")
content1 = file:read("*l")
return content1
';
if ( $info = $remote_addr) {
return 301 https://blog.whsir.com;