A server block file contains configuration directives for a website, including a site’s document root, security policies, SSL certificate configuration, and much more. Each website that is configured within an Nginx server block operates independently of each other with separate and unique settings. The Nginx server block feature allows webmasters to maximize server resources by running multiple websites on one host instead of multiple hosts running multiple websites.

How to create website directory structures on Ubuntu Linux

When you’re running multiple websites on a single host, each website will have its document root. A document root is a directory where the website files for a domain are stored and served in response to requests. Below is an example of a directory structure for multiple websites with unique content and domains. As you can see above, each domain will have its folder with a document root included. Example: /var/www/domain/public_html Run the commands below to create a directory for example.com domain with its document root. Each document root will need an index.html file that will be shown to clients. Run the commands below to create an index.html file for the example.com domain. Then copy and paste the content below into the file and save it. Below is just a basic HTML file for testing purposes. Once you save the file, we’re ready to configure Nginx to reference this content. To avoid any permission issues, change the ownership of the domain document root directory and all files within the directory to the www-data user.

How to create an Nginx server block file on Ubuntu Linux

Now that you’ve created domain content in the directory above, go and configure the Nginx server block configuration file for the domain content above. On Ubuntu Linux, the Nginx server block configuration files are located in the /etc/nginx/sites-available directory. To create a server block file in the sites-available directory for our content above, run the commands below to create a site-specific Virtual Host configuration file An example configuration that should work with most environments is shown below. Copy and paste the content below into the file above and save. Once the file is saved, you can go and enable it to become a working server block. To enable the new server block configuration file, you use the symbolic link command which creates a symbolic link from the server block file to the sites-enabled directory. Run the commands below to enable the configuration file for our domain. After that, run the commands below to restart the Nginx service. Once Nginx is restarted, you now browse to the server hostname or IP address and it should display the content file we created above. Repeat this for other domains and server block configuration files you want to create. You can create as many virtual host files as you can as long as the server can run them all. That should do it! Conclusion: This post showed you how to create an Nginx server block on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.