Link Search Menu Expand Document
Table of contents

Location syntax

  1. Exact Match: = uri
  2. Preferential prefix match: ^~ uri
  3. Regex match: ~* uri
  4. Prefix match: uri

Inheritance & directive types

  • Standard directive
  • Array directive
  • Action directive
server {
    root /site/demo;

    location {
        root /site/demo; #inherited root 
    }
}

Prefix match

without location =

server {
        listen 80;
        server_name 198.255.25.1;
        root /sites/demo; 

        location /any-path {
                return 200 "hello from nginx";
                # return 307 'https://anything.example.com/anything';
                # determine how NGINX wants to respond when request coming to specific path.
        }  
}  

Exact match

defined with location =

server {
        listen 80;
        server_name 198.255.25.1;
        root /sites/demo; 

        location = /any-path {
            return 200 "hello from nginx" 
        } 
}

Regex match - case sensitive

server {
        listen 80;
        server_name 198.255.25.1;
        root /sites/demo; 

        location ~ /any-path[0-9] {
            # ~ for 'any-path' + a number between 0 to 9
            # ~# for case insensitive
            # ^~ preferential prefix match 
            return 200 "hello from nginx" 
        } 
}