Types of NGINX Directives
Here are the different types of NGINX directives:
- Global directives are instructions that apply to the entire NGINX configuration. For example, the
worker_processes
directive specifies the number of worker processes that NGINX will create.
worker_processes 2;
- HTTP directives are instructions that apply to the HTTP protocol. For example, the
gzip
directive enables gzip compression for HTTP responses.
gzip on;
- Server directives are instructions that apply to a specific server block. A server block is a configuration that defines a virtual host. For example, the
listen
directive specifies the port that a server block will listen on.
server {
listen 80;
}
- Location directives are instructions that apply to a specific location within a server block. A location is a path that NGINX will match against the requested URI. For example, the
root
directive specifies the directory that NGINX will serve static files from.
server {
listen 80;
location / {
root /var/www/html;
}
}
Here is an example of a complete NGINX configuration:
worker_processes 2;
http {
gzip on;
server {
listen 80;
location / {
root /var/www/html;
}
}
}
This configuration will create a web server that listens on port 80 and serves static files from the /var/www/html
directory.
Here are some additional examples of NGINX directives:
Error directives are instructions that control how NGINX handles errors. For example, the
error_log
directive specifies the file that NGINX will write error messages to.Logging directives are instructions that control how NGINX logs requests and responses. For example, the
access_log
directive specifies the file that NGINX will write access logs to.Timeout directives are instructions that specify how long NGINX will wait for a client to send a request or for a server to send a response. For example, the
client_max_body_size
directive specifies the maximum size of a client request body.
Array, action, and normal directives
- Array directives are directives that can take multiple values. For example, the
access_log
directive can take multiple values, one for each log file that NGINX should write to.
access_log logs/access.log main;
access_log logs/error.log error;
In this example, NGINX will write access logs to both the logs/access.log
and logs/error.log
files.
- Action directives are directives that do not take any values. For example, the
rewrite
directive does not take any values. Therewrite
directive tells NGINX to rewrite the requested URI before sending the request to the backend server.
rewrite ^/old-url$ /new-url permanent;
In this example, NGINX will rewrite requests for the /old-url
URI to the /new-url
URI.
- Normal directives are directives that take one value. For example, the
root
directive takes one value, which is the directory that NGINX should serve static files from.
root /var/www/html;
In this example, NGINX will serve static files from the /var/www/html
directory.
Here is a table that summarizes the different types of directives in NGINX:
Type | Description | Example |
Array directive | Can take multiple values | access_log logs/access.log main; |
Action directive | Does not take any values | rewrite ^/old-url$ /new-url permanent; |
Normal directive | Takes one value | root /var/www/html; |
For more information on NGINX directives, please refer to the NGINX documentation: http://nginx.org/en/docs/dirindex.html.
I hope this explanation of the types of directives in NGINX is helpful. Please let me know in the comments if you have any questions.
Subscribe to my newsletter
Read articles from Melvin C Varghese (melvincv) directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Melvin C Varghese (melvincv)
Melvin C Varghese (melvincv)
I am a DevOps Consultant. 3 years experience with Cloud technologies. 9 years total IT experience in the Linux, Networking and Data Visualization domains. Love listening to music and playing older PC games.