title: How to get client ip running a Node.js app in Docker behind Nginx reverse proxy
body: <p>I've can't retrieve the client IP address inside my docker container</p>
  <p><a href="https://i.stack.imgur.com/MeFKX.png" rel="nofollow noreferrer">Tree</a>
  (I didn't put the Dockerfile in the following examples, but it's a basuc one)</p>
  <p>I'm using Koa as a Node.js server and I already enabled proxy.</p>
  <pre class="lang-js prettyprint-override"><code>// server.js
  
  const Koa = require('koa');
  
  const app = new Koa();
  
  // Based on Koa's doc it's required to access X-Forwarded-For
  app.proxy = true;
  
  app.use((ctx) =&gt; {
    ctx.body = ctx.ip;
  });
  
  app.listen(5000, () =&gt; {
    console.log('Listening');
  });
  </code></pre>
  <p>The compose file is basic</p>
  <pre class="lang-yaml prettyprint-override"><code># docker-compose.yml
  
  version: '3.8'
  services:
    nginx:
      image: nginx
      ports:
        - '80:80'
      volumes:
        - ./nginx.conf:/etc/nginx/nginx.conf
  
    app:
      build:
        context: .
        dockerfile: Dockerfile
      ports:
        - '5000:5000'
  </code></pre>
  <p>The Nginx conf is based on internet searches.</p>
  <pre><code># nginx.conf
  
  events {
  
  }
  
  http {
    server {
      listen    80;
  
      location / {
          proxy_pass http://app:5000;
  
          proxy_set_header Host              $http_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-Proto $scheme;
      }
    }
  }
  </code></pre>
  <p>When I go on http://localhost I get the container IP <code>172.22.0.1</code>.
  I would like to be able to get the client IP.</p>