Open both HTTP and HTTPS Ports same using Nginx and Certbot

Open both HTTP and HTTPS Ports same using Nginx and Certbot

Yes, we can say that stopping auto redirection from Port 80 to Port 443 is straightforward using a load balancer and target group. However, it's not as simple with Nginx and Certbot. In this blog, I am sharing the steps so you can open both ports simultaneously.

Step 1

Your EC2 should be deployed

Step 2

I am using here reverse proxy for the security perspective

sudo apt update
sudo apt instal nginx -y
sudo apt update
sudo apt install certbot python3-certbot-nginx

Step 3

Here I am not sharing the docker installation and containerization steps, Use the below command to open nginx configuration

sudo nano /etc/nginx/sites-available/default

Paste the Reverse proxy script

Reverse Proxy Script with Socket

server {
        server_name domain_name;
        #root /var/www/example.com;
        #index index.html;
        location / {
                proxy_pass http://localhost:7403/;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header Host $host;
        }




    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain-name.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain-name.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

Step 4 (Final Step)

Now paste the above script twice

server {
        server_name domain name;
        #root /var/www/example.com;
        #index index.html;
        location / {
                proxy_pass http://localhost:7403/;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header Host $host;
        }




    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain-name.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain-name.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
        listen 80;
        server_name domain name;
        #root /var/www/example.com;
        #index index.html;
        location / {
                proxy_pass http://localhost:7403/;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header Host $host;
        }
}
sudo nginx -t

If the test pass, then restart the nginx service

sudo systemctl restart nginx

Now you'll be able to access your domain with or without https at the same time

domain-name.com

domain-name.com

Thank you for reading my blog.

LinkedIn