Home Nginx Request processing Nginx Request Processing 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.
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 Name Plain 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 1. Pick the Server Block 2. Pick the Location 3. Index Can Trigger Another Lookup 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.
server {
listen 80 default_server;
server_name example.net;
}
server {
listen 80;
server_name example.org www.example.org;
} How It Works Host: example.org uses the second server.Unknown Host uses the default_server.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.
# 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 Longest prefix is remembered first.Regex order in the file matters.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.
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 index checks files under root in order.Internal redirect restarts location search.That is why / can still reach FastCGI.Frequently Asked Questions Does location order always matter? Prefix locations are chosen by longest match, not file order. Regex locations use the order they appear.
What is default_server? The server used when Host does not match any server_name on that listen port.
Are query strings part of location matching? No. Only the URI path is tested.
What does return 444 do? Nginx closes the connection without sending a normal HTTP response — useful for rejecting bad Hosts.
Where can I read more? See also Server names and the reverse proxy guide on this site.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion