What Is X-Forwarded-For? Proxy Headers and IP Leaks

Published:

11 minute read

Acar Diveroli
Written by: Acar Diveroli
A tall header cabinet: Host, Via and User-Agent drawers shut, the blue X-Forwarded-For drawer pulled out with an IP inside

X-Forwarded-For is an HTTP header that a proxy adds to the request it forwards, carrying the IP address of the client that started the request. The target server receives the connection from the proxy, but by reading this line it also sees your address. Forwarded and X-Real-IP carry the same information, while Via only reveals that an intermediary exists. A proxy can add these lines only to requests it can read. This post covers what the four headers carry, who adds them, what changes with HTTPS and how to test your own request.

The four headers and what they carry

Proxy headers tell the target that the request passed through an intermediary and where it came from. The four do similar jobs, but their format and standard status differ.

HeaderStandardInformation carriedExample
X-Forwarded-ForDe facto, not officialIP list of client and intermediaries203.0.113.7, 198.51.100.24
ForwardedRFC 7239Client, intermediary, host and protocolfor=203.0.113.7;proto=http
X-Real-IPDe facto, nginx conventionA single client IP203.0.113.7
ViaRFC 9110Intermediary name and protocol version1.1 proxy-a

X-Forwarded-For is a list. The client that started the request sits on the far left, and each intermediary appends the address it sees on the right. If your address is in the list, the target reads it.

Forwarded is the standardized version of the same job. RFC 7239 defines four parameters: for for the sender of the request, by for the intermediary that received it, host and proto. The standard recommends that the header be disabled by default. When it is enabled, it asks for an obfuscated identifier starting with an underscore, such as _hidden, instead of the real IP in for. For an unknown client, for=unknown is written.

X-Real-IP carries a single address. It comes from nginx configurations, not from a standard: the front server writes the connecting address into this line, and the back-end server reads it.

Via does not carry your IP. RFC 9110 requires every proxy to add Via to the messages it forwards, and allows a pseudonym if the real server name is sensitive. This line only tells the target that a proxy is in the path.

Who adds the header

Any link along the request's path can add a proxy header. There are three typical sources:

  • Your client. If your code or an extension writes X-Forwarded-For, the line goes out as is. In our test, a proxy that added no headers carried the client's line to the target unchanged.
  • The forward proxy. The proxy you use. If it runs transparently, it adds your address.
  • The layer in front of the target. A CDN, load balancer or reverse proxy. It writes the address connecting to it, which is the proxy's exit IP, at the end of the list.

The server sees two things in every request. The first is the source address of the TCP connection. When you use a proxy, that address is the proxy's exit IP. The second is the headers. A leak is when a value in the headers differs from the TCP address and shows your real address.

A correctly configured server does not trust these headers blindly. A client can write any value into the list. That is why nginx's realip module reads the header only on requests from addresses trusted via set_real_ip_from. The right side of the list, the part added by the server's own infrastructure, is trustworthy. The left side is not.

Transparent, anonymous and elite

Proxy anonymity levels are assigned based on exactly these headers. A transparent proxy adds your address, an anonymous proxy only reveals itself, and an elite proxy adds neither. We ran our local test proxy in all three modes and logged the lines the target server received:

ModeExtra lines sent to the targetReal IP visible
TransparentVia, X-Forwarded-For, Forwarded, X-Real-IP✓
AnonymousVia only✗
EliteNone✗

The label is the result of configuration, not a type of software. The same proxy software can run at all three levels with a few lines of settings. We covered the history of the levels and what the "elite" label does not guarantee in Transparent vs Anonymous vs Elite Proxies.

Headers in an HTTPS tunnel

When the target is HTTPS, the proxy cannot see the request headers, so it cannot add any either. The client first asks the proxy for a CONNECT tunnel. Once the tunnel is up, the TLS handshake and the encrypted request pass through it. RFC 9110 requires the proxy to simply relay the data in both directions at this stage.

The only thing the proxy sees is the CONNECT request. With curl 8.23.0, we sent a request for https://example.com/ to a header-logging server acting as the proxy. This is what arrived:

http
CONNECT example.com:443 HTTP/1.1
Host: example.com:443
Proxy-Authorization: Basic dXNlcjpwYXNz
User-Agent: curl/8.23.0
Proxy-Connection: Keep-Alive

The path, the cookies and the headers the site will see are not in this request. When we repeated the test with http://example.com/, the proxy received the full request: the GET http://example.com/ line, every header and Proxy-Authorization. The proxy should strip that credentials line before sending the request to the target. With plain HTTP, the proxy reads the request, modifies it and adds whatever lines it wants.

Our test confirmed this. Through our transparent test proxy, which adds all four headers, we went to https://httpbin.org/headers?show_env=1. None of the lines the proxy added reached the target. The same proxy added all four on an http:// address.

The risk remains in two places:

  • Plain http:// targets. Old APIs, internal network services and the first request before a redirect.
  • An intermediary that terminates TLS. Inspection appliances on corporate networks and debugging proxies that install their own root certificate open the traffic. That layer can add headers. We explained the mechanism in What Is a MITM Proxy?.

SOCKS5 does not speak HTTP; it carries bytes. So a SOCKS5 Proxy connection adds no HTTP headers, even when the target is http://.

Testing with httpbin

The fastest check is to go through the proxy over plain HTTP to an address that reflects the headers back. httpbin.org/headers returns the incoming headers as JSON:

bash
curl -x http://user:pass@pr.proxynet.io:8000 \
  "http://httpbin.org/headers?show_env=1"

