简体   繁体   中英

Getting Client IP Using PHP with Load Balancer (X-Forwarded-For Header)

Here's my situation: I have a PHP website set up with IIS 7 and an F5 load balancer. I need to retrieve the client IP address that is accessing the server.

As expected in this scenario, $_SERVER['REMOTE_ADDR'] returns the IP address of the load balancer, not the actual client PC. Typically, I know that $_SERVER['HTTP_X_FORWARDED_FOR'] would be the variable that would return the actual client PC IP address, but for whatever reason, it is empty here. In other words, returning the X-Forwarded-For header returns nothing - what am I to do here?

How to Add an X-Forwarded-For Header and Configure IIS Logging

The X-Forwarded-For (XFF) HTTP header field is a standard method for identifying the originating IP address of a client connecting to a server through the Kemp LoadMaster or any proxy.

The Kemp LoadMaster allows us to give the client's IP address to the destination Real Server by inserting the X-Forwarded-For header when Layer7 is used with non-transparency.

Inserting the X-Forwarded-For header allows the Real Server to log the client source IP address in its logs.

Adding the X-Forwarded-For header using the LoadMaster can be done either as a global setting or as a per-Virtual Service setting. Refer to the relevant section below for steps on how to add the header.

Note: The addition of the X-Forwarded-For header is only available for HTTP and HTTPS traffic with SSL Offloading.

Setting The Additional X-Forwarded-For Header Globally

In the main menu of the LoadMaster User Interface (UI), select System Configuration > Miscellaneous Options > L7 Configuration > Additional L7 Header > X-Forwarded-For.

Setting The Additional X-Forwarded-For Header Per Virtual Service

In the main menu of the LoadMaster WUI, select Virtual Services > View/Modify Services. Click Modify on the relevant Virtual Service. Expand the Advanced Properties section. In the Add HTTP Headers drop-down list, select either X-Forwarded-For (No Via) or X-Forwarded-For (+ Via).

Configuring Custom IIS Logging Fields on Microsoft Server 2012

In IIS 8.5 and later, custom logging fields can be added to record X-Forwarded-For headers to record a client's source IP address when transparency is not being used.

Navigate to the site which will use X-Forwarded-For logging and click Logging and Open Feature.

Click the Select Fields... option

Click the Add Field... option.

Configure the fields as indicated below:

Field Name: X-Forwarded-For

Source type: Request Header

Source name: X-Forwarded-For (syntax important)

Click OK twice.

Click Apply in the top-right of the logging options page.

Now, generate some log traffic by navigating to the Virtual Service and hitting refresh a few times.

Go to the location of the advanced logfiles and open the newly created logfiles.

The default location is C:\\inetpub\\logs\\LogFiles\\W3SVC1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM