WordPress Behind Reverse Proxy

Share This:

Wordpress Behind Reverse Proxy
Wordpress installation are usually straightforward. Unfortunately, if you’re using a reverse proxy like Caddy or NGINX in front of your WordPress webserver, you’ll likely run into issues with too many redirects, images or css not loading, or not being able to log into wp-admin. This post won’t go into the details of setting up your reverse proxy. Assuming the reverse proxy is setup and working correctly, you’ll likely need to make changes to your wp-config.php file to get everything working.

Getting WordPress to Work Behind Reverse Proxy

You’ll need to edit your wp-config.php file (using a file editor or SSH depending on your host).

After your opening PHP tag at the beginning of the file, add these lines of code:

define('.COOKIE_DOMAIN.', 'www.mydomain.com');
define('.SITECOOKIEPATH.', '.');

if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
        $_SERVER['REMOTE_ADDR'] = $list[0];
  }
define( 'WP_HOME', 'https://www.mydomain.com' );
define( 'WP_SITEURL', 'https://www.mydomain.com' );
$_SERVER['HTTP_HOST'] = 'www.mydomain.com';
$_SERVER['REMOTE_ADDR'] = 'https://www.mydomain.com';
$_SERVER[ 'SERVER_ADDR' ] = 'www.mydomain.com';

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
       $_SERVER['HTTPS']='on';

Replace www.mydomain.com with the proxy URL you’re using for your WordPress site. The code above also assumes you’re using HTTPS and SSL on your proxy, so you might need to make some changes if you’re not.

After adding the above code, save the file and refresh your browser to test. You might want to flush your browser cache just for good measure.


Share This:

 

One Response

  1. Wesley Bakker March 27, 2022

Leave a Reply