WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WordPress Behind Reverse Proxy? (5 posts)

  1. bartmoss
    Member
    Posted 5 years ago #

    Hi Guys,

    I want to run wordpress behind a reverse proxy. Setup will be:

    http://www.example.com:80 (proxy)
    --> accesses localhost:12345 (wordpress).

    WordPress automatically redirects to localhost:12345 after the initial setup, this is easily fixed by editing the url/hostname in the mysql database. However, now it will do redirects to itself in an endless loop. I understand why this happens (wordpress will see the localhost:12345 in the http get request and say "hey thats not my canonical hostname"); is there an easy way to stop this? I just want wordpress to hand out pages, and let me worry about hitting the right hostname, I know what I am doing. ;-)

    Any help appreciated.

    best wishes
    Nils

  2. bartmoss
    Member
    Posted 5 years ago #

    And ProxyPreserveHost On seems to do the trick.

  3. lecharabia
    Member
    Posted 5 years ago #

    Hi bartmoss,

    I saw that you resolved your case. I have the same problem and still no resolved.

    Can you help me ?

    Thanks

  4. westerfunk
    Member
    Posted 5 years ago #

    DO THIS AT YOUR OWN RISK!

    BACK UP ALL YOUR FILES BEFORE YOU START MANUALLY EDITING THE CODE!

    I have a solution that works, but it requires some extra code. It differentiates between a web client hitting the web server directly (as in most hosting situations) or hitting the web server through a reverse proxy. I wrote this code a year ago for another site I own that runs behind an Apache reverse proxy I run at home.

    I took this code and then added it to a my-hacks.php file I created in the WordPress root directory. Then, I went into the WP-Admin and turned on the my-hacks.php legacy functionality under Settings, Miscellaneous.

    Here's the code:

    <?php
    
    function writeIPAddress() {
    	if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) == '') {
    		return $_SERVER['REMOTE_ADDR'];
    	}
    
    	else {
    		return $_SERVER['HTTP_X_FORWARDED_FOR'];
    	}
    }
    
    ?>

    Then I took the function "writeIPAddress()" specified above and replaced every instance of "$_SERVER['REMOTE_ADDR']" in each PHP file where an IP address is either called for or displayed (such as "comment.php" under the wp-includes folder). Let me know how this works for you.
    --------------------------------------------

    UPDATE:

    I should refine this a bit further ... the only file I had to update was comment.php and there was only one place in the code where I replaced the function.

  5. kgopika
    Member
    Posted 5 years ago #

    Well we just added

    if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
            $_SERVER['REMOTE_ADDR'] = $list[0];
      }

    to the top of our wp-config.php on kerala online

Topic Closed

This topic has been closed to new replies.

About this Topic