Logging the real client IP in IIS behind a Google Cloud load balancer

When IIS on Google Compute Engine sits behind a Google Cloud Application Load Balancer, your logs show Google front end addresses instead of your visitors. Here is what the load balancer puts in X-Forwarded-For, which entry is the client, and how to record the real client IP in the standard IIS log.

Short answer. Google's documentation says the Application Load Balancer appends two addresses to X-Forwarded-For, the client then the load balancer, after any value the caller supplied, and that it does not verify anything before them. So the client is the second address from the right, not the first. IIS reads none of it, and c-ip records a Google front end address (Google documents 130.211.0.0/22 and 35.191.0.0/16 as the request source ranges for instance group backends). Log the header as a column to see the visitor, as below, or validate the chain to correct c-ip itself.

To see the visitor in your log today, add the header as a custom column (elevated PowerShell):

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' `
  -filter "system.applicationHost/sites/siteDefaults/logFile/customFields" `
  -name "." `
  -value @{logFieldName='X-Forwarded-For';sourceName='X-Forwarded-For';sourceType='RequestHeader'}

It adds a cs(X-Forwarded-For) column and does not change c-ip. Only need the column? The free custom log field covers that, and the section below says where it stops being enough.

What the header looks like

Request arrivesHeader IIS receives
No X-Forwarded-For from the clientCLIENT_IP,LOAD_BALANCER_IP
Client already supplied oneEXISTING_VALUE,CLIENT_IP,LOAD_BALANCER_IP

Google's load balancing documentation says the load balancer appends its values to the existing header and does not verify any addresses that precede them. Only the last two entries are Google's; everything to their left is caller-controlled.

Two related features are worth knowing. A custom request header variable, {client_ip_address}, is described by Google as usually the same as the next-to-last address in X-Forwarded-For, unless the client is using a proxy or the header has been tampered with. And Google's load balancing page notes that setting X-Forwarded-For as a custom request header on the backend service, for example x-forwarded-for:{client_ip_address},{server_ip_address}, discards any existing X-Forwarded-For values from the request. A reverse proxy on the backend can append further addresses of its own, and Google's page we read does not describe the header when Cloud CDN or another proxy is in front, so check a live request in that case.

Which load balancer you have matters

Google's guidance on choosing a load balancer says proxy load balancers do not preserve client IP addresses by default; that includes the external Application Load Balancers. For instance group, zonal network endpoint group and hybrid NEG backends of the global external and classic Application Load Balancers, Google documents requests as coming from 130.211.0.0/22 and 35.191.0.0/16; for a regional external Application Load Balancer the source is the proxy-only subnet you configure.

An external passthrough Network Load Balancer is different. Google documents it as preserving the client source address, so IIS already logs the real client in c-ip and no header or filter is needed. If that is what you have, you can stop here.

Lock the backend to the load balancer

Restrict ingress on the VPC firewall to the ranges above (or the proxy-only subnet), so nobody can connect to IIS directly and supply their own header. Google separately documents the health check ranges, which also need to be allowed or backends are marked unhealthy.

The fix: X-Forwarded-For for IIS

Winfrasoft X-Forwarded-For for IIS is an ISAPI web filter that reads the header the load balancer sends and writes the real client address into the standard IIS c-ip log field, with no application changes and no second log file.

Step by step

  1. Log X-Forwarded-For as a custom field and look at a real request, so you can see how many entries arrive and in what order.
  2. Install the filter on the IIS servers behind the load balancer.
  3. Populate the Proxy Trust List with the Google source ranges for your backend type and with the load balancer address Google appends as the last entry. Because the filter reads from the right and skips trusted hops, it then lands on the client address, which is the next entry in.
  4. Restrict the VPC firewall as above.
  5. Make a request through the load balancer and confirm c-ip now holds the client address.

Is the free custom log field enough?

If all you need is the address somewhere in the log, IIS 8.5 and later can record the header as an extra column with no product, using the snippet at the top of this page. That appends a cs(X-Forwarded-For) column to the W3C log. It does not change c-ip.

It stops being enough when:

The click-through steps, and the log-file naming caveat, are in how to log X-Forwarded-For in IIS. If any of the points above applies, X-Forwarded-For for IIS 10 is the supported way to put the validated client address into c-ip itself.

Frequently asked questions

Which entry in X-Forwarded-For is the real client behind a Google Cloud load balancer?

The second from the right. Google's documentation says the load balancer appends the client IP and then its own address, so the header ends CLIENT_IP,LOAD_BALANCER_IP, and anything before that came from the caller or an earlier proxy and is not verified.

Can I trust the first address in the header?

No. Google says the load balancer does not verify any addresses that precede the two it appends, so the leftmost entry is whatever the caller sent. Read from the right and stop at the first address that is not a hop you control.

Do I need a filter if I use a passthrough Network Load Balancer?

No. Google documents the external passthrough Network Load Balancer as preserving the client source IP address, so IIS sees the client directly and logs it in c-ip without any header.

Which addresses go in the Proxy Trust List for Google Cloud?

For instance group, zonal NEG and hybrid NEG backends of the global external and classic Application Load Balancers, Google documents 130.211.0.0/22 and 35.191.0.0/16 as the request source ranges; for a regional external Application Load Balancer it is the proxy-only subnet. Add the load balancer address Google appends as well, and every internal hop between the two.

Get X-Forwarded-For for IIS

Log the real client IP in the standard IIS c-ip field behind a Google Cloud load balancer, with a Proxy Trust List so the value is validated rather than merely copied. IIS 10 on Windows Server 2016–2025, with a free 14-day trial.

View the product   or request a download →

Stuck on your setup? Email us with your proxy and what c-ip shows, and we will take a look.