Nginx反向代理后無法獲取客戶端真實IP地址
當(dāng)我們使用 Nginx 代理轉(zhuǎn)發(fā)服務(wù)后,會發(fā)現(xiàn)我們無法獲取客戶端的真實IP地址,從而無法獲取客戶端的地理位置等信息。
1、原始配置文件如下
worker_processes ?1; events { ? ? worker_connections ?1024; } http { ? ? include ? ? ? mime.types; ? ? default_type ?application/octet-stream; ? ? sendfile ? ? ? ?on; ?? ? ? ? keepalive_timeout ?65; ? ? server { ? ? ? ? listen ? ? ? 80; ? ? ? ? server_name ?localhost; ? ? ? ? location / { ? ? ? ? ? ? root ? html; ? ? ? ? ? ? index ?index.html index.htm; ? ? ? ? } ? ? ? ? error_page ? 500 502 503 504 ?/50x.html; ? ? ? ? location = /50x.html { ? ? ? ? ? ? root ? html; ? ? ? ? } ? ? } }
2、配置轉(zhuǎn)發(fā)后
worker_processes ?1; events { ? ? worker_connections ?1024; } http { ? ? include ? ? ? mime.types; ? ? default_type ?application/octet-stream; ? ? sendfile ? ? ? ?on; ?? ? ? ? keepalive_timeout ?65; ? ? server { ? ? ? ? listen ? ? ? 80; ? ? ? ? server_name ?localhost; ? ? ? ? location / { ? ? ? ? ? ? root ? html; ? ? ? ? ? ? index ?index.html index.htm; ? ? ? ? } ? ? ? ?? ? ? ? ? # 代理轉(zhuǎn)發(fā) ?? ??? ?location /api/{ ?? ??? ??? ?proxy_set_header Host $http_host; ?? ??? ??? ?proxy_set_header X-Real-IP $remote_addr; ?? ??? ??? ?proxy_set_header REMOTE-HOST $remote_addr; ?? ??? ??? ?proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?? ??? ??? ?proxy_set_header Public-Network-URL http://$http_host$request_uri; ?? ??? ??? ?proxy_pass http://localhost:8080/; ?? ??? ?} ?? ??? ? ? ? ? ? error_page ? 500 502 503 504 ?/50x.html; ? ? ? ? location = /50x.html { ? ? ? ? ? ? root ? html; ? ? ? ? } ? ? } }
這樣,我們就將客戶端的頭部信息一起轉(zhuǎn)發(fā)過去,就能獲取用戶的真實 IP 地址了
到此這篇關(guān)于Nginx反向代理后無法獲取客戶端真實IP地址的文章就介紹到這了,更多相關(guān)Nginx反向代理獲取IP地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx開啟https配置之后網(wǎng)頁無法訪問的問題處理解決
最近新購服務(wù)器部署nginx之后按照之前的方式部署前端項目并配置https之后訪問頁面無法顯示,本文主要介紹了nginx開啟https配置之后網(wǎng)頁無法訪問的問題處理解決,具有一定的參考價值,感興趣的可以了解一下2023-11-11Nginx+RTMP+nginx-http-flv-module環(huán)境搭建
本文主要介紹了Nginx+RTMP+nginx-http-flv-module環(huán)境搭建,搭建方式可用于直播、視頻會議等場景,同時支持HTTP-FLV,方便在瀏覽器中進行播放2024-03-03Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Requ
HTTP 413 Request Entity Too Large錯誤常常出現(xiàn)在客戶端發(fā)送的請求體超過服務(wù)器允許的大小限制時,本文主要介紹了Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Request Entity Too Large錯誤解決,感興趣的可以了解一下2024-07-07Nginx隱藏index.php和Pathinfo模式配置例子
這篇文章主要介紹了Nginx隱藏index.php和Pathinfo模式配置例子,需要的朋友可以參考下2014-04-04Nginx結(jié)合keepalived實現(xiàn)集群
本文主要介紹了Nginx結(jié)合keepalived實現(xiàn)集群,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05