0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Lezione

Inoltro degli header e IP del client

Impari come Nginx riscrive gli header delle richieste quando opera come proxy e come conservare le informazioni originali del client per le applicazioni upstream.

Inoltro degli header e IP del client è una lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Why Headers Matter

When Nginx acts as a reverse proxy, the upstream server only sees a connection coming from Nginx, not the real client. Without extra configuration, the backend loses the visitor's original IP, host, and protocol.

To keep this information, Nginx must forward headers describing the original request.

The Default Host Header

By default Nginx may pass the upstream server's address as the Host header. Many apps rely on the original host for routing or building absolute URLs.

Set it explicitly so the backend sees what the client requested:

location / {
    proxy_pass http://backend;
    proxy_set_header Host $host;
}

Preserving the Real Client IP

The X-Real-IP header carries the original client's address so the backend can log or rate-limit by real visitor.

  • $remote_addr holds the connecting client's IP
proxy_set_header X-Real-IP $remote_addr;

X-Forwarded-For Chain

X-Forwarded-For is a comma-separated chain of every proxy a request passed through. The variable $proxy_add_x_forwarded_for appends the current client to any existing value.

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Forwarding the Protocol

If TLS terminates at Nginx, the backend speaks plain HTTP and cannot tell the original scheme. Send X-Forwarded-Proto so it can build correct https URLs and set secure cookies.

proxy_set_header X-Forwarded-Proto $scheme;

Putting It Together

A standard reverse-proxy block bundles all four headers. This is the baseline you will reuse across nearly every project.

location / {
    proxy_pass http://backend;
    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-Proto $scheme;
}

Reusing Header Config

Repeating the same headers in every location is error prone. Put them in an include file and pull it in wherever needed.

# /etc/nginx/proxy_headers.conf
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-Proto $scheme;

# in a server/location block:
include /etc/nginx/proxy_headers.conf;

Trusting Forwarded Headers Safely

Clients can spoof X-Forwarded-For. Only trust it from known proxies. Use the real_ip module to set the true client IP from trusted upstreams.

set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;
real_ip_recursive on;

Removing or Clearing Headers

You can strip a header by setting it to an empty string. This is useful to hide internal headers from upstream or to prevent leaking server identity.

proxy_set_header X-Powered-By "";
proxy_hide_header X-Powered-By;

Passing Custom Headers

You can inject your own headers to give the backend extra context, such as a tag identifying which gateway handled the request.

proxy_set_header X-Gateway-Node "edge-1";
proxy_set_header X-Request-Start $msec;

Verifying the Result

Test from a backend that echoes headers (or use an inspector endpoint). You should see X-Real-IP matching your machine and X-Forwarded-Proto matching the scheme you used.

curl -H "Host: example.com" http://localhost/headers

Quick Check

Which Nginx variable appends the current client to an existing forwarding chain?

Recap

You learned how Nginx rewrites requests and how to keep client context intact:

  • Host preserves the requested domain
  • X-Real-IP and X-Forwarded-For carry the client IP
  • X-Forwarded-Proto reports the original scheme
  • Use real_ip and trusted ranges to avoid spoofing

These headers are the foundation of a transparent, secure reverse proxy.

Domande Frequenti

La lezione «Inoltro degli header e IP del client» è gratuita?

Sì — il testo completo di «Inoltro degli header e IP del client» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), passa a CoddyKit PRO. Il corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include 4 lezioni in totale.

Cosa imparerò in «Inoltro degli header e IP del client»?

Impari come Nginx riscrive gli header delle richieste quando opera come proxy e come conservare le informazioni originali del client per le applicazioni upstream. Eserciti API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Non è richiesta alcuna esperienza precedente. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Inoltro degli header e IP del client»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Sì. Ogni lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Configurazione di un proxy inverso semplice
  2. Server upstream e bilanciamento del carico
  3. Buffering e caching del proxy
  4. Inoltro degli header e IP del client
← Torna a API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)