Deep Dive on NGINX Configuration Blocks

NGINX is a highly configurable web server and reverse proxy, and its power comes from its configuration blocks. These blocks define how requests are processed, connections are managed, and traffic is handled in various scenarios.

Here is the list of the configuration blocks :

  • Main (Global) Context

  • Events Block

  • HTTP Block

  • Server Block

  • Location Block

  • Upstream Block

  • Mail Block (for email proxying)

  • Stream Block (for TCP/UDP load balancing)


1. Main (Global) Context

The global or main context sets global directives for the entire NGINX process, including worker processes, error logging, and module loading. It is placed outside any specific block.

Key Directives in Main Context

Directive
Purpose

worker_processes

Defines the number of worker processes (usually set to auto in production).

error_log

Specifies the location and level of error logs.

pid

Defines the file where NGINX stores its process ID.

include

Allows importing additional configuration files.

Example:

Production Tip:

Set worker_processes auto; to utilize all available CPU cores.


2. Events Block

The events block configures how NGINX handles client connections.

Key Directives in Events Block

Directive
Purpose

worker_connections

Defines the max simultaneous connections per worker process.

multi_accept

Allows a worker to accept multiple new connections at once.

use

Specifies the event-driven model (e.g., epoll for Linux).

Final Example:

Production Tip:

For high-traffic applications, increase worker_connections based on server resources.


3. HTTP Block

The HTTP block is where web server configurations, caching, compression, and proxy settings are defined.

Key Directives in HTTP Block

Directive
Purpose

sendfile

Enables optimized file transfers.

tcp_nopush

Reduces TCP overhead.

keepalive_timeout

Defines how long persistent connections stay open.

resolver

Specifies DNS resolvers for dynamic upstream handling. especially important when using domain names in upstream blocks. Here it will work as global for all

log_format

Customizes the access log format.

include

Include MIME types and other HTTP-level configurations.

client_max_body_size

Defines the maximum allowed size of the client request body.

Example:

Production Tip:

Enable Gzip compression (gzip on;) to improve page load times.


4. Server Block

The server block defines individual virtual hosts that listen for incoming requests.

Key Directives in Server Block

Directive
Purpose

listen

Defines the port (e.g., 80 for HTTP, 443 for HTTPS).

server_name

Specifies domains for this server block.

root

Sets the root directory for static files.

index

Defines the default files to serve.

error_page

Customize the error responses.

resolver

Specifies DNS resolvers for dynamic upstream handling. especially important when using domain names in upstream blocks. Here it will work as server level not global.

Final Example:

Production Tip:

Use separate server {} blocks for HTTP and HTTPS.


5. Location Block

The location block defines how specific request paths are handled.

Key Directives in Location Block

Directive
Purpose

location

Defines URL patterns for request handling.

proxy_pass

Forwards requests to an upstream server.

proxy_set_header

To farward header to upstream server

rewrite

Modifies request URIs dynamically.

Example:

Production Tip:

Use try_files $uri $uri/ =404; for serving static content.


6. Upstream Block

The upstream block defines backend servers for load balancing.

Key Directives in Upstream Block

Directive
Purpose

server

Specifies backend servers.

weight

Assigns load balancing priority.

Final Example:

Production Tip:

Use ip_hash; for session persistence.


7. Mail Block

The mail block enables NGINX to act as an email proxy.

Key Directives in Mail Block

Directive
Purpose

server

Defines mail servers.

protocol

Specifies email protocols (IMAP, POP3, SMTP).

Final Example:

Production Tip:

Secure email connections using SSL.


8. Stream Block

The stream block allows NGINX to handle TCP/UDP traffic (e.g., databases).

Key Directives in Stream Block

Directive
Purpose

proxy_pass

Forwards TCP/UDP traffic.

upstream

Defines backend servers.

Final Example:

Production Tip:

For database connections, enable proxy_timeout to prevent idle disconnections.

Final Configuration Example


Conclusion

Each NGINX configuration block plays a crucial role in handling traffic efficiently. Understanding these blocks and their production best practices ensures a secure, scalable, and high-performance setup.

By applying these blocks correctly, you can optimize static content delivery, API routing, load balancing, email proxying, and database handling, making NGINX a powerful tool for any production environment.

Last updated