0

I have a wordpress site with nginx packed into a Docker image behind an AWS ELB Load Balancer. Only HTTPS is enabled on the load balancer.

When I access the URL: https://example.com/wp-admin/ it works fine but when I access the URL:

https://example.com/wp-admin the server fails as it tries to redirect to url: http://example.com/wp-admin/

I found this thread which has similar issue and applies all three possible solution options suggested but it didn't workout for me.

Here is my nginx config file for the site

How do I prevent the HTTPS to HTTP redirect or redirect from HTTPS to HTTPS e.g: https://example.com/wp-admin to https://example.com/wp-admin/

My wp-config.php

Pawan Rai
  • 101
  • 1
  • I might be missing something but after reading your config files I cannot see where Nginx is handling https at all. Do you have another config including https, it is more likely that is where the issue is because it redirects from https to http. – Admiral Noisy Bottom Sep 07 '20 at 08:47
  • Oh wait, I've also just seen the proxy_pass statement. Is Nginx acting as a reverse proxy? – Admiral Noisy Bottom Sep 07 '20 at 08:49
  • Yes. And https is being handeled by aws elb. – Pawan Rai Sep 07 '20 at 08:51
  • You explicitly redirect to `http` with: `rewrite /wp-admin$ $scheme://$host$uri/ permanent;` - that statement is not necessary as the `try_files` should perform that function for you anyway. – Richard Smith Sep 07 '20 at 08:52
  • @RichardSmith thank you very much for pointing it out for me. Sure I'll make the changes. – Pawan Rai Sep 07 '20 at 08:54
  • I'm not an expert but in my experience if Nginx is the front caching server it should handle https and the backend is http. My setup is Nginx in front with Apache at the back end and the only way to make it work with https was let Nginx do it. Having said that, I know nothing about Amazon services. Was the site originally http? If so, after moving to https it is important to update the wordpress site & home URL under settings too. – Admiral Noisy Bottom Sep 07 '20 at 09:00
  • As a side note to my previous comment I also had to add the following to wp-config.php; "if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') $_SERVER['HTTPS']='on'; if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST']; – Admiral Noisy Bottom Sep 07 '20 at 09:02
  • In addition, if you must use rewrite statements you can rewrite https:// rather than rewrite $scheme. – Admiral Noisy Bottom Sep 07 '20 at 09:35
  • @AdmiralNoisyBottom I'd update my wp-config, checked the wordpress url settings (it is in https). Still the site breaks without the trailing `/` at the end. – Pawan Rai Sep 08 '20 at 04:27
  • @PawanRai Perhaps this url might be useful; https://stackoverflow.com/questions/645853/add-slash-to-the-end-of-every-url-need-rewrite-rule-for-nginx – Admiral Noisy Bottom Sep 08 '20 at 11:28
  • @AdmiralNoisyBottom solved the issue, – Pawan Rai Sep 13 '20 at 04:10
  • @PawanRai If you resolved it could you add it here as an answer and mark it correct so others can benefit. :) – Admiral Noisy Bottom Sep 14 '20 at 05:56

1 Answers1

0

Adding:

absolute_redirect off;
if ($http_x_forwarded_proto = "http") {
    return 301 https://$host$request_uri;
}

in my nginx config file for the site solved the issue for me.

Pawan Rai
  • 101
  • 1