title: Can't get client's real IP when using Nginx reverse proxy
body: <p><em><strong>Problem description:</strong></em><br/>
  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. I'm not sure if the problem is my code in asp.net core, the Nginx reverse proxy configuration not forwarding my client ip to Kestrel container, or something else.</p>
  <p><strong>Output:</strong><br/></p>
  <blockquote>
  <p>::ffff:172.23.0.3</p>
  </blockquote>
  <p><strong>Desired Output (example using random ip):</strong><br/></p>
  <blockquote>
  <p>231.43.6.124</p>
  </blockquote>
  <p><strong>Setup:</strong><br/></p>
  <ul>
  <li>Remote VPS using Ubuntu and running Docker (i.e. not running locally)<br/></li>
  <li>Kestrel docker container that has no public access<br/></li>
  <li>Nginx Docker container that has public access on port 80 and forwards requests to backend kestrel server.</li>
  </ul>
  <p><strong>nginx.conf</strong><br/></p>
  <pre><code>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 {
  
  }
  </code></pre>
  <p><strong>asp.net core code</strong><br/></p>
  <pre><code>// controller
  public IActionResult GetIP()
  {
     var ip = HttpContext.Connection.RemoteIpAddress.ToString();
     ViewBag.ip = ip;
     return View();
  }
  
  // view
  @{
      ViewData[&quot;Title&quot;] = &quot;Get IP&quot;;
  }
  
  &lt;div class=&quot;text-center&quot;&gt;
     &lt;p&gt;@ViewBag.ip&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  <p><strong>Docker startup:</strong><br /></p>
  <pre><code>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
  </code></pre>
  <p><strong>UPDATE 1:</strong>
  As a test I opened the Kestrel 80 port. I can get client IP when I hit Kestrel directly I just can't get it when it's coming via Nginx reverse proxy.</p>
  <p><strong>UPDATE 2:</strong>
  Added some lines to ngix.conf as per suggestion of one of replies below but didn't seem to make a difference.</p>