top of page
Writer's pictureSiddhesh Kadam

Understanding the Nginx Directive Structure


x

The core of Nginx's configuration revolves around directives that are organized into hierarchical blocks. The most fundamental blocks are the http, server, and location blocks.


1. The 'http' Block

The http block is the top-level context for HTTP configuration. It contains all the configuration necessary to handle HTTP traffic. Within this block, you can define global settings that apply to all server blocks nested within it.

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
}

2. The 'server' Block

Inside the http block, you define one or more server blocks. Each server block represents a virtual server that handles requests for a specific domain or IP address. The server block is where you configure settings specific to a particular domain, such as server names, SSL certificates, and listening ports.


http {
    server {
        listen       80;
        server_name  builddevops.com www.builddevops.com;
        # Location blocks go here
        location / {
            ...
        }
    }
}

3. The 'location' Block

Within a server block, you use location blocks to define how requests for different URIs should be handled. The location block allows you to specify directives that apply to particular parts of your website. This is where you configure things like proxying, static file serving, and URL rewrites.


http {
    server {
        listen       80;
        server_name  builddevops.com www.builddevops.com;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        location /admin/ {
            root   /usr/share/nginx/admin;
        }
    }
}

Directive Inheritance

One of the powerful features of Nginx is the inheritance of directives. Directives defined in higher-level blocks (like http) are inherited by lower-level blocks (like server and location). This allows you to set common configurations once and have them apply throughout your configuration hierarchy.


For example, if you define a log_format directive in the http block, it will be inherited by all server and location blocks within it:


http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server {
        listen       80;
        server_name  builddevops.com www.builddevops.com;
        location / {
            access_log /var/log/nginx/access.log main;
        }
    }
}

In this example, the log_format directive defined in the http block is used in the access_log directive within the location block.





Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page