Nginx - Location block


A location block is a block that is located inside a server block and maps a a resource path to a destination path such as:

A server block may have several location block.


server {
    location / {
        root /data/www;

    location /images/ {
        root /data;
    location ~* \.(gif|jpg|png)$ {
        expires 30d;

    location ~ \.php$ {
        fastcgi_pass  localhost:9000;
        fastcgi_param SCRIPT_FILENAME
        include       fastcgi_params;


Syntax 1)

location [prefix] expression {


  • prefix is optional may be:
    • = matches exactly the expression
    • ~ indicates a regular_expression match
    • ~* the asterisk after the tilde makes the regular match case insensitive.
  • expression is an expression that is matched only against the URL path part (and not the query part because it can can have many form for the same data. ie user=john&page=1 is equivalent to page=1&user=john)

An expression without prefix and the value /doc will match the directory /doc/ but also /docker

Regular expression

Regular expression in Nginx use the pcre syntax 2) with some twist such as you don’t have to escape the forward slash (/) in a URI as you do in a standard regex.

  • Name group capture is supported. Below is example of two groups with the name begin and end respectively.
location ~* (?<begin>.*myapp)/(?<end>.+\.php)$ {


After the server has been found in the request processing, the second step is to find the location block.

The Location block processing follows the following algorithm;

  • nginx first searches for the most specific prefix location given by literal strings regardless of the listed order.
  • then:
    • nginx checks locations given by regular expression in the order listed in the configuration file.
    • The first matching regular expression is chosen
    • If no regular expression matches a request, then nginx uses the most specific prefix location found earlier.


Discover More
Nginx - Block

block in Nginx is a grammar construct of the directive type. It groups: name and parameters and a set of additional instructions surrounded by braces ({ and }). See If a block directive can...
Nginx - FastCgi (php-fpm)

Using nginx with php is done trough a FastCgi service known as php-fpm that creates a bridge between nginx and php where Nginx forward the request to the php-fpm service. nginx:...
Nginx - HTTP Request Processing

How nginx process a HTTP request nginx first decides which server should process the request with: The request’s header field “Host”. If its value does not match any server name, or the request...
Nginx - Proxy

proxy in Nginx A proxy is defined in a location block where: upstream Define a group of server See upstream module If the URI is specified...
Nginx - Server Block Definition

A server block is a block that define a web server endpoints. It maps an URI to: a local directory or an other URI () and set some configurations. It is the equivalent of a Virtual host for apache....
URI - Path

The path component of an URI locates a resource inside a service identified by its authority. The path component of a hierarchical URI is itself said to be absolute if it begins with a slash character...

Share this page:
Follow us:
Task Runner