show_env=1 is required. Without this parameter, httpbin strips the Via, X-Forwarded-For and X-Real-IP lines from the response. In our test, of the four lines the transparent proxy added, only Forwarded showed up in the response without the parameter. With it, all four showed up.

Watch one detail when reading the output. The load balancer in front of httpbin appends the address connecting to it to the end of the X-Forwarded-For list. Even on a request without a proxy, you will see one address in this line. If the last item is the proxy's exit IP, all is well. Every item before it was added by someone along the way.

The same check in Python looks like this (we ran it with Requests 2.34.2):

python
import requests

PROXY = "http://user:pass@pr.proxynet.io:8000"
URL = "http://httpbin.org/headers?show_env=1"  # plain http: the proxy can add headers

r = requests.get(URL, proxies={"http": PROXY}, timeout=15)
h = r.json()["headers"]

# httpbin's load balancer appends the connecting address to the end of the list
xff = [ip.strip() for ip in h.get("X-Forwarded-For", "").split(",") if ip.strip()]
print("XFF added by the chain:", xff[:-1] or "none")
for name in ("Via", "Forwarded", "X-Real-Ip"):
    print(f"{name}:", h.get(name, "none"))

In elite mode, the script printed "none" on all four lines. In transparent mode, it printed the client's address in the list, plus the Via, Forwarded and X-Real-Ip values.

Testing with your own server

If you would rather not rely on a third-party service, look at the headers on your own server. Run this script on a small server with a public IP:

python
from http.server import BaseHTTPRequestHandler, HTTPServer

class Echo(BaseHTTPRequestHandler):
    def do_GET(self):
        body = f"Connected address: {self.client_address[0]}\n{self.headers}"
        self.send_response(200)
        self.send_header("Content-Type", "text/plain; charset=utf-8")
        self.end_headers()
        self.wfile.write(body.encode())

HTTPServer(("0.0.0.0", 9000), Echo).serve_forever()

Then send the request through the proxy. Replace 203.0.113.10 in the example with your server's address:

bash
curl -x http://user:pass@pr.proxynet.io:8000 \
  http://203.0.113.10:9000/

The first line of the response is the address the server sees at the TCP level. It should be the proxy's exit IP. The lines below it are all the headers that reached the target. If you see a line carrying your own address here, there is a leak. With no CDN in front of the server, the proxy is the only layer in between, which makes the result cleaner than httpbin's.

Reading the results

Every line in the output means something:

  • Your own address in X-Forwarded-For or Forwarded: for=. The proxy runs transparently, or your client writes this line itself. Check the default headers in your own code first.
  • Via only. The proxy reveals itself, not your address.
  • for=unknown or a value starting with for=_. The proxy adds the line but hides the address.
  • No extra lines on HTTPS. The expected result. The tunnel takes care of it.
  • No headers at all, but the site still says you are on a proxy. A non-header signal is at work: the IP's reputation, its ASN or the browser fingerprint.

How we tested

We ran the tests on December 1, 2026, on Windows 11, with Python 3.13.17, Requests 2.34.2 and curl 8.23.0. We ran a local test proxy written in Python in transparent, anonymous and elite modes. As targets we used httpbin.org and a local server that logs the incoming request line and headers as they arrive. We repeated each scenario with both an http:// and an https:// target.

The bottom line

To spot a header leak, go through the proxy to a plain http:// echo address, and don't forget to add show_env=1. On HTTPS traffic the proxy cannot add headers, but an intermediary that opens TLS can. Above all, make sure your own client does not write X-Forwarded-For: even an elite Residential Proxy carries that line as is.

Sources

Frequently asked questions

Who adds the X-Forwarded-For header?

Any intermediary along the path can add it. The forward proxy you use writes your address if it runs transparently. The CDN or load balancer in front of the target appends the address connecting to it to the end of the list. The client itself can send this line too. The line is a list: the address on the left shows who started the request, the ones on the right show the intermediaries that followed.

Does an elite proxy add X-Forwarded-For?

No. By definition, an elite proxy does not add X-Forwarded-For, Forwarded, X-Real-IP or Via. But it is not guaranteed to delete a line your client sends itself. In our test, a proxy that added no headers carried the client's X-Forwarded-For value to the target unchanged. Trust a test with httpbin or your own server, not the label.

Can a proxy leak my IP on HTTPS sites?

Not through a normal CONNECT tunnel. The proxy only sees the target's name and port, and it cannot add lines to the encrypted request. The exception is an intermediary that terminates TLS: a corporate inspection appliance or a debugging proxy whose root certificate you installed opens the request and can add headers. Non-header leaks, such as WebRTC, are a separate topic.

What is the difference between X-Forwarded-For and X-Real-IP?

X-Forwarded-For is a list, and each intermediary appends the address it sees to the end. X-Real-IP carries a single address, usually the client IP written by a front server such as nginx. Neither is an official standard. Their standardized counterpart is the Forwarded header in RFC 7239. In a leak test, look at all four together.

Why doesn't httpbin show X-Forwarded-For?

Because the show_env=1 parameter is missing. Without it, httpbin strips the Via, X-Forwarded-For and X-Real-IP lines from the response. In our test, only Forwarded remained in the response without the parameter. Write the address as http://httpbin.org/headers?show_env=1. The last X-Forwarded-For item comes from httpbin's own load balancer. A leak means seeing your own address before that item.

Does a SOCKS5 proxy add HTTP headers?

No. SOCKS5 does not speak HTTP; it sets up the connection and carries the bytes as they are. Since it does not read the request headers, it cannot add X-Forwarded-For or Via. The only headers the target sees are the ones your client sends and the ones added by layers in front of the target. If there is a leak, its source is one of those two places. Check your own code's default headers first.

Ask ChatGPTAsk Claude