Support Online
Skip to main content

Nginx Server and Location Selection Algorithms

What Will You Learn in This Guide?

This guide explains how Nginx determines which server and location block will process incoming HTTP requests.
By understanding the selection algorithm, you can prevent routing errors and create more performant configurations.

Technical Summary

This content details the selection order of server and location blocks in Nginx configuration.
The goal is to avoid conflicting rules, eliminate misdirection, and write predictable configurations.
The evaluation process consists of IP/Port matching, domain priority and URI-based location selection.


1. Server Block Selection Algorithm

Nginx determines which virtual server will respond when a request arrives in two stages.

1.1 listen Directive Priority

Nginx first compares the IP address and port information of the request.
The more specific definition always takes precedence.

Example priority order:

  • 192.168.1.10:80
  • 0.0.0.0:80

The specific IP definition is evaluated before the definition that listens on all IPs.


1.2 server_name Evaluation Order

If the IP and port match, this time the Host header is evaluated.
Nginx strictly enforces the following order:

  1. Exact match (ornek.com)
  2. With joker on the front (*.ornek.com)
  3. Ending with joker (www.genixnode.*)
  4. Regex (~^www\.)
  5. Default server (default_server)

If there is no match, the request is directed to the default server block.


2. Location Block Selection Algorithm

After the server block is determined, the location block that will process the URI is selected.
This process follows a multi-layered priority order.

2.1 Exact Match (=)

location = /login { ... }
  • If the URI matches exactly, the search stops. It is the fastest and highest priority match type.

  1. Nginx checks all prefixes and keeps the longest matching one.

location / { ... }
location /static/ { ... }
  • /static/ wins when requesting /static/logo.png.

2.3 Prior Prefix (^~)


location ^~ /static/ { ... }
  • If this prefix matches, the regex lookup is skipped entirely. It is a high-performance solution for static files.

2.4 Regex Search (~ and ~*)


location ~* \.jpg$ { ... }
  1. Regex blocks are evaluated in the order they are written in the file. The first one to match wins.

2.5 Default Prefix

  • If the regex does not match, the longest previously stored prefix is used.

3. Code Blocks and Technical Behaviors


listen 10.0.0.5:80;
  • This definition only accepts requests over the specified IP.

server_name ornek.com;
  • This setting provides exact domain matching.


location = / { ... }
  • Processes fixed requests such as the home page in the fastest way.


try_files $uri $uri/ @app;
  • If the file is not found, forwards the request to the application layer.

Frequently Asked Questions? (FAQ)

  1. Why isn't my Regex block working? It could be a previously matched ^~ prefix or another regex defined above.

  2. What is the difference between root and alias? root appends the URI; alias replaces the matching part.

  3. What happens if I do not define default_server? The first server listening to the relevant port automatically becomes the default.

  4. Is the / at the end of the URI important? Yes. /static and /static/ may result in different matches.


Result

Always go from simple to complex in Nginx configurations. Keeping Regex usage to a minimum improves performance. After changes, be sure to check the following:


nginx -t

A properly configured Nginx infrastructure is the foundation of stable and high-performance systems. You can safely apply these principles on the GenixNode platform.