Difference between revisions of "Webservers/nginx"
Jump to navigation
Jump to search
(→Nginx) |
|||
| Line 4: | Line 4: | ||
* Go-To's - <code>rewrite</code> | * Go-To's - <code>rewrite</code> | ||
* Case statements - <code>map</code> and <code>geo</code> | * Case statements - <code>map</code> and <code>geo</code> | ||
* inheritance of context configurations | * inheritance of context configurations - inherit if there is nothing else, otherwise override | ||
== [https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms server context] match order == | == [https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms server context] match order == | ||
Revision as of 18:01, 4 August 2020
Nginx
Features
- conditionals -
if - Go-To's -
rewrite - Case statements -
mapandgeo - inheritance of context configurations - inherit if there is nothing else, otherwise override
server context match order
For the server {} context selection nginx algorithm takes only 2 directives into account listen and server_name.
server {
listen 80; server_name *.example.com; . . .
}
- Matching
listendirective
At first listen directive is parsed, during this step any "incomplete" directives are added with default values
- no-ip -> set with
0.0.0.0 - no-port -> set with port
:80
The listen directive can be set to:
- An IP address/port combo.
- A lone IP address which will then listen on the default port 80.
- A lone port which will listen to every interface on that port.
- The path to a Unix socket
The most accurate listen directive match gets chosen.
- Matching
server_namedirective
If there are multiple the same listen directives, then server_name is being parsed in this order:
- exact match host header from the request
- leading wildcard (indicated by a
*at the beginning of the name in the config). If multiple matches are found, the longest match will be used to serve the request. - trailing wildcard (indicated by a server name ending with a
*in the config). If multiple matches are found, the longest match will be used to serve the request. - using regular expressions (indicated by a
~before the name). The first server_name with a regular expression that matches the “Host” header will be used to serve the request. - selects the
default_serverblock for that IP address and port part of listen directive. There can be only onedefault_serverdeclaration per each IP address/port combination. - First block, for an IP address/port combo, this will either be the first block in the configuration or the block that contains the default_server option as part of the listen directive (which would override the first-found algorithm).
server {
listen 80 default_server;
server_name example.com;
. . .
}
server {
listen 80;
server_name ~^(subdomain|set|www|host1).*\.example\.com$;
. . .
}
server {
listen 80;
server_name www.example.*;
. . .
}
server {
listen 80;
server_name *.org;
. . .
}
References
- Sites-enabled vs conf.d Explanation of standards and Debian-ism of sites-enabled.