Kundinformation

Molnservrar Hjälp

Configure NGINX Server Blocks - CentOS

Difficulty: 2Time: 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 should install it as part of a LEMP stack:

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

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.

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

Look for the following include line:

include /etc/nginx/conf.d/*.conf;

Directly under this line, add the following line:

include /etc/nginx/sites-enabled/*;

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.

Create a new file to use as a server block template:

sudo vim /etc/nginx/sites-available/template

Inside this file, paste the following content. This has the basics for your NGINX server block.

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:

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.