Warm tip: This article is reproduced from stackoverflow.com, please click
amazon-elastic-beanstalk amazon-web-services https ssl wordpress

AWS Elastic Beanstalk app initial access is not secure, but subsequent redirect is

发布于 2020-04-10 16:18:19

I am using Elastic Beanstalk (with load balancer) to support a Wordpress site. I have setup SSL through the Certificate Manager (ACM) and enabled the load balancer to listen on Port 443 for HTTPS. I have also tried to force HTTPS on my Wordpress site by adding the following lines to the wp_config.php in my source bundle (as suggested by many posts):

define('WP_HOME','https://example.com');
define('WP_SITEURL','https://example.com');
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
       $_SERVER['HTTPS']='on';

However, my problem is that when I try to access my site, the initial access is not secure, ie a HTTP connection, but when I try to navigate to other pages and back on my site, the connection immediately becomes a secure HTTPS connection. I am wondering what can I do to resolve this issue such that upon initial access to the site, HTTPS is forced. I also ran into "Too many redirects" errors before because of trying to force HTTPS and Wordpress makes it a bit tricky doing that, so I am trying to avoid that. Any help is much appreciated. Thanks!

Questioner
EdenHazard
Viewed
87
markwalker_ 2020-02-02 06:17

You can add elastic beanstalk extensions to your environment. Typically you would include an .ebextensions folder in the root of your app to include each of your extensions. (docs)

If you're running nginx as your proxy server which I'll assume you are, add a file called 10-nginx.config to the extensions directory.

files:
  "/etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf":
    content: |
      server {
          listen 80 default_server;

          server_name _;

          return 301 https://$host$request_uri;
      }

However you should also consider HTTPS termination on your load balancer & having the application running on port 80 within the environment network.

Docs on this are here; https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-httpredirect.html