Transmission des en-têtes et adresse IP du client
Découvrez comment Nginx réécrit les en-têtes de requête lors du relais, et comment préserver les informations d’origine du client pour les applications en amont.
Transmission des en-têtes et adresse IP du client est une leçon API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), et ta progression se synchronise sur le web et l'application CoddyKit. Le cours API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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_addrholds 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/headersQuick 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:
Hostpreserves the requested domainX-Real-IPandX-Forwarded-Forcarry the client IPX-Forwarded-Protoreports the original scheme- Use
real_ipand trusted ranges to avoid spoofing
These headers are the foundation of a transparent, secure reverse proxy.
Questions Fréquemment Posées
La leçon « Transmission des en-têtes et adresse IP du client » est-elle gratuite ?
Oui — le texte complet de « Transmission des en-têtes et adresse IP du client » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), passe à CoddyKit PRO. Le cours API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Transmission des en-têtes et adresse IP du client » ?
Découvrez comment Nginx réécrit les en-têtes de requête lors du relais, et comment préserver les informations d’origine du client pour les applications en amont. Tu pratiques API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ?
Aucune expérience préalable n'est requise. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.
Combien de temps prend la leçon « Transmission des en-têtes et adresse IP du client » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ?
Oui. Chaque leçon API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Configurer un proxy inverse simple
- Serveurs en amont et équilibrage de charge
- Mise en mémoire tampon et mise en cache du proxy
- Transmission des en-têtes et adresse IP du client