Mastering Nginx: Minimal Config, root, location, and try_files Explained

This guide walks through building a minimal Nginx server, explains the root, location, and try_files directives with syntax, modifiers, matching priority, and practical code examples, and highlights common pitfalls such as using try_files in the wrong context.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Nginx: Minimal Config, root, location, and try_files Explained

Minimal Nginx configuration

A minimal nginx.conf that is sufficient to start the server and to isolate configuration errors:

# /etc/nginx/nginx.conf (minimal example)
http {
    server {
        listen 80;
        server_name javatpoint.co www.javatpoint.co *.javatpoint.co;
        return 200 "Hello";
    }
}

root directive

The root directive defines the filesystem path that Nginx uses as the document root for a server or location block. The request URI is appended to this path to locate the file to serve.

server {
    listen 80;
    server_name javatpoint.co;
    root /var/www/javatpoint.co;
}

Resulting request mapping:

http://javatpoint.co:80/index.html   → /var/www/javatpoint.co/index.html
http://javatpoint.co:80/foo/bar.html → /var/www/javatpoint.co/foo/bar.html

location directive

The location block selects configuration based on the request URI. Its syntax is:

location [modifier] path {
    # configuration statements
}

If no modifier is supplied, the path is treated as a prefix match.

location /foo {
    # matches /foo, /foo/, /foo/bar, /foo123, etc.
}

Multiple location blocks can coexist in the same server context. Nginx chooses the most specific block according to the following priority (from highest to lowest): = – exact match ^~ – preferential (prefix) match ~ – case‑sensitive regular expression ~* – case‑insensitive regular expression

no modifier – ordinary prefix match

Example demonstrating the order of evaluation:

location /match { return 200 'Prefix match'; }
location ~* /match[0-9] { return 200 'Case‑insensitive regex'; }
location ~ /MATCH[0-9] { return 200 'Case‑sensitive regex'; }
location ^~ /match0 { return 200 'Preferential match'; }
location = /match { return 200 'Exact match'; }

# Request → response
/match      → 'Exact match'
/match0    → 'Preferential match'
/match1    → 'Case‑insensitive regex'
/MATCH1    → 'Case‑sensitive regex'
/match-abc → 'Prefix match'

try_files directive

The try_files directive attempts to serve a list of files in the order given. If none of the listed files exist, the last parameter is used as a fallback (commonly a URI or an error code).

try_files $uri index.html =404;

For a request /foo.html, Nginx evaluates the list as follows:

$uri        → /foo.html (if the file exists)
index.html  → /index.html (fallback file)
=404        → return HTTP 404 if neither file is found

Important placement rule: try_files should be defined inside a location block, not directly under server. When placed in the server context it creates a pseudo‑location that can unintentionally override more specific location blocks.

Correct usage:

server {
    listen 80;
    server_name javatpoint.co;

    location / {
        try_files $uri /index.html =404;
    }
}

Incorrect usage (to be avoided):

server {
    try_files $uri /index.html =404;  # discouraged – creates a pseudo‑location
    location / { }
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendConfigurationNGINXtry_fileslocation directiveroot directive
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.