DEV Community

Ravi Agheda
Ravi Agheda

Posted on • Edited on

How to redirect the domain to the port number using Nginx and set up SSL?

Nginx Configuration Setup

Install Nginx

sudo apt update
sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

Link port with domain

Open this file in nano or vim /etc/nginx/nginx.conf

http{
  server{
    listen 80;
    server_name DOAMIN.com www.DOMAIN.com;
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_pass http://<IP_ADDRESS>;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

After updating nginx.conf apply this commands
Test nginx.conf sudo nginx -t
Restart nginx service sudo systemctl restart nginx or sudo service nginx restart
Check nginx status sudo systemctl status nginx or sudo service nginx status

Issue ssl certificate from certbot

Ref: https://www.nginx.com/blog/using-free-ssltls-certificates-from-lets-encrypt-with-nginx/

Install certbot

sudo apt-get update
sudo apt-get install certbot
sudo apt-get install python-certbot-nginx
Enter fullscreen mode Exit fullscreen mode

Obtain the SSL/TLS Certificate

sudo certbot --nginx -d example.com -d www.example.com

It'll handle the required modification for https in nginx.conf

Automatically Renew Let’s Encrypt Certificates

crontab -e
0 12 * * * /usr/bin/certbot renew --quiet

Manually setup SSL and https configuration on nginx

http{
  server{
    listen 80;
    server_name spotcodes.in www.spotcodes.in;
    return 301 https://$server_name$request_uri;
  }

  server {
    listen 443 ssl;
    server_name spotcodes.in www.spotcodes.in;

    ssl_certificate /etc/letsencrypt/live/spotcodes.in/cert.pem;
    ssl_certificate_key /etc/letsencrypt/live/spotcodes.in/privkey.pem;

    location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_pass http://3.83.202.107:2001;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Test SSL certificate installation

https://www.experte.com/ssl-check

Usefull links and credits

Top comments (2)

Collapse
 
ritik_raj_eb4e6e986982918 profile image
Ritik Raj

I was having trouble configuring Nginx to redirect my domain to a specific port and setting up SSL on my server. After trying various solutions, I finally came across this guide, and it helped me resolve everything step by step.

The part about linking the domain to a specific port using Nginx was particularly tricky for me, but this guide made it clear. The instructions for obtaining and installing SSL certificates with Certbot were super helpful, especially since I was struggling with securing my site.

I was also unsure how to set up automatic renewal for the SSL certificates, but thanks to the Let’s Encrypt and Nginx tutorial, I got that part figured out.

One thing that confused me was configuring the manual SSL setup in Nginx, but after following the detailed steps here, everything worked perfectly. For anyone else who’s struggling with installing Nginx and configuring SSL on Ubuntu 24.04, I also found this guide on installing and setting up Nginx useful.

Thanks for this awesome tutorial—it made all the difference!

Collapse
 
megha_khateek_aec27554ca7 profile image
Megha Khateek

"Great guide! I followed the steps to redirect my domain to a specific port using Nginx, and I also set up SSL with Certbot. I did face some challenges with the SSL certificate installation, especially when configuring it manually on Nginx. I mistakenly missed the server block adjustments in the Nginx configuration file, which caused the SSL setup to fail. After re-reading the documentation, I understood that certbot was supposed to automatically handle the changes, but I had to troubleshoot a bit.

If you're also configuring a server on Ubuntu and looking to install Apache, check out this guide on how to install Apache on Ubuntu 24. It helped me set up a LAMP stack for a different project without any issues. I hope this helps anyone who might be struggling with a similar setup!"

Let me know if you want any adjustments!