Server Blocks/Virtual Host Setup For Nginx

When running a web server, you often want the ability to host multiple websites on one machine. Configuring a server in this manner reduces hardware and power costs. Modern web server software accomplishes this via virtual hosts. In Nginx, the equivalent functionality is called server blocks. The concept of a virtual host exists in almost all major web server packages. Nginx, Apache, Lighttpd and IIS all have their own implementations. Using virtual hosts, you will be able to serve many different server blocks from one port on Nginx. The server pulls the domain name from the request sent by the user’s web browser. Then, the server handles the request by passing it to a separate backend server or by processing it in the same instance. This is particularly helpful when trying to set up something like Linux containers. The “servers” have custom access settings, TLS configurations, directory layouts and other features.

Configuring Hosts for Server Blocks

In this brief tutorial, we are going to set up several virtual hosts on an installation of Nginx. If you want to use your own Internet domains, you’ll need to log into your control panel and create A records to the IP address of your server.

If you do not wish to use Internet domains, you’ll need to modify your hosts file. On Linux and other Unix-like operating systems, this file can be found at /etc/hosts. Open this file in your favorite editor and add the following at the bottom of the file:

127.0.0.1 server1.local 127.0.0.1 subdomain.server1.local
Code language: CSS (css)

You must format these entries with tabs. They should match the other entries in the file. We have just instructed our computer to locally assign these domain names to these IPs. If we now go into the terminal and run ping server1.local we should successfully receive a response from our loopback address.

Installing Nginx

With our domains properly configured, it is time to install Nginx. On Debian Linux and its derivatives, open up the terminal and run the following:

sudo apt install nginx

This command will install Nginx. Now, we need to start and enable the service before it will run.

sudo systemctl enable nginx sudo systemctl start nginx

We now have our web server installed and running. Open your web browser and go to 127.0.0.1. You should see a greeting message from Nginx.

How to Set Up Server Blocks in Nginx

Now that we have nginx configured, it is time to configure our virtual domains. At this point, if you were to go to any of our domains in your browser, you’d get the same message as for 127.0.0.1. This is due to the fact that Nginx is currently ignoring which domain the browser is asking for. Instead, it is always returning the default virtual server.

Server Blocks/Virtual Hosts Configuration

To set up a virtual host, open /etc/nginx/sites-enabled/server1.local.conf file. The http blocks within the Nginx configuration contain the defined virtual hosts. These blocks need at a minimum a port to listen on, a domain to listen for, and a root directory to pull files from.

server { listen 80; root /usr/share/nginx/first_domain; server_name server1.local; }

Now, reload the Nginx server by running the following:

sudo systemctl reload nginx

Note that reload and restart are not the same thing. In particular, reload simply instructs Nginx to reload its configuration files from disk. It will not stop running if any of the files have syntax errors. Instead, it will continue running with the configuration from before. In contrast, the restart command stops the server on incorrect config formatting. This doesn’t make a huge difference for us now. However, on production servers this can be a huge problem. It can potentially take our sites offline until we fix the offending lines in the files. This is especially a problem if you are running a large number of websites all on the same instance. Having one site go down is bad. Having thirty sites all go down at once is catastrophic.

Creating the Root Directory for the Server Block

Now try accessing server1.local again. You should receive a 404 error. This is normal. We have not yet created any directories for it and therefore it is not locating the index file properly. Create /usr/share/nginx/first_domain. Now, go ahead and run touch /usr/share/nginx/first_domain/index.html. We have now created the index file. At this point, going to server1.local would give us a blank page rather than a 404 error. Let’s add some content.

<html> <head> <title>Test Website</title> </head> <body> This is a <strong>Test</strong> </body> </html>
Code language: HTML, XML (xml)

Reloading the page will now render our code. We now have our first functional virtual host. At this point it is only capable of serving static files from the root directory.

Hosting Multiple Websites Under One Domain With Subdomains

Now, let’s go ahead and configure a virtual host for the subdomain we defined earlier. Insert the following into /etc/nginx/sites-enabled/subdomain.local.conf.

server { listen 80; root /usr/share/nginx/subdomain; server_name subdomain.server1.local; }

Now, once again reload Nginx by running sudo systemctl reload nginx. If you now attempt to access this subdomain, you will receive the same 404 error. You will now be able to serve static files from this virtual server by assigning a root directory to it. The server_name directive is very flexible. You can make one virtual server match to multiple different domains by using wildcards (*).

Conclusion

To summarize, server blocks are an extremely powerful tool which allow you to host many different websites on one server. You are even able to host many different websites on one domain via the use of subdomains. These virtual hosts allow you to consolidate website hosting and therefore greatly help to reduce costs. They’re a vital tool for system administrators and are an essential part of any Nginx user’s toolkit.

Leave a Reply

Your email address will not be published. Required fields are marked *