Configure NGINX Server Blocks - CentOS

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 - - 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 should 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/
  2. Give this directory the correct permissions ownership for the NGINX user:
    sudo chown -R nginx:nginx /usr/share/nginx/
    sudo chmod -R 755 /usr/share/nginx/

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/
  2. Paste this example HTML file in the editor. You can use your own text and domain as you see fit.
        <h1>Hooray! NGINX is properly serving on!</h1>
  3. Save and close the file:

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. You do need to add another line to your NGINX configuration file, so go ahead and open that in a text editor.
    sudo vim /etc/nginx/nginx.conf
  3. Look for the following include line:
    include /etc/nginx/conf.d/*.conf;
  4. Directly under this line, add the following line:
    include /etc/nginx/sites-enabled/*;
  5. Save and close the file:

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
  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;

Note: 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:
  • 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/
    2. Open your config file:
      sudo vim /etc/nginx/sites-available/
    3. Replace all instances of |YOUR_DOMAIN| with the domain name you're using.
    4. Save and close the file:
    5. Add a symbolic link in the sites-enabled directory to enable this server block.
      sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
    6. Restart NGINX to confirm all your changes:
      sudo service nginx restart

      Note: 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.

    Var den här artikeln till hjälp?
    Tack för din feedback. Ring vårt supportnummer eller starta chattalternativet ovan om du vill prata med en medarbetare på kundtjänst.
    Vi är glada att vi kunde hjälpa till! Finns det något mer vi kan göra för dig?
    Det var tråkigt att höra. Berätta vad som var krångligt eller varför lösningen inte hjälpte dig med problemet.