Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

471
Visualizações
Express, Docker and Nginx - Real IP not showing in Express

I have an api gateway that's dockerized and uses nginx as a load balancer. I need to get the original client's ip address but it's not working, I keep getting the nginx ip (I know it's the nginx ip because if I make a request to it, I get the response from my app)

nginx.conf

# cache requests for 10m up to 10g with auto removal after 60m
proxy_cache_path /tmp/nginx_cache/ levels=1:2 keys_zone=backcache:10m max_size=10g
inactive=60m use_temp_path=off;

# allow 10 requests per second with burst cap at 20 (see location block)
limit_req_zone $binary_remote_addr zone=custom_limit:10m rate=10r/s;

upstream gateway {
    # balance requests based on which gateway has the least # of active connections
    server gateway_a:4001;
    server gateway_b:4002;
}

server {
    listen 80;
    gzip on;
    location / {
        # rate-limit but allow for bursting of up to 20 req with nodelay in queue additions
        limit_req zone=custom_limit burst=20 nodelay;

        proxy_http_version 1.1;

        # cache config
        proxy_cache backcache;
        proxy_cache_bypass $http_upgrade;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 3;
        proxy_cache_use_stale error timeout updating http_500 http_502
        http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;


        # proxy headers
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Remote-Port $remote_port;
        
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://gateway;
    }
}

index.js

import os from "os";
import express from "express";

import setupLogging from "./logging";
import setupProxies from "./proxy";
import ROUTES from "./routes";

const { PORT = 8080 } = process.env;

const app = express();

app.use(express.json());

app.use(
  express.urlencoded({
    extended: true,
  }),
);
    
setupLogging(app);
  
setupProxies(app, ROUTES);
  
app.set("trust proxy", true);
  
app.get("/", (req, res) =>
  res.send(`<h1>Hi, my name is ${os.hostname()}</h1>`)
);

app.get("/health", (req, res) => res.sendStatus(200));

app.listen(PORT, () => {
  global.console.info(`Gateway listening at http://localhost:${PORT}`);
});

I'm also using http-proxy-middleware library, I need to process the ip address on the onProxyReq option

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda