Nginx - HTTP Request Processing

About

How nginx process a HTTP request

Steps

Server

nginx first decides which server should process the request with:

One Host on one IP

The request’s header field “Host”. 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.

If 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;
}

where: the code 444 is a special status nginx code that closes the connection.

One host multiple listen IP

Steps:

  • 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.

Example:

server {
    listen      192.168.1.1:80;
    server_name example.org www.example.org;
    ...
}

server {
    listen      192.168.1.2:80;
    server_name example.com www.example.com;
    ...
}

Location

After the server has been found, the second step is to find the location block.

The Location block processing follows the following algorithm;

  • nginx first searches for the most specific prefix location given by literal strings regardless of the listed order.
  • then:
    • nginx checks locations given by regular expression in the order listed in the configuration file.
    • The first matching regular expression is chosen
    • If no regular expression matches a request, then nginx uses the most specific prefix location found earlier.

Ref

Documentation / Reference





Discover More
Nginx

nginx is a web server nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker...
Nginx - Location block

A location block is a block that is located inside a server block and maps a a resource path to a destination path such as: a local directory or an other service (see ) A server block may have several...



Share this page:
Follow us:
Task Runner