Mastering Nginx: Essential Directives, Root, Location, and try_files Explained
This guide explains how to build a minimal, secure Nginx server, covering the required minimal configuration and detailed usage of the root, location, and try_files directives with practical code examples and matching rules.
Secure servers should run only the necessary services; building a minimal system helps debugging by adding features only when needed.
The minimal configuration required to run Nginx is:
# /etc/nginx/nginx.conf events {} # event context must be defined to consider config valid
http {
server {
listen 80;
server_name javatpoint.co www.javatpoint.co *.javatpoint.co;
return 200 "Hello";
}
}Root Directive
The root directive sets the document root for requests, allowing Nginx to map incoming URLs to the file system.
server {
listen 80;
server_name javatpoint.co;
root /var/www/javatpoint.co;
}With this configuration Nginx serves files such as:
javatpoint.co:80/index.html # returns /var/www/javatpoint.co/index.html
javatpoint.co:80/foo/index.html # returns /var/www/javatpoint.co/foo/index.htmlLocation Directive
The location directive configures handling based on the request URI.
Syntax:
location [modifier] pathExample:
location /foo {
# ...
}If no modifier is given, the path is treated as a prefix match. The following URIs would match the example above:
/foo
/fooo
/foo123
/foo/bar/index.html
...Multiple location blocks can be defined in the same server context:
server {
listen 80;
server_name javatpoint.co;
root /var/www/javatpoint.co;
location / {
return 200 "root";
}
location /foo {
return 200 "foo";
}
}Nginx also provides modifiers that affect matching priority:
= - Exact match
^~ - Preferential match
~ & ~* - Regex match (case-sensitive / case-insensitive)
(no modifier) - Prefix matchThe matching order is: exact matches first, then preferential, then regex, and finally the first prefix match.
location /match {
return 200 'Prefix match: will match everything that starting with /match';
}
location ~* /match[0-9] {
return 200 'Case insensitive regex match';
}
location ~ /MATCH[0-9] {
return 200 'Case sensitive regex match';
}
location ^~ /match0 {
return 200 'Preferential match';
}
location = /match {
return 200 'Exact match';
}try_files Directive
The try_files directive attempts to serve files in the order specified and returns the first one that exists.
try_files $uri index.html =404;For a request to /foo.html, Nginx will try /foo.html then index.html, returning a 404 if none are found.
Defining try_files in the server context creates a pseudo‑location that can interfere with other location blocks, so it should be placed inside a location block instead:
server {
location / {
try_files $uri /index.html =404;
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
