ScaleScaleScaleScale

Tips / Nginx


Nginx Location Directive

Nginx location directive is widely used in Nginx configurations, so in order to avoid website issues, first you need to understand how nginx location directive works.

Syntax of Nginx location directive

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location { } @ name { ... }
Context: server / location

First, you must know that different configurations can be applied depending on the URI you are processing, you can use literal strings and also regular expressions as you need.

Nginx Location with Regular expressions

In order to use regular expressions, you must always use a prefix:

"~"  must be used for case sensitive matching
"~*" must be used for case insensitive matching

If you need to not match a regular expression, then just target the regular expression and assign an empty block, then the rest of the queries can be matched using a location / .

Note: Nginx has the ability to decode URIs in real time. For example, in order to match /app/%20/images you may use /app/ /images to determine the location.

Nginx Location with literal strings

The = prefix forces a literal match between the request URI and the location parameter. When this is matched, the search stops right away. If you find your app is accessing the request / frequently, it should be a better idea to use a literal string instead, for example: location = /, this will speed up the processing of this request.

Nginx Location directive examples

location  = / {
  # this matches only the / query.

  # configuration example #1
}
location  / {
  # matches any query due to the fact that all queries begin at /, however, regular expressions will 
  # be matched at first place.

  # configuration example #2  
}
location /data/ {
  # this config matches any query beginning with /data/ and then continues searching,
  # in this example regular expressions will be checked and /data/ will be matched only if
  # regular expressions don't find a match.

  # configuration example #3
}
location ^~ /img/ {
  # matches any query beginning with /img/ and then stops searching,
  # in this example there is not place for regular expressions.

  # configuration example #4 
}
location ~* .(png|ico|gif|jpg|jpeg)$ {
  # this config matches any request ending in png, ico, gif, jpg or jpeg. However, all
  # requests to the /img/ directory will be handled by the previous location block we defined
  # at the previous example config #4

  # your configuration here #5  
}

Real Nginx location examples:

Location directive used for Nginx Anti-hotlinking:

location ~ .(gif|png|jpe?g)$ {
    valid_referers none blocked mywebsite.com *.mywebsite.com;
    if ($invalid_referer) {
    return   403;
    }
}

Another example, this time to deny scripts inside writable directories

location ~* /(images|cache|media|logs|tmp)/.*.(php|pl|py|jsp|asp|sh|cgi)$ {
    return 403;
    error_page 403 /403_error.html;
}

Location directive used to enable Nginx autoindex:

location /testing {
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
}

If you need to read more about Nginx Location Directive, remember to also check the official documentation

Popular search terms:

  • nginx location
  • nginx location regex
  • https://www scalescale com/tips/nginx/nginx-location-directive/
  • nginx location directive
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • eknoop

    Not sure if this is what you intended, but .(png|ico|gif|jpg|jpeg)$ will probably match things like “whatever.com/asdfasdfasdfjpg”. For extensions you need to use .(png|ico|gif|jpg|jpeg)$

  • admin

    @eknoop

    Thanks for letting me know that.

    In fact, at code level it was well stated (same as you suggested) but for some strange reason Worpdress didn’t show the initial slash. Now it’s been corrected.

    Cheers!

  • Great post! And love the site design 🙂

  • Iman

    very brief and very helpful