Configure NGINX Server Blocks - Arch Linux

Difficulty: 2
Time: 30 minutes

If you want to host websites on your server running NGINX (which you probably do), you can use NGINX server blocks. Server blocks are the NGINX equivalent of Apache's virtual hosts - they let you direct incoming HTTP traffic for a domain name to a particular directory on your server.

Server blocks are most often recommended when you want to host multiple websites on the same server, but we recommend creating server blocks for all of your websites including the first.

Throughout this article, we'll use an example domain - coolexample.com - but you should replace it with the domain name or subdomain you want to host on your server.

Install NGINX

To get NGINX on your server, you can either install it as part of a LEMP stack.

Create your website's root directory

For each site you want to host, we're going to create a directory in /usr/share/nginx/.

  1. Create a directory for your domain:
    sudo mkdir -p /usr/share/nginx/coolexample.com
  2. Give this directory the correct permissions ownership for the NGINX user:
    sudo chown -R http:http /usr/share/nginx/coolexample.com
    sudo chmod -R 755 /usr/share/nginx/coolexample.com

You now have a directory for your site. Repeat this for any more sites that you want to host.

Create content for the website

If you have the content for the website prepped, you can upload it to the website's directory you created in the last section.

If you don't have content ready to upload, you can create a sample home page (also known as an index file, which is the first page that loads when visitors come to your domain).

  1. Create a test index file.
    sudo vim /usr/share/nginx/coolexample.com/index.html
  2. Paste this example HTML file in the editor. You can use your own text and domain as you see fit.
    <html>
      <head>
        <title>coolexample.com</title>
      </head>
      <body>
        <h1>Hooray! NGINX is properly serving on coolexample.com!</h1>
      </body>
    </html>
  3. Save and close the file:
    :wq!

Create the NGINX server block

Now for the important part. You will create the file and directory for the NGINX server block information. This guide will set this up in a way that will make it simple to add more sites in the future.

Create and configure your directories

  1. Create the following directories:
    sudo mkdir /etc/nginx/sites-enabled
    sudo mkdir /etc/nginx/sites-available
    This sort of directory system is typically used by Debian and Ubuntu, but they can be used by any Linux distribution running NGINX.
  2. We need to modify your NGINX configuration file, so open that now:
    sudo vim /etc/nginx/nginx.conf
  3. Look for the following include line:
    include mime.types;
  4. Directly under this line, add the following line:
    include /etc/nginx/sites-enabled/*;
  5. Save and close the file:
    :wq!

Create a server block template

By creating a template, you'll have something to work from whenever you want to add new websites to your server.

  1. Create a new file to use as a server block template:
    sudo vim /etc/nginx/sites-available/template
    You'll be able to use this template for creating sites in the future.
  2. Inside this file, paste the following content. This has the basics for your NGINX server block.
    server {
      listen  80;
      server_name |YOUR_DOMAIN|  www.|YOUR_DOMAIN|;
      location / {
        root   /usr/share/nginx/|YOUR_DOMAIN|;
        index index.php index.html index.html;
      }
    }

Leave in the value |YOUR_DOMAIN|; do not replace it with your domain name right now. You will replace this later when you set up additional server blocks.

  • Save and close the file:
    :wq!
  • Create a server block for your site

    1. Copy the template for your new server block:
      sudo cp /etc/nginx/sites-available/template /etc/nginx/sites-available/coolexample.com
    2. Open your config file:
      sudo vim /etc/nginx/sites-available/coolexample.com
    3. Replace all instances of |YOUR_DOMAIN| with the domain name you're using.
    4. Save and close the file:
      :wq!
    5. Add a symbolic link in the sites-enabled directory to enable this server block.
      sudo ln -s /etc/nginx/sites-available/coolexample.com /etc/nginx/sites-enabled/coolexample.com
    6. Restart NGINX to confirm all your changes.
      sudo systemctl restart nginx

      If you get an error when restarting NGINX, it is likely that you have not properly changed the server block file in the sites-available directory. Make sure that you have all your { } brackets closed and that all directives end in a semicolon.

    Point your domain name to your server

    If your domain name isn't currently loading another website, you should point it to your server to test your new config.

    How you do this depends on where your domain name is registered and whose server you're using:

    Domain registered? Server hosted? Do this...
    GoDaddy GoDaddy Point your domain name to a server
    Another company GoDaddy Find a server's public IP address and then update your domain name's primary ("@") A record.
    GoDaddy Another company Find your server's IP address, and then change your domain's IP address to use it.
    Another company Another company Find your server's IP address, and then change your domain's IP address to use it.

    Changes to your domain can take up to 48 hours to display across the internet. However, once they do, you can visit your domain name and view the test page you created earlier!

    Adding more server blocks

    Because we had the foresight to create a template for our configuration, you can easily add more server blocks by just completing the steps in the Create a server block for your site and Point your domain name to your server sections.

    Next step

    Now that you have NGINX up and running, you can do things like install WordPress or Drupal on your sites.


    Was This Article Helpful?
    Thank You For Your Feedback
    Glad we helped! Anything more we can do for you?
    Sorry about that. How can we be more helpful?