Sie sind auf Seite 1von 3

Cheat sheet

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 { … }

The order in which location directives are checked is as follows:


(1) Directives with the "=" prefix that match the query exactly (literal string). If found, searching stops.
(2) All "^~" prefixed locations with conventional strings. If it matches, searching stops.
(3) Regular expressions, in the order they are defined in the configuration file.
(4) All remaining directives with conventional strings, “most specific“ strings are executed:

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 / {

2. # Redirect domain-only access (= no path given, w/o or with /) to default wiki:

3. # 302 → temporary redirect

4. # 301 → permanent redirect

5. return 301 "^[/]?$" /web/;

6. }

7. location ^~ /web/ {

8. try_files $uri $uri/ @do_wikipage; # if it fails try named location block @do_wikipage

9. }

10.location @do_wikipage { # try as wiki page:

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

Changes will take effect after you restart nginx.

Das könnte Ihnen auch gefallen