Cannot login to WordPress Admin with SSL terminated load balancer

Running WordPress 4.7.3 with a HAProxy (version 1.7.2) frontend and
Apache 2.4.10 with PHP 5.6 backend.

This is all under Debian 8.7 as well.

X-Forwarded-For and X-Forwarded-Proto are set correctly by HAProxy.

The issue I’m having is when I have SSL terminated on the HAProxy frontend (running http mode) and login to WordPress Admin I get the error

Sorry, you are not allowed to access this page.

I tried the steps from here but they didn’t fix the issue.

At the bottom of my wp-config.php I have

define('FORCE_SSL_ADMIN', true);
// in some setups HTTP_X_FORWARDED_PROTO might contain
// a comma-separated list e.g. http,https
// so check for https existence
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
       $_SERVER['HTTPS']='on';

I tried a clean install of WordPress (was importing a clients side of an earlier WordPress for spring cleaning, but that didn’t fix the issue either).

Solutions Collecting From Web of "Cannot login to WordPress Admin with SSL terminated load balancer"

I just figured out the issue.

You need to add the HTTPS/SSL settings before require_once(ABSPATH . 'wp-settings.php'); in your wp-config.php

So the relevant part of your wp-config.php should look like this.

define( 'FORCE_SSL_ADMIN', true );

// in some setups HTTP_X_FORWARDED_PROTO might contain
// a comma-separated list e.g. http,https
// so check for https existence
if( false !== strpos( $_SERVER['HTTP_X_FORWARDED_PROTO'], 'https' ) ) {
    $_SERVER['HTTPS'] = 'on';
}

/** Sets up WordPress vars and included files. */
require_once( ABSPATH . 'wp-settings.php' );