Meneruskan Header & IP Klien
Pelajari cara Nginx menulis ulang header permintaan saat melakukan proksi, serta cara mempertahankan informasi klien asli untuk aplikasi hulu.
Meneruskan Header & IP Klien adalah pelajaran API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Meneruskan Header & IP Klien” gratis?
Ya — teks lengkap “Meneruskan Header & IP Klien” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), upgrade ke CoddyKit PRO. Kursus API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Meneruskan Header & IP Klien”?
Pelajari cara Nginx menulis ulang header permintaan saat melakukan proksi, serta cara mempertahankan informasi klien asli untuk aplikasi hulu. Kamu berlatih API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
Tidak diperlukan pengalaman sebelumnya. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Meneruskan Header & IP Klien” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ini?
Ya. Setiap pelajaran API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Mengonfigurasi Proksi Balik Sederhana
- Server Upstream dan Penyeimbangan Beban
- Penyanggaan dan Penyimpanan Tembolok Proksi
- Meneruskan Header & IP Klien