In this tutorial, we will discuss about Nginx server block and how the nginx decides which server block will handle the request.
Nginx Server Block Structure
This is what the Nginx server block looks like:
server {
listen 80;
server_name example.com;
. . .
location / {
. . .
}
}
There many directives you can put inside a server block, but there are 2 main directives that we will discuss here. These 2 directives are:
listen
directive, it will be the address and port where the Nginx will listen to.server_name
directive, it will be like your host domain.
These directives decides which server block will Handle the incoming request.
How Nginx Decide Which Server Block is Chosen
This is step by step how the Nginx decide which server block will be choosen:
- First Nginx will see to which
ip
andport
the request comes. Nginx will match theip
andport
withlisten
directive. - If there are many server block with the same
listen
directives, Nginx then see thehost
header from the request and then match them withserver_name
directive. - If there are many server block with the same
server_name
directive that match thehost
header, Nginx will choose the first server block with that name. - If there are no
server_name
directive match thehost
header, the nginx will choose the server block who hasdefault_server
parameter.
Nginx Server Block Usage Example
To have better understanding of how the Nginx evaluate and choose the server block, we will provide some example for you.
First Example
Let’s assume that our server has *.example.com
domain, resolvable by DNS, pointing to our server, and this is the configuration of Nginx in our server.
server {
listen 127.0.0.1:80;
. . .
}
server {
listen 80;
. . .
}
If we have request from a client to http://example.com
, the second server block will be chosen. If we write the listen
directive without the adress, then the Nginx will not bind to specific address and can serve from any interfaces. The first server block cannot serve the request because it bind to localhost and can only serve a request from localhost.
Second Example
Let’s assume that our server has *.example.com
domain, resolvable by DNS, pointing to our server, and this is the configuration of Nginx in our server.
server {
listen 80;
server_name first.example.com;
. . .
}
server {
listen 80;
server_name second.example.com;
. . .
}
If we have request from a client to http://second.example.com
, second server block will be chosen. Both server block has the same listen
directive, so Nginx will match the server_name
directive with the host
header on the client request.
Third Example
Let’s assume that our server has *.example.com
domain, resolvable by DNS, pointing to our server, and this is the configuration of Nginx in our server.
server {
listen 80;
server_name first.example.com;
. . .
}
server {
listen 80;
server_name second.example.com;
. . .
}
server {
listen 80 default_server;
. . .
}
If we have request from a client to http://example.com
, third server block will be chosen. The Nginx will try to match the server_name
directive with the host
header on the client request first, but as you can see no server_name
matching with the host
header. Nginx will then search the default_server
parameter and choose the server block.
For more advance configuration with the listen
and server_name
directive, you can always see the Nginx Documentation here: Module ngx_http_core_module.
Thats it! Hopefully now you understand how Nginx decide which server block will handle the request fro these explanation. Next, I will recommend you to find out how the Nginx choose the location block inside the server block. Please read my other tutorial here: Understanding Nginx Location Block.