Using location context contained within server context.
Location context holds configuration of server's response to requests for resources within the server.
Location context defines what ngin server should check URL against.
A modifier effects the way nginx attempts to match the location block.
Modifiers table
Order | Modifier | Explanation |
---|---|---|
1 | = | exact match |
2 | ^~ | preferrential prefix match |
3 | ~ and ~* | regex match (~ is case sensitive) |
4 | no modifier |
exact match
location = /favicon.ico{expires 1y;}
request to: http:/www/site.com/favico.ico
expires directive states that this file will be cached for 1 year
preferrential prefix match
location ^~ /.well-known {try_files $uri = 404;}
request to: http://www.site.com/.well-known
case sensitive regular expression match
location ~ /.{deny all;}
request to: http://www.site.com/.htaccess
deny all directive will apply
case insensitive regular expression match
location ~* /uploads/.*/php$ {deny all;}
request to: http://www.site.com/uploads/2017-07/hacked.php
deny all will block all access when someone has uploaded a php script to uploads folder and now tries to access it.
no modifier match
location / {try_files $uri $uri/ index.php?args;}
When the request is made to the server the nginx will check for the file first at the location matching URL. If it does not find the file the server will searhc for a directory that matches the request. If neither file or directory are found then an internal re-direct is created to index.php passing the query string arguments as parameters.