I'm trying to setup Google Load Balancer and couldn't get it work. It produces either 502 or 302 error.

Wanting to direct www to non-www, http to https.

This is my rewrite rule

    SetEnvIf X-Forwarded-Proto https HTTPS=on
    <VirtualHost _default_:80>
      DocumentRoot "/opt/bitnami/apache/htdocs"
      Include "/opt/bitnami/apps/letsencrypt/conf/httpd-prefix.conf"
      <IfModule mod_proxy.c>
        ProxyPass /.well-known !
      </IfModule>
      RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
      RewriteCond %{HTTP_HOST} !^localhost
      RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
      RewriteCond %{REQUEST_URI} !^/\.well-known
      RewriteRule ^(.*)$ http://%1$1 [R=permanent,L]
<VirtualHost _default_:443>

  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteCond %{HTTP_HOST} !^localhost
  RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
  RewriteCond %{REQUEST_URI} !^/\.well-known
  RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]

Google Load Balancer has only 1 backend service. Backend is configured for HTTP and frontend is HTTPS with 60s timeout.

  • The instance is accessible via both instance's IP and load balancer's IP
  • www to non-www is not working.
  • Http gives a 404 error.
  • https gives a 502 / 302 error.

Are the rewrite rules all wrong?


Solution 1:

As per my understanding you are trying to configure http to https in a external HTTP(S) Load Balancer, it seems that you set up a URL Rewrite rules in apache instead redirects in GCP, based on the traffic management for HTTP(S) Load Balancers, redirects includes the capability to redirect all HTTP requests to HTTPS requests, here’s an example to set up http-https redirect