• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Nginx Tip – Set up a root directive for document root

December 19, 2023

Nginx Tip – Set up a root directive for document root

When it comes to web servers, Nginx is a popular choice due to its high performance, scalability, and flexibility. One of the essential configurations in Nginx is setting up the document root, which determines the location of the files that will be served by the server. In this article, we will explore how to set up a root directive for the document root in Nginx.

Understanding the Document Root

The document root is the directory on the server where the web files are stored. When a user requests a webpage, the web server looks for the requested file within the document root and serves it to the user’s browser. By default, Nginx sets the document root to /usr/share/nginx/html, but you can customize it according to your needs.

Setting up the Root Directive

To set up the root directive in Nginx, you need to modify the server block configuration file. The server block contains the directives that define how Nginx should handle incoming requests. Here’s how you can do it:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    ...
}

In the above example, we have set the root directive to /var/www/html. This means that Nginx will look for the requested files within the /var/www/html directory.

Using Variables in the Root Directive

Nginx allows you to use variables in the root directive, which can be useful in certain scenarios. For example, you might want to serve different content based on the user’s location or the requested URL. Here’s an example:

server {
    listen 80;
    server_name example.com;
    root /var/www/html/$country;
    ...
}

In the above example, we have used the $country variable in the root directive. This variable can be set based on the user’s location, allowing you to serve different content for different countries.

Testing the Configuration

After making changes to the Nginx configuration file, it is essential to test the configuration for any syntax errors. You can do this by running the following command:

sudo nginx -t

If the configuration is valid, you will see a message indicating that the configuration file syntax is ok. Otherwise, it will display an error message along with the line number where the error occurred.

Conclusion

Setting up the root directive for the document root in Nginx is a crucial step in configuring your web server. By customizing the document root, you can control where Nginx looks for the requested files. Whether you want to serve static files or dynamic content, understanding and correctly configuring the root directive is essential for a smooth and efficient web server setup.

Summary

In summary, setting up the root directive for the document root in Nginx is a fundamental configuration step. By customizing the document root, you can control where Nginx looks for the requested files. To set up the root directive, modify the server block configuration file and specify the desired directory. Nginx also allows you to use variables in the root directive, enabling you to serve different content based on various factors. To ensure the configuration is error-free, test it using the sudo nginx -t command. For more information on Nginx and VPS hosting, visit Server.HK.

Recent Posts

  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)
  • CentOS End of Life (EOL): What It Means and Migration Options in 2026

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot