Search code examples
amazon-web-servicesnginxamazon-ec2lets-encrypt

NGINX on AWS EC2 to forward HTTPS to HTTP://localhost


I have some dockers containers deployed on AWS EC2, that listens on http. My idea is using nginx as reverse proxy, to pass traffic from https, to http://localhost.

Each container listens on a specific http port. The EC2 instance will accept traffic just on http:80 and http:443 and I will use subdomain to chose the right port. So I should have:

https://backend.my_ec2instance.com --> http://localhost:4000
https://frontend.my_ec2instance.com --> http://localhost:5000

I'v got my free TSL certificate from Let's Encrypt (it's just on file containing public and private keys), and put it in

/etc/nginx/ssl/letsencrypt.pem

Then I have configured nginx in this way

sudo nano /etc/nginx/sites-enabled/custom.conf

and wrote

server {
       listen 443 ssl;
       server_name backend.my_ec2instance;
       # Certificate
       ssl_certificate letsencrypt.pem;

       # Private Key
       ssl_certificate_key letsencrypt.pem;

       # Forward
       location / {
               proxy_pass http://localhost:4000;
               }
}
server {
       listen 443 ssl;
       server_name frontend.my_ec2instance;
       # Certificate
       ssl_certificate letsencrypt.pem;

       # Private Key
       ssl_certificate_key letsencrypt.pem;

       # Forward
       location / {
               proxy_pass http://localhost:5000;
               }
}

then

sudo ln -s /etc/nginx/sites-available/custom.conf /etc/nginx/sites-enbled/

Anyway, if I open my browser on https://backend.my_ec2instance it's not reachable. http://localhost:80 instead correctly shows the nginx page.


Solution

  • HTTPS default port is port 443. HTTP default port is port 80. So this: https://localhost:80 makes no sense. You are trying to use the HTTPS protocol on an HTTP port.

    In either case, I don't understand why you are entering localhost in your web browser at all. You should be trying to open https://frontend.my_ec2instance.com in your web browser. The locahost address in your web browser would refer to your local laptop, not an EC2 server.


    Per the discussion in the comments you also need to include your custom Nginx config in the base configuration file.