This article translated from www.keycdn.com/support/ngi…

background

Nginx Location is a skill that must be mastered when using Nginx, whether in Server Blocks or other locations Blocks. This article briefly describes how the location directive handles client URI requests when a request comes in.

Block configuration for Nginx

Nginx will configure the configuration in hierarchical blocks, and each time a request comes in, the Nginx server will process which block configuration the request maps to. In Nginx configuration files, the two main block configurations are:

  • serverBlock configuration
  • locationBlock configurationserverThe block configuration contains a series of virtual server configurations that can handle requests for multiple domain names, including IP ports. whilelocationThe configuration inserverThe block configuration plays a crucial role in determining thatURIOr how should resource requests be handled, where URI requests can be split into multiplelocationThe configuration of the

Syntax for the Nginx Location directive

The following is the syntax for a common location configuration, where modifier is optional and location_match is the key to determining which configuration the URI should go to.

location optional_modifier location_match {
 . . .
}
Copy the code

Regular Expressions (RE) or literals can be used to define the modifier. If the modifier is specified in the location configuration, it may change the way nginx matches the location.

  • (None) No modifier at all means that the location will be interpreted as a prefix match. To determine a match, the location will be matched from the beginning of the URI.
  • The = equals sign indicates that the current location will match a certain request, whatever is configured to match, and if a match is found, the search will stop.
  • The ~ tilde indicates that the current location is treated as a case-sensitive RE match.
  • The ~* tilde and asterisk location are matched by case-insensitive RE.
  • ^~ Non-expression (RE) matches, the regular expression will not take effect.

Matching order of Ngnix Location

For each request,nginx selects the location that best matches the request. Nginx compares these location rules to select a location. The order of comparison can be summarized as:

  1. Prefix matches are first matched (no RE expression), and each prefix match is matched once for the current request.
  2. search=Match, if the current request is matched, the search will stop and use this location directly.
  3. If the second step does not match,nginx continues to search for the longest prefix match by following the following steps: 3.1 If the longest prefix match exists^ ~With this modifier, Nginx will stop searching and use this location.3.2 directly ifThere is nouse^ ~, temporarily store the location and continue searching.
  4. As long as the longest prefix match is cached and selected,nginx looks to see if the current location has a case-sensitive RE(~and~ *), the first one that matches this will be treated as a valid location.
  5. If none of the RE’s locations match, the previously temporary location will be selected to handle the request.

Note: Location without modifier is a waste of resources. You can use ^~ instead.

For example,

Here are some examples of location configuration to detail the processing order described above, and you can modify the examples to suit your situation.

location  = / {
  Only requests/are processed.
}
Copy the code
location /data/ {
  # all matches with /data/, but the search continues.
  # if no other location matches, use this to process the request.
}
Copy the code
location^ ~ /img/ {
  # all requests beginning with /img/ will stop the search.
}
Copy the code
location ~* .(png|gif|ico|jpg|jpeg)$ {
  PNG, GIF, ICO, JPG, JPEG.
  # If the request goes to /img/, the location above 👆 will still be handled
}
Copy the code

How to prevent photo theft:

location ~ .(png|gif|jpe? g)$ {
  valid_referers none blocked yourwebsite.io *.yourwebsite.io;
  if ($invalid_referer) {
  return   403; }}Copy the code

Disable scripts in writable directories:

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

For more information about the nginx location directive, see nginx