0

I have this website that works but once it is placed behind a load balancer, it gives a 502 error. After some troubleshooting, this snippet is the culprit.

    if( function_exists( 'is_shop' ) && is_shop( ) ){
        // $redirect_page_id = 132; // adjust to ID of page you are redirecting to */
        wp_redirect( '/product-category/main-dish/' , 301 ); exit;
    }
}
add_action( 'template_redirect', 'shop_redirect' );

I want the shop page, which is also the home page, to be a specific product archive page. Hence, I do a redirection. The above snippet works fine but once I added a load balancer, it fails to work. It returns a 502 error. I don't understand why it stops working behind a load balancer.

The error from load balancer is statusDetails: "failed_to_pick_backend"

What I am trying to is to have the product archive page to be the home page. The approach I attempted is to have the home page domain.com to redirect to domain.com/product-category/main-dish/

I've tried many different ways to configure the load balancer and lost track of them. This is the current setting and I've tried with enabling and disabling the code snippet.

Just a recap:

  1. The website works correctly with the redirection snippet enabled.
  2. However, once it is placed behind a load balancer, it produces 502 error
  3. With a load balancer, there is no 502 error when the redirection snippet is disabled
  4. When the redirection snippet is enabled, it produces 502.

enter image description here

enter image description here

enter image description here

jollysea
  • 109
  • 7
  • 1
    Why do you think that code is the problem? You have tagged **google-cloud-load-balancer**. Show the configuration of the load balancer and health check. Show the output from Cloud Logging for 502 errors. – John Hanley Apr 11 '22 at 20:46
  • Hi @JohnHanley Thanks. Once it is placed behind the load balancer, when the code is disabled, it works. When the code is enabled, it returns a 502 error. Hence, I find that the snippet meant for redirection is the problem. – jollysea Apr 12 '22 at 01:23
  • Review the logs to find the actual error. Did you review the logs? If yes, post them in your question. Otherwise, your question does not contain the details required to help you debug your problem and might be closed. – John Hanley Apr 12 '22 at 01:39
  • Hi @JohnHanley Just added to the OP. the error is `statusDetails: "failed_to_pick_backend"` – jollysea Apr 12 '22 at 03:14
  • The error **failed_to_pick_backend** means that the load balancer could not select a healthy backend. Your health checks are failing. Is your code in the health check path? – John Hanley Apr 12 '22 at 03:52
  • Hi @JohnHanley I'm not sure if I understand correctly. Do you mean if the path for health check is set to `/product-category/wallet/` as in my case? – jollysea Apr 12 '22 at 04:44
  • What is the health check endpoint? Go back to my first comment asking for details. – John Hanley Apr 12 '22 at 06:37
  • @JohnHanley Have them up there – jollysea Apr 12 '22 at 16:37

1 Answers1

0

Check the health check you created while creating load balancer , add health check firewall rule if health check is showing unhealthy status even though instance is working fine.

Vishal Bulbule
  • 313
  • 1
  • 7