How to host your website with Nginx

How to host your website with Nginx

NGINX is a high‑performance, highly scalable, highly available web server, reverse proxy server, and web accelerator (combining the features of an HTTP load balancer, content cache, and more).

Now, keep in mind that there are many options when it comes to hosting static websites nowadays --- Github pages, any number of hosting providers, Amazon S3 or Cloudfront, Netlify etc.


  • Linux server/VM - check part 1 of this series

  • Linux Command-line

  • For the purposes of this guide, we'll use the domain and the IP address as examples.

Step 2: Domain, pointing it to the right address

You need to point your domain to the new server you created. You can do this by creating an A record in your hosting provider's DNS settings, pointing your domain name (eg. to the server IP address (eg. If you don't want to wait for the DNS to propagate, edit your /etc/hosts file to point your domain to the right IP address. (⚠ Don't forget switching out with your actual domain name and IP address as needed when you encounter these.)

Step 3: Install NGINX

ssh into your server and install NGINX. If using Ubuntu, you can run:

sudo apt-get update

sudo apt-get install nginx

Step 4: Website's static files

You can't deliver your website if the server doesn't have your files, so let's add your files to the server.

By default, NGINX expects your static files to be in a specific directory (which varies). You can override this in the configuration. For now, let's assume that you'll be putting your website's static files in the /var/www/ directory.

Create a directory in /var/www/ called This is where your static website's files will go.

Copy your website's static files into that folder. You can use the scp command from your local machine. cd into your website's directory and run:

scp -r * root@

(⚠ Don't forget to switch the 127.0.01 and with values relevant to you.)

If you don't have a website just yet, you can create a file called index.html with some "Coming soon" text.

Step 4: Configuring NGINX

We will now tell Nginx on how to serve our website.

cd into /etc/nginx/. This is the location for Nginx configuration files.

The two important directories are sites-available and sites-enabled.

  • sites-available contains individual configuration files for all of your possible static websites.

  • sites-enabled contains symbolic links to the configuration files that NGINX will actually read and run.

What we're going to do is create a configuration file in sites-available, and then create a symbolic link (a pointer) to that file in sites-enabled to actually tell NGINX to run it.

Create a file called in the sites-available directory and add the following text to it: (you can use the touch command to create the

server {

 listen 80 default_server;

 listen [::]:80 default_server;

 root /var/www/;

 index index.html;


 location / {

   try_files $uri $uri/ =404;



(⚠ Don't forget using your own domain instead of

This file tells NGINX several things:

  • Deliver files from the /var/www/ directory

  • The main page is called index.html.

  • Requests that are requesting should be served by this server block.

  • You might be wondering why www is listed separately. This tells nginx to also route requests starting with www to the site. There's actually nothing special about the www --- it's treated like any other subdomain.

Now that the file is created, we'll now create the symbolic link in the sites-enabled directory and in order to do so, we will use the following syntax:


The actual syntax will look like:

ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Now, restart NGINX to see your site! 🎉

sudo systemctl restart nginx

If it gives you an error ☹, there's likely a syntax error. You can stop here if you'd like, but you can also continue for some more optimization.

Please hit the ♥ if you found it useful! You can also follow me on Twitter.