Configure Nginx Reverse Proxy on Ubuntu VMs + SSL Encryption
Tags: nginx, cloud, azure, security
Published: 2023-05-16 19:31:39

<p>Hi folks! I recently just decided to host my own personal website on a cloud virtual machine. I've experimented a lot of things from doing it and now I'm more than excited to share some of my experiences. This article shows how I configured a reverse proxy and SSL encryption using Nginx.</p>
<hr/>
<h3>What is a reverse proxy?</h3>
<p>It's literally another server that sits in front of our website server. That said, whenever a request comes from a client to our website server, it will be intercepted by the reverse proxy server.</P>
<h3>Why do we even need one?</h3>
<p>There are, of course, advantages of using a reverse proxy.</p>
<ul>
<li><p>Load balancing</p></li>
<li><p>Protection from attacks</p></li>
<li><p>SSL Encryption</p></li>
<li><p>Caching</p></li>
</ul>
<h3>Setting up project</h3>
<h4>Key technologies used</h4>
<ol>
<li>Virtual Machine - A running virtual machine instance. There are many virtual machines available for rent such as <a href="https://aws.amazon.com/ec2/" target="_blank">Amazon EC2</a>, <a href="https://cloud.google.com/compute" target="_blank">Google Compute Engine</a>, <a href="https://azure.microsoft.com/en-ca/products/virtual-machines/" target="_blank">Azure Virtual Machine</a>, etc.</li>
<li>Domain Name - A registered domain name. Domain registrars like <a href="https://www.namecheap.com/" target="_blank">Namecheap</a>, <a href="https://domains.google.com/registrar/" target="_blank">Google Domains</a>, and <a href="https://www.godaddy.com/" target="_blank">GoDaddy</a> provide and let us manage domain names.</li>
<li>Web Application - A server application. View code example on github <a href="https://github.com/wildanazz/demo-web-application" target="_blank">here</a>.</li>
<li>Nginx - Open source software that's often used for web serving and reverse proxying.</li>
</ol>
<h3>How it works</h3>
<h4>Configuring Nginx as a reverse proxy</h4>
<ol>
<li>Install Nginx and make sure it runs successfully.
<pre>
<code>$ sudo apt update</code>
<code>$ sudo apt install nginx</code>
<code>$ sudo service nginx status</code>
</pre></li>
<li>Edit server configuration file in /etc/nginx/sites-available/.
<pre>
<code>$ sudo nano /etc/nginx/sites-available/wildanazz.com</code>
</pre>
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkubsk3ke84557e4cioh.png" alt="NGINX Configuration" /></li>
<li>Make sure that there are no syntax errors in the server configuration file.
<pre>
<code>$ sudo nginx -t</code>
</pre></li>
<li>Enable the server configuration file by creating a link from sites-available directory to sites-enabled directory.
<pre>
<code>$ sudo rm link /etc/nginx/sites-enabled/default</code>
<code>$ sudo ln -s /etc/nginx/sites-available/wildanazz.com /etc/nginx/sites-enabled/wildanazz.com</code>
</pre></li>
</ol>
<h4>Testing application</h4>
<ol>
<li>Clone a demo server application.
<pre>
<code>$ git clone https://github.com/wildanazz/demo-web-application.git</code>
</pre></li>
<li>Run it.
<pre>
<code>$ sudo apt install nodejs && sudo npm install yarn -g</code>
<code>$ cd demo</code>
<code>$ yarn --frozen-lockfile && yarn start</code>
</pre></li>
<li>Copy and enter the ip address of the running virtual machine in a browser.</li>
</ol>
<h4>Installing SSL/TLS certificate</h4>
<p>Before installing SSL/TLS certificate, the registered domain name needs to be pointing to the running virtual machine.</P>
<ol>
<li>Install Certbot to obtain a free SSL certificate.
<pre>
<code>$ sudo snap install --classic certbot</code>
</pre>
</li>
<li>Run this code to let Certbot edit Nginx configuration automatically.
<pre>
<code>$ sudo certbot --nginx</code>
</pre>
</li>
<li>Certbot can also renew the certificate automatically before it expires by running this code.
<pre>
<code>$ sudo certbot renew --dry-run</code>
</pre>
</li>
</ol>
<h3>Summary</h3>
<p>In this article, I discussed how I set up Nginx reverse proxy server and added a SSL certificate for my website from the ground up. If you decide to host your own website on a virtual machine, I highly recommend you to utilize a reverse proxy server. If you have found something I could improve or that you would have done differently? Let me know in the comments.</p>