How To Set Up Nginx Server Blocks
How To Set Up Nginx Server Blocks
Introduction
When using the Nginx web server, server blocks (similar
to the virtual hosts in Apache) can be used to encapsulate
configuration details and host more than one domain off of
a single server.
In this guide, we'll discuss how to configure server blocks
in Nginx on an Ubuntu 16.04 server.
Prerequisites
We're going to be using a non-root user with sudo
privileges throughout this tutorial. If you do not have a
user like this configured, you can create one by following
our Ubuntu 16.04 initial server setup guide.
You will also need to have Nginx installed on your server.
The following guides cover this procedure:
How To Install Nginx on Ubuntu 16.04: Use this guide to
set up Nginx on its own.
How To Install Linux, Nginx, MySQL, PHP (LEMP stack)
in Ubuntu 16.04: Use this guide if you will be using
Nginx in conjunction with MySQL and PHP.
When you have fulfilled these requirements, you can
continue on with this guide.
Example Configuration
For demonstration purposes, we're going to set up two
domains with our Nginx server. The domain names we'll
use in this guide are example.com and test.com.
You can find a guide on how to set up domain names with
DigitalOcean here. If you do not have two spare domain
names to play with, use dummy names for now and we'll
show you later how to configure your local computer to
test your configuration.
Note
Depending on your needs, you might need to adjust the
permissions or ownership of the folders again to allow certain
access to the www-data user. For instance, dynamic sites will
often need this. The specific permissions and ownership
requirements entirely depend on what your configuration.
Follow the recommendations for the specific technology you're
using.
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
First, we need to look at the listen directives. Only one of
our server blocks on the server can have the
default_server option enabled. This specifies which block
should serve a request if the server_name requested does
not match any of the available server blocks. This
shouldn't happen very frequently in real world scenarios
since visitors will be accessing your site through your
domain name.
You can choose to designate one of your sites as the
"default" by including the default_server option in the listen
directive, or you can leave the default server block
enabled, which will serve the content of the /var/www/html
directory if the requested host cannot be found.
In this guide, we'll leave the default server block in place
to server non-matching requests, so we'll remove the
default_server from this and the next server block. You can
choose to add the option to whichever of your server
blocks makes sense to you.
/etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
...
}
Note
You can check that the default_server option is only enabled
in a single active file by typing:
grep -R default_server /etc/nginx/sites-enabled/
If matches are found uncommented in more than on file
(shown in the leftmost column), Nginx will complain about an
invalid configuration.
The next thing we're going to have to adjust is the
document root, specified by the root directive. Point it to
the site's document root that you created:
/etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
}
Next, we need to modify the server_name to match
requests for our first domain. We can additionally add any
aliases that we want to match. We will add a
www.example.com alias to demonstrate.
When you are finished, your file will look something like
this:
/etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
That is all we need for a basic configuration. Save and
close the file to exit.
Create the Second Server Block File
Now that we have our initial server block configuration, we
can use that as a basis for our second file. Copy it over to
create a new file:
sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-
available/test.com
Open the new file with sudo privileges in your editor:
sudo nano /etc/nginx/sites-available/test.com
Again, make sure that you do not use the default_server
option for the listen directive in this file if you've already
used it elsewhere. Adjust the root directive to point to your
second domain's document root and adjust the
server_name to match your second site's domain name
(make sure to include any aliases).
When you are finished, your file will likely look something
like this:
/etc/nginx/sites-available/test.com
server {
listen 80;
listen [::]:80;
root /var/www/test.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
When you are finished, save and close the file.
server_names_hash_bucket_size 64;
...
}
Save and close the file when you are finished.
Next, test to make sure that there are no syntax errors in
any of your Nginx files:
sudo nginx -t
If no problems were found, restart Nginx to enable your
changes:
sudo systemctl restart nginx
Nginx should now be serving both of your domain names.
Step Five: Modify Your Local
Hosts File for Testing(Optional)
If you have not been using domain names that you own
and instead have been using dummy values, you can
modify your local computer's configuration to let you to
temporarily test your Nginx server block configuration.
This will not allow other visitors to view your site correctly,
but it will give you the ability to reach each site
independently and test your configuration. This basically
works by intercepting requests that would usually go to
DNS to resolve domain names. Instead, we can set the IP
addresses we want our local computer to go to when we
request the domain names.
Note
Make sure you are operating on your local computer during
these steps and not your VPS server. You will need to have
root access, be a member of the administrative group, or
otherwise be able to edit system files to do this.
Conclusion
You should now have the ability to create server blocks for
each domain you wish to host from the same server.
There aren't any real limits on the number of server blocks
you can create, so long as your hardware can handle the
traffic