====== Nginx ====== Nginx (gesprochen engineX) ist ein Webserver. Hier sind div. Konfigurationen zum Webserver abgelegt. ===== Location ===== Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } ^ Zeichen ^ Erklärung ^ | ''~'' | beachtet Groß- und Kleinschreibung | | ''~*'' | muss verwendet werden, wenn Groß- und Kleinschreibung nicht beachtet werden soll | | ''^~'' | verarbeitet alle Anfragen welche mit dem, nach den Zeichen angegebenen, Suchbegriff beginnen | | ''='' | das Zeichen weist an, nur an diesem Ort zu suchen (alle anderen Zeichen such am angegebenen Ort und rekursiv) | **Erklärung** (Quelle [[http://wiki.nginx.org/HttpCoreModule#location]]): This directive allows different configurations depending on the URI. It can be configured using both literal strings and regular expressions. To use regular expressions, you must use a prefix: - "~" for case sensitive matching - "~*" for case insensitive matching - there is no syntax for NOT matching a regular expression. Instead, match the target regular expression and assign an empty block, then use location / to match anything else. The order in which location directives are checked is as follows: - Directives with the "=" prefix that match the query exactly (literal string). If found, searching stops. - All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops. - Regular expressions, in the order they are defined in the configuration file. - If #3 yielded a match, that result is used. Otherwise, the match from #2 is used. Details below. To determine which location directive matches a particular query, the literal strings are checked first. Literal strings match the beginning portion of the query - the most specific match will be used. Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the literal string search is used. For case-insensitive operating systems, like Mac OS X or Windows with Cygwin, literal string matching is done in a case insensitive way (0.7.7). However, comparison is limited to single-byte locale's only. Regular expression may contain captures (0.7.40), which can then be used in other directives. **It is possible to disable regular expression checks after literal string matching by using "^~" prefix.** If the most specific match literal location has this prefix: regular expressions aren't checked. The "=" prefix forces an exact (literal) match between the request URI and the location parameter. When matched, the search stops immediately. A useful application is that if the request "/" occurs frequently, it's better to use "location = /", as that will speed up the processing of this request a bit, since the search will stop after the first comparison. It is important to know that nginx does the comparison against decoded URIs. For example, if you wish to match "/images/%20/test", then you must use "/images/ /test" to determine the location. The location directive only tries to match from the first / after the hostname, to just before the first ? or #. (Within that range, it matches the unescaped url.) Weitere Beispiele aus : location = / { # matches the query / only. [ configuration A ] } location / { # matches any query, since all queries begin with /, but regular # expressions and any longer conventional blocks will be # matched first. [ configuration B ] } location /documents/ { # matches any query beginning with /documents/ and continues searching, # so regular expressions will be checked. This will be matched only if # regular expressions don't find a match. [ configuration C ] } location ^~ /images/ { # matches any query beginning with /images/ and halts searching, # so regular expressions will not be checked. [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { # matches any request ending in gif, jpg, or jpeg. However, all # requests to the /images/ directory will be handled by # Configuration D. [ configuration E ] } ==== try_files / index ==== Möglichst die beiden Anweisungen so benutzen: **Location ohne php**: index index.php index.html index.htm; try_files $uri $uri/ =404; **Location mit php**: try_files $uri =404; ==== Rewrite ==== if ($http_host = example.org) { rewrite (.*) http://www.example.org$1; server { listen 80; server_name example.org; return 301 http://www.example.org$request_uri; } ==== root oder alias ==== ^ Anweisung ^ Beschreibung ^ | ''root'' | Hier wird der volle Pfad **inklusive** der ''location''-Anweisung zum Aufruf verwendet. | | ''alias'' | Hier wird der volle Pfad **ohne** der ''location''-Anweisung zum Aufruf verwendet. | Beispiele: Hier ein Beispiel für eine ''root''-Anweisung: location /static/ { root /var/www/app/static/; autoindex off; } In diesem Fall wird der suchpfad so aussehen: **"/var/www/app/static/static"** Wenn sich in dem Verzeichnis "/var/www/app/static" wiederum das Verzeichnis namens "static" befindet, wird der Webserver diesen Pfad bedienen können. Sonst gibt er die Antwort "404" zurück. Die richtige Konfiguraton wäre hier: location /static/ { root /var/www/app/; autoindex off; } Hier das Beispiel für die ''alias''-Anweisung: location /static/ { alias /var/www/app/static/; autoindex off; } Hier versucht der Webserver folgenden Pfad zu bedienen: **"/var/www/app/static"** ==== Regex ==== Hier ein Beispiel wie man Regex in ''location'' Funktionen nutzen kann. location ~* /(images|cache|media|logs|tmp)$ oder location ~* /(images|cache|media|logs|tmp)/.*.(php|pl|py|jsp|asp|sh|cgi)$ ===== Nützliche Befehle ===== * Konfiguration vor dem "Reload" testen \\ nginx -t -c