Understanding NGINX lookup: Server and Location Blocks

NGINX is one of the world’s most popular web server, which can act as a load balancer and a reverse proxy, handling high number of concurrent connections with high performance.

NGINX lookup starts from the http block, then through one or more server blocks, followed by the location block(s).

The http block contains directives for handling the web traffic, which are often referred to as universal, since, they are passed to all the website configurations that NGINX serves. The configuration file for handling different types of web traffic requests is passed using include directive, followed by configuration file path.

Figure 1 : http block example

NOTE : You’d generally want single http block, and multiple server blocks.

The server block contains server configuration directives, containing information such as server name, and TCP port. The listen directive tells NGINX hostname/IP and the TCP port where it should listen for HTTP connections. The server_name directive directs NGINX to select the server (out of multiple server blocks), when a request hits the server.

Figure 2 : server block example

NOTE : default_server argument makes the server block answer all the requests that doesn’t match any server block. The default arguments to listen directives are IP 0.0.0.0 and Port 80.

server_name enables you to serve multiple subdomains, inside one http block. Also, feel free to make use of regular expressions while defining server_name.

The combination of server_name and listen directives, enables NGINX to choose the server block, however, if multiple server blocks are matching, preference is given to exact match, followed by longest matching prefix asterisk, then longest matching suffix asterisk. Following server block matching, NGINX goes on to IP Look Up for the most suitable location block.

The location block enables you to handle several types of URIs/routes, within a server block. Typically, you’d make use of one or more regular expressions to define and handle a category of routes. The lookup then proceeds to comparing and matching the location blocks, and serves the one which is closest to the request URI hit.

How to Allow DNS Lookup Through a Firewall

Jackson, Charles. "How to Allow DNS Lookup Through a Firewall." Small Business - Chron.com,

Jackson, Charles. (n.d.). How to Allow DNS Lookup Through a Firewall. Small Business - Chron.com. Retrieved from http://smallbusiness.chron.com/allow-dns-lookup-through-firewall-42886.html

Jackson, Charles. "How to Allow DNS Lookup Through a Firewall" accessed July 06, 2019.

Note: Depending on which text editor you're pasting into, you might have to add the italics to the site name.

Comments