0

I have running server nodeJs with express module. And website is made by React.js which is server-side-redering.

And I have domain : www.example.com, Route53 with load balancer applied AWS Certificate Manager.

the problem is I don't know how to automatically access https:// protocol, when I type url http://www.example.com

now, I can access each of url like as http://www.example.com or https://www.example.com

I found solution :

app.use (function (enter code herereq, res, next) {
    if (req.secure) {
        next();
    } else {
        res.redirect('https://' + req.headers.host + req.url);
    }
});

but it's not working...

what is nice way???


update

  1. if using nodejs with express, how to set a port? the express block port 80, So I use sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000. is it common method to connect port?

  2. I have load balancer using SSL (ACM), and port using port are 80, 443. how to directly access that either 'http' or 'https should access https.

XOneto9
  • 15
  • 7

1 Answers1

1

When deploying NodeJS applications it is recommended to use a web server like Nginx or Apache infront of Node server as a proxy, for gzip encoding, static file serving, HTTP caching, SSL handling, load balancing and etc.

  • One option is to have the redirection at proxy level
  • In AWS another option is to use Cloudfront with http to https redirect for behavior of the origin (Also caching static content at Edge Location level)
Ashan
  • 18,898
  • 4
  • 47
  • 67
  • thanks to reply. :), and, I need a little complement explain :) both of solutions, which one is more common methods? – XOneto9 May 31 '17 at 01:05
  • I use CloudFront always unless its websocket functionality there in Node App since Edge caching for static content improves application performance. Also do the SSL termination at CloudFront level with SSL certificates issued by AWS certificate manager. – Ashan May 31 '17 at 02:10
  • wow, are you using CloudFront? how about using Ngine x ? I find solution about proxy level you told me. So, one of solutions using Nginx is applied by lots of people. how about that using nginx?? – XOneto9 May 31 '17 at 10:08