nginx獲取使用者真實ip

2021-09-28 15:06:41 字數 4074 閱讀 8725

我們訪問網際網路上的服務時,大多數時,客戶端並不是直接訪問到服務端的,而是客戶端首先請求到反向**,反向**再**到服務端實現服務訪問,通過反向**實現路由/負載均衡等策略。這樣在服務端拿到的客戶端ip將是反向**ip,而不是真實客戶端ip,因此需要想辦法來獲取到真實客戶端ip

客戶端訪問服務端的資料流走向

client(172.25.0.1) --> adsl( 192.168.0.1) --> cdn(10.0.0.1) --> slb(反向**)11.0.0.1 --> server(nginx)12.0.0.1

可以看出,服務端根本獲取不到真實的客戶端ip,只能獲取到上一層服務的ip,那麼nginx怎樣才能獲取到真實的ip呢?

用一台伺服器模擬實現獲取本機ip

原始碼編譯,新增乙個模組,獲取real ip

[root@base1 ~]# tar zxf nginx-1.14.2.tar.gz

[root@base1 ~]# yum install -y gcc pcre-devel zlib-devel # 這是編譯nginx的依賴包

[root@base1 ~]# cd nginx-1.14.2

[root@base1 nginx-1.14.2]# make

[root@base1 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_realip_module # 新增了乙個獲取realip的模組

[root@base1 nginx-1.14.2]# make && make install

[root@base1 nginx-1.14.2]# nginx -v # 檢視編譯引數

[root@base1 conf]# nginx # 開啟nginx服務

[root@base1 conf]# nginx -s reload

[root@base1 conf]# vim /etc/hosts # 新增網域名稱

172.25.78.11 base1 base1.westos.org

測試[root@base1 conf]# curl -i base1.westos.org

當檢測成功時,讓回應real ip

測試[root@base1 conf]# curl base1.westos.org

從x-forwarded-for中獲取到真實客戶端ip

測試[root@base1 conf]# curl -h 「x-forwarded-for:1.1.1.1,172.25.78.11」 base1.westos.org # 給x-forwarded-for中新增ip,從而獲取ip

172.25.78.11

修改配置檔案裡的引數real_ip_recursive 為on

[root@base1 conf]# vim nginx.conf

real_ip_recursive on; # 從前往後依次遞迴獲取ip

[root@base1 conf]# nginx -s reload

測試[root@base1 conf]# curl -h 「x-forwarded-for:1.1.1.1,172.25.78.11」 base1.westos.org

以上操作僅僅實現了獲取本機ip和使用x-forwarded-for的功能,現在我們來模擬實際生產中在有反向**的阻礙下獲取真實客戶端ip,這裡我們用乙個反向**來模擬

base1 後端伺服器

base2 nginx反向**伺服器

配置反向**伺服器

配置服務端

[root@base1 conf]# vim nginx.conf

server

}[root@base1 conf]# nginx -s reload

[root@base1 conf]# mkdir /web

[root@base1 conf]# vim /web/index.html

www.westos.org

客戶端測試

[root@foundation78 desktop]# vim /etc/hosts

172.25.78.11 base1 base1.westos.org

[root@foundation78 desktop]# curl base1.westos.org

www.westos.org

服務端檢視日誌

[root@base1 conf]# cat /usr/local/nginx/logs/access.log # 通過日誌可以直接檢視到客戶端真實ip

總結:1.使用realip模組後,rem

otea

ddr輸

出結果為

真實客戶

端ip,

可以使用

remote_addr輸出結果為真實客戶端ip,可以使用

remote

a​dd

r輸出結

果為真實

客戶端i

p,可以

使用realip_remote_addr獲取最後乙個反向**的ip;

3.real_ip_headerx-forwarded-for:告知nginx真實客戶端ip從哪個請求頭獲取;

4.set_real_ip_from 172.25.78.0/24:告知nginx哪些是反向**ip,即排除後剩下的就是真實客戶端ip

5.real_ip_recursive on:是否遞迴解析,當real_ip_recursive配置為off時,nginx會把real_ip_header指定的請求頭中的最後乙個ip作為真實客戶端ip;

當real_ip_recursive配置為on時,nginx會遞迴解析real_ip_header指定的請求頭,最後乙個不匹配set_real_ip_from的ip作為真實客戶端ip。

nginx獲取使用者真實ip

我們有的介面牽扯到使用者資訊的變更,這些介面只允許接入方來調我們,所以我們在nginx上,關於這些介面都個ip白名單,裡面配上接入方的出口ip。下面是我們nginx的access裡的日誌。nginx取的我們這個 remote addr 當做真實ip了,而事實上,http x forwarded fo...

flask nginx獲取使用者真實ip

應用使用flask框架開發,使用uswgi啟動,使用supervisor管理程序,使用nginx 服務。flask本身可以通過request.remote addr獲取使用者的ip,但是通過nginx 之後,獲取到的是本地位址或者區域網位址。為了在nginx 的環境下獲取使用者真實ip,可以通過以下...

CDN下nginx獲取使用者真實IP位址

隨著nginx的迅速崛起,越來越多公司將apache更換成nginx.同時也越來越多人使用nginx作為負載均衡,並且 前面可能還加上了cdn加速,但是隨之也遇到乙個問題 nginx如何獲取使用者的真實ip位址 例項環境 使用者ip 120.22.11.11 cdn前端 61.22.22.22 cd...