Nginx反向代理后無法獲取客戶端真實IP地址
當我們使用 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配置HTTP強制跳轉(zhuǎn)到HTTPS的解決辦法
這篇文章主要給大家介紹了關(guān)于Nginx配置HTTP強制跳轉(zhuǎn)到HTTPS的解決辦法,當Nginx配置https后通常需要將用戶http請求強制跳轉(zhuǎn)到https,需要的朋友可以參考下2023-08-08
Nginx加固的幾種方式(控制超時時間&限制客戶端下載速度&并發(fā)連接數(shù))
本文主要介紹了Nginx加固的幾種方式,包括控制超時時間,限制客戶端下載速度,并發(fā)連接數(shù)這幾種方式,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Nginx gateway集群和動態(tài)網(wǎng)關(guān)的實現(xiàn)思路
這篇文章主要介紹了Nginx gateway集群和動態(tài)網(wǎng)關(guān),動態(tài)網(wǎng)關(guān)即任何配置都實現(xiàn)不用重啟網(wǎng)關(guān)服務(wù)器都可以及時刷新,對Nginx gateway集群相關(guān)知識感興趣的朋友一起看看吧2022-10-10
windows安裝nginx部署步驟圖解(反向代理與負載均衡)
這篇文章主要介紹了windows安裝nginx部署步驟,設(shè)置反向代理與負載均衡的使用方法,需要的朋友可以參考下2014-02-02
寶塔里nginx自動停止的解決方法(檢測腳本實現(xiàn)每分鐘檢測并自動啟用)
nginx突然停止的原因有多種,這里不列舉,可以排查具體原因,這里主要為大家分享nginx服務(wù)的檢測腳本,達到自動啟用的實現(xiàn)2025-02-02

