Beruflich Dokumente
Kultur Dokumente
Test config
sudo nginx -t
location
Syntax:
location [ = | ^~ | ~ | ~* ] uri { ... }
location @name { … }
Context: server, location
Check order:
(1) location = string-uri { … } exact, identical match (stop further searching)
(2) location ^~ string-uri { … } match beginning with (stop further searching)
(3) location ~ regex-uri { case sensitive } ┬ executed in order of appearance
(3) location ~* regex-uri { case insensitive } ┘
(4) location string-uri { … }
1. location /w/ { … }
2. location /w/images/details/ { … }
3. location /w/images/a/ { … }
Examples of requests: “/” “/documents/document.html” “/documents/1.jpg”
│ “/index.html” │ “/images/1.gif” │
location = / { ←─┘ │ │ │ │ ← rank check order (1):
│ │ │ │ matches the query / only
[ configuration A ] │ │ │ │
} │ │ │ │
location / { │ │ │ │ ← rank check order (4):
│ │ │ │ matches any query but regular
[ configuration B ] ←────────┘ │ │ │ expressions and any longer
} │ │ │ normal blocks will be matched first
location /documents/ { │ │ │ ← rank check order (4)
[ configuration C ] ←──────────────────┘ │ │
} │ │
location ^~ /images/ { │ │ ← rank check order (2): matches any
│ │ query beginning with /images/
[ configuration D ] ←────────────────────────────┘ │ and halts searching, so regular
│ expressions will not be checked.
} │
location ~* \.(gif|jpg|jpeg)$ { │ ← rank check order (3): matches any
│ case insensitive request ending in
│ gif, jpg, or jpeg except for those
[ configuration E ] ←───────────────────────────────────────────┘ beginning with /images/ (=config D)
} (not C: regex is executed before)
rewrite, try_files
Syntax:
rewrite regex replacement [flag];
Context: server, location, if
Check order rules:
* executed in order of appearance
* [flag] can terminate further processing of the directives
├→ last → stops processing the current set of ngx_http_rewrite_module directives
│ and starts a search for a new location matching the changed URI;
├→ break → stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
├→ redirect → returns a temporary redirect (302 code); used if a replacement string does not start with
│ “http://” or “https://”.
└→ permanent → returns a permanent redirect (301 code)
* “http://” or “https://” begins the rewrite: the processing stops and the redirect is returned to a client
Examples (see also http://wiki.nginx.org/Pitfalls)
1. location / {
6. }
7. location ^~ /web/ {
8. try_files $uri $uri/ @do_wikipage; # if it fails try named location block @do_wikipage
9. }
11. rewrite "^/web/?(.+)$" /w/index.php?title=$1&args redirect; #(wiki reports 404 for non-existing pages! But can be created)
12.}
Virtual hosts
To test if the configuration is OK, run
sudo nginx -t
sudo nginx -t -c /etc/nginx/nginx.conf # test a specific configuration file