Securing NGINX & Serving Let’s Encrypt SSL for single domain with multiple root ports on Ubuntu

Rabib Galib
2 min readOct 21, 2020

--

Peace upon you. Securing NGINX server with SSL is easy. Normally Let’s encrypt establishes a secure link between a web server(it represents the domain) & a browser. And we already know that Let’s encrypt is totally free to certify your domains. If you don’t know how to certify domain with one root, this story will assist you to secure your web server.

Now come to the point. In your NGINX server you might have only one domain but you want to make multiple root folders like

So, you want to secure all the root folders with multiple ports like 80, 8080, 8081 & you have only one domain like example.com

If you have not installed your LEMP stack, follow this story. As we know every root folder listens to port 80, for multiple roots here we’ll use other ports too. Once LEMP is installed, we’ll configure NGINX with laravel & react [ I’m showing laravel & react configuration as an example ].

Now run below two commands

sudo rm -rf /etc/nginx/sites-available/default
sudo rm -rf /etc/nginx/sites-enabled/default

Now deploy below command to write config file

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

Paste the below code in your configuration file

Now follow below commands

Now here comes the Let’s Encrypt SSL (Secure Socket Layer). Read & Follow my 2 minutes certbot SSL Encryption story.

Once certbot SSL is done, check the configuration file

nano cd /etc/nginx/sites-available/multiPorts

As we are using one domain [ example.com ]only, certbot will secure & manage port 80 only. If you browse http://example.com:8081 or http://example.com:8082, it will remain unsecure. To secure these ports too, we need to add some manual codes in sites-available/multiPorts file. Here, in below gist, we can see that from line 49–55 & 72–78 we added some manual code to establish the SSL link for 8081 & 8082. And make sure that you’ve kept the domain name same in manual codes.

Now paste the below code in multiports in /etc/nginx/sites-available/ directory.

After this, we need to link this with sites-enabled directory & need to restart nginx again.

It’s all done here. As I said, I’ve shared my laravel & react deployment knowledge here. If you want to know how Laravel & React deployment & configuration works!, then follow my those stories too.

Now your domain with 8081 & 8082 ports are secured too. You can browse those too [ https://example.com:8081 or https://example.com:8082 ].

Have fun 🔥 💪

If you have any query, please put in below. Find me on Github.

--

--

Rabib Galib
Rabib Galib

Written by Rabib Galib

Senior Software Engineer, Masters in Computer Science & Engineering, PHP, Python, Chatbot Expert, AI Research

No responses yet