Nginx Request Processing

Beginner
6 min read
Updated: Nov 2026
3 examples
locations

Definition

For every request Nginx first picks a server block (IP/port, then Host / server_name). Then it picks a location inside that server.

Location rules: remember the longest matching prefix location, then test regex locations in order. The first regex match wins; otherwise the remembered prefix is used.

Config

Start with this shape. Adjust paths for your server.

Nginx
server {
    listen 80;
    server_name example.org;
    root /data/www;

    location / {
        index index.html index.php;
    }

    location ~* \.(gif|jpg|png)$ {
        expires 30d;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Locations look at the URI path only — not the query string. /a.php?x=1 and /a.php?y=2 use the same location.

Key Directives

NamePlain meaning
listenIP and port this server accepts.
server_nameHost names for name-based selection.
location /Prefix location — often the fallback.
location ~Regex location tested in listed order.

Examples

Example 1 — Pick the Server Block

Nginx matches listen first, then compares Host to server_name. No match means the default server for that port.

Nginx
server {
    listen 80 default_server;
    server_name example.net;
}

server {
    listen 80;
    server_name example.org www.example.org;
}

How It Works

  1. Host: example.org uses the second server.
  2. Unknown Host uses the default_server.
  3. default_server is per listen port, not global.

Example 2 — Pick the Location

A request for /logo.gif matches prefix / then regex for images — the regex wins.

Nginx
# Request: /logo.gif
# 1) prefix / matches
# 2) regex \.(gif|jpg|png)$ matches → use that location

# Request: /about.html
# 1) prefix / matches
# 2) no regex → use location /

How It Works

  1. Longest prefix is remembered first.
  2. Regex order in the file matters.
  3. No regex hit falls back to the prefix.

Example 3 — Index Can Trigger Another Lookup

A request for / may internally redirect to /index.php, then locations are searched again.

Nginx
location / {
    index index.html index.php;
}
# If only index.php exists, Nginx internally redirects to /index.php
# and the .php location handles it.

How It Works

  1. index checks files under root in order.
  2. Internal redirect restarts location search.
  3. That is why / can still reach FastCGI.

Frequently Asked Questions

Prefix locations are chosen by longest match, not file order. Regex locations use the order they appear.
The server used when Host does not match any server_name on that listen port.
No. Only the URI path is tested.
Nginx closes the connection without sending a normal HTTP response — useful for rejecting bad Hosts.
See also Server names and the reverse proxy guide on this site.

Next: Load balancing

Keep going through the beginner Nginx path.

Load balancing →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful