Search code examples
wordpressamazon-web-servicesnginxamazon-ec2aws-application-load-balancer

error when accessing wordpress that is behind an alb


In short, I'm using AWS, I have an instance that is private, and receives traffic through the alb, the installation process works ok, but after installation, access to Wordpress is lost.

error

I uploaded another machine privately with nginx, and this machine receives traffic from the alb, however, it redirects to the site successfully, but when I try to log into Wordpress to manage it, it gives me the same error.

my site

When passing the domain/login or wp-admin I should land on the access page, and even when I was landing on that page I tried to authenticate but without success.

Anyway, I already tried using the instance behind the alb and also using nginx behind the alb, doing the reverse proxy but I couldn't make it work.

I don't know if anyone has already tried this type of configuration.

There are several videos doing it, but the idea is to have more security by keeping the instance in private subnets, and all I think is leaving the resources publicly accessible, adding the MySQL database part, I'm using RDS.

this is something very specific to wordpress in my opinion

I tried using wordpress in a private instance behind the alb and also using another ec2 with nginx revers proxy but in both cases without success.

Access to the site via domain works, what doesn't work is access to the WordPress administration page


Solution

  • The "Too many redirects" error your browser is showing indicates that WordPress is continuing to issue redirects because it thinks the requests it is receiving from the load balancer are either not the correct protocol, or not the correct domain name.

    If you have an SSL certificate configured on the load balancer, then you need to configure WordPress to be aware that the SSL certificate is being served by a proxy in front of WordPress. To do that, you need to add the following to wp-config.php:

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

    To configure WordPress to be aware of the load balancer's domain name (the domain name you are entering in your web browser), you need to configure the following to wp-config.php:

    define('WP_HOME', 'https://your-domain.com'); 
    define('WP_SITEURL', 'https://your-domain.com');
    

    Note that this needs to be exactly what is in the web browser address bar when you are trying to access the website. If you have SSL configured then make sure you use https:// here, otherwise use http://. If you are using a subdomain like www.your-domain.com then make sure you specify that exactly.