How nginx processes a request | english русский 简体中文 עברית 日本語 türkçe news about download security advisories documentation pgp keys faq links books support donation trac wiki nginx.com | |
Name-based virtual serversnginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port *:80: server { listen 80; server_name example.org www.example.org; ... } server { listen 80; server_name example.net www.example.net; ... } server { listen 80; server_name example.com www.example.com; ... }
In this configuration nginx tests only the request’s header field
“Host” to determine which server the request should be routed to.
If its value does not match any server name,
or the request does not contain this header field at all,
then nginx will route the request to the default server for this port.
In the configuration above, the default server is the first
one — which is nginx’s standard default behaviour.
It can also be set explicitly which server should be default,
with the server { listen 80 default_server; server_name example.net www.example.net; ... }
The Note that the default server is a property of the listen port and not of the server name. More about this later. How to prevent processing requests with undefined server namesIf requests without the “Host” header field should not be allowed, a server that just drops the requests can be defined: server { listen 80; server_name ""; return 444; } Here, the server name is set to an empty string that will match requests without the “Host” header field, and a special nginx’s non-standard code 444 is returned that closes the connection.
Since version 0.8.48, this is the default setting for the
server name, so the
Mixed name-based and IP-based virtual serversLet’s look at a more complex configuration where some virtual servers listen on different addresses: server { listen 192.168.1.1:80; server_name example.org www.example.org; ... } server { listen 192.168.1.1:80; server_name example.net www.example.net; ... } server { listen 192.168.1.2:80; server_name example.com www.example.com; ... }
In this configuration, nginx first tests the IP address and port
of the request against the
listen directives
of the
server blocks.
It then tests the “Host”
header field of the request against the
server_name
entries of the
server
blocks that matched
the IP address and port.
If the server name is not found, the request will be processed by
the default server.
For example, a request for As already stated, a default server is a property of the listen port, and different default servers may be defined for different ports: server { listen 192.168.1.1:80; server_name example.org www.example.org; ... } server { listen 192.168.1.1:80 default_server; server_name example.net www.example.net; ... } server { listen 192.168.1.2:80 default_server; server_name example.com www.example.com; ... }
A simple PHP site configurationNow let’s look at how nginx chooses a location to process a request for a typical, simple PHP site: server { listen 80; server_name example.org www.example.org; root /data/www; location / { index index.html index.php; } location ~* \.(gif|jpg|png)$ { expires 30d; } location ~ \.php$ { fastcgi_pass localhost:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
nginx first searches for the most specific prefix location given by
literal strings regardless of the listed order.
In the configuration above
the only prefix location is “ Note that locations of all types test only a URI part of request line without arguments. This is done because arguments in the query string may be given in several ways, for example: /index.php?user=john&page=1 /index.php?page=1&user=john Besides, anyone may request anything in the query string: /index.php?page=1&something+else&user=john
Now let’s look at how requests would be processed in the configuration above:
|