簡體   English   中英

在 Nginx 反向代理背后的 Apache 中獲取真實訪客 IP 不起作用

[英]Get real Visitors IP in Apache behind Nginx reverse proxy dont work

在嘗試解決問題並閱讀我找到的任何教程 5 個小時后,我在這里尋求幫助。

我有一個指向我的Apache Web 服務器的Nginx Reverse Proxy服務器,但在Apache我只從Nginx reverse proxy服務器獲得了IP address ,而不是訪問者 ip。

我的 nginx 代理文件: /etc/nginx/sites-available/proxy.conf

proxy_pass http://MYIPADDRESS;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header        Host            $host;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header        X-Real-IP       $remote_addr;

在 Apache 上,我已添加到/etc/apache2/apache2.conf文件中:

RemoteIPHeader X-Forwarded-For

並更換

LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{X-Forwarded-For}i" combined

a2enmod remoteip也已啟用

同樣在 Nginx /var/log/nginx/access.log文件中,我總是只有localhost IP,例如:

127.0.0.1 - - [27/Jan/2020:23:53:07 +0800] "GET...

還嘗試添加到/etc/nginx/nginx.conf

set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For

如果我現在在我的網絡服務器上運行瀏覽器中的 PHP 腳本:

<?php

echo 'User IP - '.$_SERVER['REMOTE_ADDR'];

?>

我剛剛獲得了反向代理服務器 IP,整個服務器的 IP 都是一樣的。

兩台服務器操作系統都是 Debian 9,有任何想法嗎? 非常感謝!

首先,確保您安裝並啟用了mod_remoteip apache 模塊。

我在我的服務器上使用以下配置。

在 httpd.conf 中:

LoadModule remoteip_module modules/mod_remoteip.so

RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 127.0.0.1

在 nginx 站點配置文件中:

proxy_set_header  X-Real-IP $remote_addr;
proxy_set_header  X-Forwarded-For $http_x_forwarded_for;
proxy_set_header  X-Forwarded-Proto $scheme;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM