Monday, June 22, 2020

Set Wordpress to work behind reverse proxy

When Wordpress is behind reverse proxy, it will think and reply only to HTTP only instead of HTTPS.  But the original request from the reverse proxy will HTTPS, causing a mismatch.

To fix this issue, we simply need to trick Wordpress to think that it is running as HTTPS instead of HTTP.

The reverse proxy will pass the variables:

HTTP_X_FORWARDED_PROTO


Adding the following to wp-config.php will trick Wordpress to think it is running HTTPS:

if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { $_SERVER['HTTPS'] = 'on'; }