0

When accessing Google Compute Engine with wordpress installed via Cloud Load Balancing from a browser, a redirect loop occurs.
Chrome returns ERR_TOO_MANY_REDIRECTS, and when I open the network inspector or Cloud Logging, I see that many 301s are running.

Configuration

  • Cloud Load Balancing
    • Frontend: HTTP and HTTPS protocol
    • Host and path rules: all unmatched (default) -> following Backend
    • Backend: Instance group with HTTP protocol that contains just one of the following Google Compute Engines VM instance with HTTP Protocol
  • VM instance
    • Installed on the VM instance: Apache2 2.4.38 , Google-Fluentd 1.9.3 , MySQL-Client 5.7.36 , MySQL-Server 5.7.36 , PHP 7.4.25 , Stackdriver-Agent 6.1.4 , WP-CLI 2.5.0 , WordPress 5.8.1 , phpMyAdmin 5.0.2 (These were installed by WorkPress(Google Click to Deploy))

What I tried

  • Hypothesis that WordPress is issuing redirects due to HTTP (not HTTPS) from the load balancer.
    • I added the WordPress plugin SSL Insecure Content Fixer and changed the https detection setting to configure X-Forwarded-Proto header.

Other Information

  • The permalink setting of wordpress is https://~ (not http://~).
  • If you set the IP address of the Google Compute Engine instance to DNS A record instead of the Cloud Load Balancing IP, you can access the site.

How can I fix the redirect loop and access wordpress with the URL of the Load Balancer IP address?

enter image description here

enter image description here

kusumoto_teruya
  • 2,415
  • 4
  • 23
  • 38

1 Answers1

0

You have a typical setup with HTTP and HTTPS for the frontend and HTTP for the backend. This is called SSL offloading.

The problem is that your backend code is not detecting that the client connected to the load balancer using HTTPS. The HTTP header HTTP_X_FORWARDED_PROTO indicates the client connection protocol (HTTP/HTTPS).

This is easily fixed by editing the WordPress wp-config.php file and adding the following code at the bottom.

if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
{
       $_SERVER['HTTPS'] = 'on';
}

X-Forwarded-Proto

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • I added above code to `/var/www/html/wp-config.php` and tried to access the site with a browser. The problem is still happening... – kusumoto_teruya Jan 12 '22 at 06:35
  • Are you still experiencing the issue ? If so maybe try recreating the load balancer and see if that worked. – Wojtek_B Jan 12 '22 at 09:17
  • I recreated the load balancer, but the redirect loop is still occurring... – kusumoto_teruya Jan 12 '22 at 15:15
  • @kusumoto_teruya You may have more than one place that is redirecting. Edit your question with details on what is configured on the VM instance. For example, Apache/Nginx can also issue redirects. – John Hanley Jan 12 '22 at 19:06
  • @JohnHanley I followed your advice and added the VM instance configurations. I am using Apache. Currently I have no idea how to know who is issuing them. – kusumoto_teruya Jan 13 '22 at 15:09
  • @kusumoto_teruya - Do you know how to configure Apache? Read a basic tutorial on how to configure a website, redirects, etc. There are numerous methods to enforce HTTPS - virtual hosts, .htaccess, etc. – John Hanley Jan 13 '22 at 19:32