awsdocs / elastic-beanstalk-samples

This repository contains code and configuration samples (e.g. .ebextensions) for AWS Elastic Beanstalk.
Apache License 2.0
1.22k stars 889 forks source link

https-redirect-nodejs.config not working on new platform #132

Open tamslinn opened 4 years ago

tamslinn commented 4 years ago

I put the file https-redirect-nodejs.config into .ebextensions and confirmed this was part of the deployment archive, but it had no effect - http was not redirected to https.

I think the file is executed and creates a file in /etc/nginx/conf.d as required but then later in the deployment the directory is overwritten.

I got it to work by placing the contents of the file which would have been written by https-redirect-nodejs.config into .platform/nginx/conf.d/https-redirect-nodejs.conf

I wonder if the instructions need updating for new platform versions?

Gr8z commented 4 years ago

Could you send me that file? @tamslinn

I am struggling to get this to work.

tamslinn commented 4 years ago

Apologies I missed your comment. This is what I put in .platform/nginx/conf.d/https-redirect-nodejs.conf. I just took this from https-redirect-nodejs.config in this repository.

Note I had to make sure the node app I was deploying was listening on 8081


{upstream nodejs {
   server 127.0.0.1:8081;
   keepalive 256;
}

server {
   listen 8080;

   if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
       set $year $1;
       set $month $2;
       set $day $3;
       set $hour $4;
   }
   access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
   access_log  /var/log/nginx/access.log  main;

   location / {
       set $redirect 0;
       if ($http_x_forwarded_proto != "https") {
         set $redirect 1;
       }
       if ($http_user_agent ~* "ELB-HealthChecker") {
         set $redirect 0;
       }
       if ($redirect = 1) {
         return 301 https://$host$request_uri;
       }

       proxy_pass  http://nodejs;
       proxy_set_header   Connection "";
       proxy_http_version 1.1;
       proxy_set_header        Host            $host;
       proxy_set_header        X-Real-IP       $remote_addr;
       proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }

gzip on;
gzip_comp_level 4;
gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

}```
bnbon commented 3 years ago

Anyone else having this issue? The post above is invalid, I tried correcting the obvious flaws, but couldn't get it to work either.