Understanding NGINX Configuration Directory Structure and Files
NGINX is a popular open-source web server software that is widely used to serve web applications. One of the key aspects of NGINX is its configuration, which is managed through a set of files and directories. In this blog post, we will delve into the NGINX configuration directory structure, explain the default NGINX core config file, identify included directories/files, and describe the order of how these files are merged into the running configuration.
NGINX Configuration Directory Structure
The NGINX configuration directory structure consists of several files and subdirectories. The main configuration directory is typically located at /etc/nginx/ or /usr/local/nginx/conf/ depending on your operating system and installation method.
Here’s a breakdown of the main directories and files:
nginx.conf: The main configuration file for NGINX. This file contains the global configuration settings that apply to all server blocks.mime.types: A file that maps file extensions to MIME types.fastcgi_params,uwsgi_params, andscgi_params: Files that contain parameters for FastCGI, uWSGI, and SCGI protocols respectively.sites-available/andsites-enabled/: Directories that contain server blocks for virtual hosts. Thesites-availabledirectory contains all the available server blocks, while thesites-enableddirectory contains the enabled server blocks.conf.d/: A directory that contains additional configuration files that can be included in the main configuration file.
Default NGINX Core Config File
The default NGINX core config file is located at /etc/nginx/nginx.conf (or /usr/local/nginx/conf/nginx.conf on some systems). This file contains the basic configuration settings for NGINX, such as worker processes, error logs, and connection timeouts.
Here’s an example of a basic NGINX core config file:
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
error_log /var/log/nginx/error.log;
# Enable keepalive for HTTP/1.1
keepalive_timeout 65;
upstream backend {
server localhost:8080;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
This file sets up a basic HTTP server that listens on port 80, serves files from a backend server at localhost:8080, and logs access and error information.
Included Directories/Files
NGINX includes several directories/files that contain additional configuration settings. These files are merged into the running configuration in a specific order:
/etc/nginx/mime.types: Maps file extensions to MIME types./etc/nginx/fastcgi_params,/etc/nginx/uwsgi_params, and/etc/nginx/scgi_params: Contain parameters for FastCGI, uWSGI, and SCGI protocols respectively./etc/nginx/conf.d/*.conf: Additional configuration files that can be included in the main configuration file.sites-available/*.conf: Server blocks for virtual hosts.
Order of Included Files
When NGINX reads its configuration files, it follows a specific order:
- The main configuration file (
nginx.conf) is read first. - The included files from
/etc/nginx/mime.types,/etc/nginx/fastcgi_params,/etc/nginx/uwsgi_params, and/etc/nginx/scgi_paramsare read next. - The additional configuration files from
/etc/nginx/conf.d/*are read after that. - Finally, the server blocks from
sites-available/*are read and merged into the running configuration.
Directive Inheritance and Overriding
In NGINX, directives can inherit values from parent blocks or override them if specified in a child block. For example:
http {
listen 80;
server_name example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 8080;
server_name example.net;
index index.html;
location / {
try_files $uri $uri/ =404;
internal;
}
}
In this example, the index directive is inherited from the parent http block by the server block for example.net. However, when specifying a new value for the try_files directive in the location block, it overrides the value inherited from the parent block.
In conclusion, understanding NGINX’s configuration directory structure, default core config file, included directories/files, and directive inheritance properties is crucial for configuring your NGINX setup effectively. By following this guide, you can create complex configurations for your web servers and manage them efficiently.