簡體   English   中英

使用 Nginx 反向代理時無法獲取客戶端的真實 IP

[英]Can't get client's real IP when using Nginx reverse proxy

問題描述:
When I access Asp.net MVC Core 3.1 webpage that is supposed to return my remote client's ip address I instead get internal docker IP of Nginx container. 我不確定問題是否是我在 asp.net 核心中的代碼,Nginx 反向代理配置未將我的客戶端 ip 轉發到 Kestrel 容器,或其他東西。

Output:

::ffff:172.23.0.3

所需的 Output(使用隨機 ip 的示例):

231.43.6.124

設置:

  • 使用 Ubuntu 並運行 Docker 的遠程 VPS(即不在本地運行)
  • 沒有公共訪問權限的 Kestrel docker 容器
  • Nginx Docker 容器在端口 80 上具有公共訪問權限並將請求轉發到后端 kestrel 服務器。

nginx.conf

http {
        upstream backendservers {
                server kestrel:80;
        }
        server {
                listen 80;
                location / {
                        proxy_pass http://backendservers/;
                        proxy_set_header Host $host;
                        proxy_set_header X-Real-IP $remote_addr; 
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Forwarded-Host $host:443;
                        proxy_set_header X-Forwarded-Server $host;
                        proxy_set_header X-Forwarded-Port 443;
                        proxy_set_header X-Forwarded-Proto https;
                }
        }
}

events {

}

asp.net核心代碼

// controller
public IActionResult GetIP()
{
   var ip = HttpContext.Connection.RemoteIpAddress.ToString();
   ViewBag.ip = ip;
   return View();
}

// view
@{
    ViewData["Title"] = "Get IP";
}

<div class="text-center">
   <p>@ViewBag.ip</p>
</div>

Docker 啟動:

docker network create --driver=bridge stacknet
docker run -d --name=kestrel --restart=always -h kestrel.local --network=stacknet mykestrelimage
docker run -d --name=nginx --restart=always  -p 80:80 -h nginx.local --network=stacknet mynginximage

更新 1:作為測試,我打開了 Kestrel 80 端口。 當我直接點擊 Kestrel 時,我可以獲得客戶端 IP 當它通過 Nginx 反向代理來時我無法獲得它。

更新 2:根據以下回復之一的建議,在 ngix.conf 中添加了一些行,但似乎沒有什么不同。

我有類似的問題,這是我嘗試過的,它解決了我的問題..

  1. go 到/etc/nginx/sites-available/你會發現你已經像這樣配置你的服務器的默認文件。
 server{ server_name example.com; location / { proxy_pass http://localhost:4000; proxy_set_header X-Real-IP $remote_addr; <---Add this line proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; <---this line too }

2. /nginx 文件夾中還有一個名為 proxy_params 的文件,請確保您在此文件中有以下行。

 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  1. 在您的后端代碼中最重要的是像這樣獲得 IP。
 const ipAddress = req.headers['x-forwarded-for'] as string;

您也可以通過此命令查看請求 ip。

 $ sudo tail -f /var/log/nginx/access.log

您可能需要轉發原始 IP。 這是 NGinx 關於該主題的文檔: https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/

還有一個較舊的 x-forwarded-for 示例來自: https://plone.lucidsolutions.co.nz/web/reverseproxyandcache/setting-nginx-http-x-forward-headers-for-reverse-proxy

location / {
  proxy_pass              http://upstream/;
  proxy_set_header        Host               $host;
  proxy_set_header        X-Real-IP          $remote_addr;
  proxy_set_header        X-Forwarded-For    $proxy_add_x_forwarded_for;
  proxy_set_header        X-Forwarded-Host   $host:443;
  proxy_set_header        X-Forwarded-Server $host;
  proxy_set_header        X-Forwarded-Port   443;
  proxy_set_header        X-Forwarded-Proto  https;
}

暫無
暫無

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

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