Commenter IP vs. the Load Balancer

I have always thought the support for WordPress has been amazing because it is so readily available, but this support went to a new level yesterday.  When I went to fix my most recent issue, I found that the solution was archived in a comment in the WordPress core (in this case, comment.php).  Troubleshooting doesn't get any easier than that.

The Problem

 I have a WordPress installation running on load balanced servers.  Whenever someone leaves a comment, instead of saving the Commenter's IP address to the database, WordPress saves the IP address of the load balancer.

Line 1310-1313 of comment.php provided the answer in a comment:

* We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure * that it is properly set, such as in wp-config.php, for your environment. * See {@link http://core.trac.wordpress.org/ticket/9235}

The ticket linked to a solution by Andrew Nacin that grabs the commenter's IP address from the server, checks to make sure it is the appropriate format, and sets the variable that comment.php will need to save the correct address.  The way our servers work, this solution worked by swapping out  HTTP_X_FORWARDED_FOR with HTTP_CLIENT_IP.

Lesson

If you have a problem with WordPress, read its code.