NGINX Configuration Basics
NGINX can be configured through its configuration file ‘nginx.conf’. The default nginx.conf file (that comes with installation) is configured to listen on port 80 and will serve a default test page (as shown in figure 1.1 of chapter 1).
Now instead of taking this default nginx.conf file as a starting point, we will take a bottom up approach where we’ll try to explain various aspects of nginx.conf file such as:
- where it’s located?
- what is its file format?
- what are it’s building blocks?
- how is it structured?
- how to configure a simple web server?
- how to verify our configuration changes?
- how to apply these changes on a to running service?
So lets start our journey!
2.1 – File Location
On most of the linux distributions, the standard location of this file is
/etc/nginx
Other common locations of this file are:
/usr/local/etc/nginx
/usr/local/nginx/conf
You can also findout this path using the command:
$ nginx -V 2>&1 | --color -- --conf-path=
Running this command will highlight the ‘–conf-path=’ so you can see the path.
On windows, if you have setup the nginx under ‘c:\nginx’, then it will be under the ‘conf’ folder, i.e.
c:\nginx\conf
2.2 – File Format
nginx.conf is a plain text file that can be edited in any text editor you are comfortable with. However for some popular text editors third party plugins are now available to help you in defining the configuration.
Visual Studio Code comes with a community plugin for writing the nginx.conf file. Starting with this may be useful as it provides contextual help.
2.3 – Building Blocks
NGINX is a software whose functionality is based on modules. These modules expose their functionality in the form of commands known as directives.
Modules are classified as core and non-core. All the basic functionality is embedded in core modules.
Directives are the basic building block. They can be considered analogous to a function of a typical programming language. Similar to function parameters, we pass one or more input values to directive with space as a separator.
Directives are classified as simple and block directive.
Simple Directive
As the name suggests they are simple commands of the form
<directive> <value1> <value2>;
Each simple directive is defined on a single line that ends with a semicolon ‘;’. Most of the simple directives just accepts a single value.
As an example consider how a simple directive is defined within nginx.conf file:
#set the number of cpu cores to use (one per worker process)
worker_processes 2;
here you need to understand:
- # symbol is used to define comments
- worker_processes is a directive which tells the master process about how many worker processes needs to be spawned
- 2 is the value (as parameter) that tell master process to spawn two worker processes
- a semicolon ‘;’ is required at the end of command
Refer to section 1.3 (chapter 1) if you need some explanation on master and worker process.
Block Directive
A block directive is like a container that can hold one or more simple and block directives. Its form is something like:
<block1> {
<directive1> <value1>;
<directive2> <value2>;
...
<block2> {
<directive3> <value3>;
}
}
here
- block1 is the block directive that contains two simple directives and a single block directive (block2)
- directive1, directive2 and directive3 are simple directives
- value1, value2, value3 is the parameter value of respective directive
- block2 is another block directive which is contained within the parent block (block1)
As an example consider how a block directive is defined within nginx.conf file:
events {
worker_connections 1024;
}
here we need to understand
- events is the name of block directive whose purpose is to define how the worker processes will perform connection processing
- { } defines the scope of the block directive
- within the { }, a simple directive worker_connections tells a worker process the limit of concurrent connections
- 1024 is the value that tells a worker process to open a maximum of 1024 concurrent connections
2.4 – File Structure
There is an underlying structure in nginx.conf file that needs to be understood well. This structure is based on ‘context’
A context is a area (or region) within the nginx.conf file where certain directives (simple/block) becomes eligible. Using any directive outside its eligibility area, results in an invalid configuration file.
In general, there are two types of context:
- Main Context
- Block Context
Main Context
This is the top most context and is also referred to as global or file context. A very limited set of directives can be defined at this level. Some commonly used simple directives at this level are:
| Simple Directive | Description |
|---|---|
| user | Defines the OS user and group that will own the spawned worker process. It accepts two parameters. The first parameter is the OS user name while the second parameter is the OS group name. |
| worker_processes | Defines the total number of worker processes that needs to be spawned. It accepts only one parameter. Parameter values can be postive integer values less than or equal to number of cpu cores of the system. If you want the system to auto assign the value based on the cpu cores, then you should set the parameter value to ‘auto’ |
| pid | Defines the file name and path of the .pid file of master process |
At main context level, only the following 4 block directives are eligible:
| Block Directive | Description |
|---|---|
| events | Defines how the worker processes will perform connection processing. |
| http | Defines how the http traffic will be managed |
| Defines how the email traffic will be managed | |
| stream | Defines tcp/udp traffic will be managed |
Block Context
Each block directive sets its own context. This context is called block context. Since a block directive can contain another block directive, the containing block context is referred to as parent context while the contained block context is referred to as child context.
For example, consider http directive:
http {
server {
}
}
here
- http is a block directive and its context is referred to as http context
- server is another block directive within the existing http context and its context is referred to as server context
- from server context, the http context is referred to as parent context
- similarly from http context, the server context is referred to as child context
This parent-child relationship between block context results in:
- Any eligible directive that is defined at parent level, but not at child level will inherit it from parent context.
- If this directive is also defined at child level, then within child context, its value will be used (instead of value from parent context)
2.5 – Configure A Simple Web Server
Now its time to apply what we have learned so far. We’ll setup an simple web server that will serve files from file system.
#you can give name of any valid operating system user
user nobody;
#here we only want a single worker process for processing http requests
#you may select as many as your system allows
worker_processes 1;
#the default file name and path where it will be placed.
#this file contains the process id of our master process
pid /run/nginx.pid;
#a block directive to for configuring connection processing logic
events {
#specifies how many concurrent connections a worker process can accept
worker_connections 1024;
}
#a block directive for handling http traffic
http {
#another block directive that is used to define a virtual host
server {
#the port on which this virtual host listens
listens 80;
#the path from where this server will serve files
#also known as document root
#you can set this to any of your existing folder
root /usr/share/nginx;
}
}
So far we have created a configuration file from scratch. In the next two section we’ll learn how to verify and apply changes.
2.6 – Verify Configuration Changes
To verify whether our file is syntatically correct or not, use the following command
$ sudo service nginx configtest
If it results in
Testing nginx configuration … [OK]
then nginx.conf file is in a valid state that you can apply. However if it results in
Testing nginx configuration … [FAIL]
Then you need to review your changes in order to fix the root cause.
2.7 – Apply Configuration Changes
Assuming your nginx.conf file is in valid state, you can apply configuration changes to running service using the following command
$ sudo service nginx reload
With above command the master process is not affected. Only worker processes are gracefully recreated. This is quite useful feature as it give ability to make configuration changes without disrupting your service.
In a currently running service, if you mistakenly runs the above command when the nginx.conf file is in invalid state, the reload will fail without disrupting the existing service.
2.8 – Review Questions
Q.1 – Using command (discussed in section 3.1) find out the path of the the nginx.pid file.
Q.2 – All simple directives just accept a single parameter value. [True/False]
Q.3 – All block directives require curly braces {} after its name. [True/False]
Q.4 – Which of the following block directive cannot be used in main context:
- http
- server
- events
Q.5 – If your system has 4 cpu cores. Setting
worker_connections auto;
will result in spawning of how many worker processes?
Q.6 – Consider the following snippet:
..
http {
root /usr/share/nginx;
server {
listen 80;
}
server {
listen 90;
root /home/moiz;
}
}
..
What will be the document root (path from where user request will be served) for:
Q.7 – Take a note of process ids assigned to nginx services on your system. Now reload (instead of restarting as done in Q.6 of previous chapter) the nginx service. Again take a note of assigned process ids. Do you got the same process ids?
2.9 – Answers to Review Questions
A.1 – In section 3.1 we looked for the path of nginx.conf file with the search keyword ‘–conf-path=’. Now just replace the search keyword with ‘–pid-path=’
$ nginx -V 2>&1 | grep --color -- --pid-path=
In our case, the nginx.pid is located at:
/run/nginx.pid
A.2 – False.
While most of the simple directives accept a single parameter value, but this in not true for all. For example the simple directive ‘user’ accepts 2 parameter.
A.3 – True.
A.4 – ‘server’ block directive cannot be defined directly under the main context. It can only come under the http context.
A.5 – 4 worker processes will be spawned
A.6 – http://localhost/: As no root path has been defined for this virtual server, therefore it will inherit the root path defined in the parent context. So the document root of this virtual server will be /usr/share/nginx.
http://localhost:90/: As root path has already been defined for this virtual server, it will override any such settings defined in the parent context.
A.7 – When we run the command,
sudo service nginx reload
only worker processes are recreated. There is no change in master process id.






