江蘇藝居建設(shè)有限公司網(wǎng)站整合營銷什么意思
一、Nginx配置訪問IP白名單
有時部署的應(yīng)用需要只允許某些特定的IP能夠訪問,其他IP不允許訪問,這時,就要設(shè)置訪問白名單;
設(shè)置訪問白名單有多種方式:
1.通過網(wǎng)絡(luò)防火墻配置,例如阿里云/華為云管理平臺
2.通過服務(wù)器防火墻配置,iptables
3.通過nginx配置訪問分發(fā)限制
4.通過nginx的allow、deny參數(shù)進行訪問限制(本文使用此方案)
Nginx白名單使用allow和deny來控制,該配置可以添加在http段,也可以server、location中
如果想增加允許訪問的IP范圍,例如10.10.10.0~10.10.10.255,需要使用CIDR格式表示你的IP范圍,在Nginx中默認僅允許IP地址和CIDR格式,CIDR轉(zhuǎn)IPv4網(wǎng)站:https://www.ipaddressguide.com/cidr
示例一
nginx所有代理生效,僅允許192.168.1.6的訪問,其他所有IP均不能訪問。
http {
......
allow 192.168.1.6;
deny all;
......
}
示例二
nginx某個端口server代理生效,僅允許192.168.1.6和192.168.1.6的訪問,其他所有IP均不能訪問。
server {
......
allow 192.168.1.6;
allow 192.168.1.8;
deny all;
......
}
示例三
nginx某個location代理生效,僅允許192.168.1.6和192.168.2.0~192.168.2.255網(wǎng)段的訪問,其他所有IP均不能訪問。
location /screen {
......
allow 192.168.1.6;
allow 192.168.2.0/24;
deny all;
......
}
修改完成后,重載nginx配置文件生效:sbin/nginx -s reload
注意:如果本機也需要訪問這個代理,記得加上allow 127.0.0.1;
配套操作:互聯(lián)網(wǎng)上,一般訪問端client的IP都不是本機的局域網(wǎng)IP,而是運營商的出口IP,需要注意;
查詢方法:百度或者搜狗,輸入關(guān)鍵詞“IP”,然后點擊“IP地址查詢”相關(guān)搜索結(jié)果,即可查詢到自己的IP。
不確定或者不能訪問外網(wǎng)的話,就在nginx的報錯日志里找,logs/error.log,能找到訪問此nginx的IP。
二、Nginx添加白名單的四種方式
1)添加防火墻白名單
針對nginx域名配置所啟用的端口(比如80端口)在iptables里做白名單,比如只允許100.110.15.16、100.110.15.17、100.110.15.18訪問.但是這樣就把nginx的所有80端口的域名訪問都做了限制,范圍比較大!
2)利用$remote_addr參數(shù)進行訪問的分發(fā)限制
如果只是針對nginx下的某一個域名進行訪問的白名單限制,那么可以在nginx的配置文件里進行設(shè)置,如下:
##白名單設(shè)置,只允許下面三個來源ip的客戶端以及本地能訪問該站。主要是下面這三行if?($remote_addr !~ ^(100.110.15.16|100.110.15.17|100.110.15.18|127.0.0.1)) {rewrite ^.*$?/maintence.php last;}
3)也可以使用$http_x_forwarded_for參數(shù)進行訪問的分發(fā)限制,如下:
##白名單設(shè)置,只允許下面三個來源ip的客戶端以及本地能訪問該站。if?($http_x_forwarded_for !~ ^(100.110.15.16|100.110.15.17|100.110.15.18|127.0.0.1)) {rewrite ^.*$?/maintence.php last;}
4)還可以利用nginx的allow、deny參數(shù)進行訪問限制
##白名單設(shè)置,只允許下面三個來源ip的客戶端以及本地能訪問該站。allow 100.110.15.16;allow 100.110.15.17;allow 100.110.15.18;allow 127.0.0.1;deny all;
三、Nginx根據(jù)用戶IP設(shè)置訪問跳轉(zhuǎn)
第一種方法
根據(jù)$remote_addr客戶端IP地址判斷,判斷成功即返回301跳轉(zhuǎn),可以寫正則,如果有大量不規(guī)則IP就很頭疼了。
if ($remote_addr = 192.168.1.123) {return 301 https://blog.whsir.com;
}
第二種方法
nginx通過lua實現(xiàn),這方法是孔大神給的,將需要做301跳轉(zhuǎn)的IP,直接寫到/tmp/ip文件中,支持網(wǎng)段,一行一個,添加后不需要重啟nginx,即時生效。
注意nginx要編譯lua模塊才可以使用,我的whsir一鍵包nginx已集成lua。
rpm -ivh http://mirrors.whsir.com/centos/whsir-release-centos.noarch.rpmyum install wnginx -y
set_by_lua $info 'local opt = ngx.var.remote_addrlocal 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;}
四、nginx配置IP白名單的詳細步驟
分析nginx訪問日志,有哪些IP訪問過nginx。
命令參考:
awk '{print $1}' logs/access.log | sort | uniq -c | sort -nr -k1
輸出的效果案例:
1053 192.168.3.15
893 192.168.3.10
818 192.168.0.8
1、添加IP白名單文件
在nginx目錄的 conf 中添加文件 ip.conf,注意白名單文件不用添加任何注釋,可以有空行
vi ip.conf
192.168.3.11 1;192.168.3.10 1;
192.168.0.112 1;
2、配置nginx.conf
編輯http節(jié)點:
http {# ...# geo IP whitelistgeo $remote_addr $ip_whitelist {default 0;include ip.conf;}# ...}
編輯server節(jié)點:
server {listen ? ? ? 80;# ...# IP whitelistset $whitelist_flag 1;if ( $ip_whitelist != 1 ) {set $whitelist_flag "${whitelist_flag}0";}if ( $request_uri !~* '/warn_navigate_page' ) {set $whitelist_flag "${whitelist_flag}0";}if ( $whitelist_flag = "100" ) {#return 403;rewrite ^(.*)$ $scheme://$host:$server_port/warn_navigate_page break; #白名單的提示頁面}# ...}
也可以在location節(jié)點中編輯,示例:
編輯location節(jié)點:
location /test {proxy_pass ?http://IP/test;# ...# IP whitelistset $whitelist_flag 1;if ( $ip_whitelist != 1 ) {set $whitelist_flag "${whitelist_flag}0";}if ( $request_uri !~* '/warn_navigate_page' ) {set $whitelist_flag "${whitelist_flag}0";}if ( $whitelist_flag = "100" ) {#return 403;rewrite ^(.*)$ $scheme://$host:$server_port/warn_navigate_page break; #白名單的提示頁面}# ...}
添加導(dǎo)航的提示頁 /warn_navigate_page
server {listen ? ? ? 80;# ...# 白名單的提示導(dǎo)航頁面location /warn_navigate_page {root /home/java/nginx/bizapp/warn_navigate_page;index ?warn_navigate_page.html warn_navigate_page.htm;rewrite ^(.*)$ /warn_navigate_page.html break;}}
3、編輯白名單的提示導(dǎo)航頁面
在 /home/java/nginx/bizapp/warn_navigate_page 中編輯頁面warn_navigate_page.html
參考:
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"><meta content="yes" name="apple-mobile-web-app-capable"><meta content="black" name="apple-mobile-web-app-status-bar-style"><meta content="telephone=no" name="format-detection"><meta content="email=no" name="format-detection"><title>系統(tǒng)通知</title><style type="text/css">body {background: url(https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png) no-repeat;background-size: 100% 100%;background-attachment: fixed;}</style></head><body><div><pre> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 【通知公告】尊敬的用戶您好,系統(tǒng)已不提供IP地址直接訪問,請聯(lián)系管理員添加白名單?;ヂ?lián)網(wǎng)的域名訪問地址:<a href="https://www.baidu.com">跳轉(zhuǎn)https://www.weidianyuedu.com</a></pre></div></body><script type="text/javascript"></script></html>
?參考:https://blog.csdn.net/wanzhong11/article/details/131396910
https://blog.csdn.net/hdxx2022/article/details/132020861
https://blog.whsir.com/post-4430.html