4. An overview of the default nginx.conf file
Contents
Have a glance of Nginx default configuration file
The first place we need to visit after the installation should be the nginx.conf file. This file contains everything we need for rendering a very basic static HTML web page on HTTP port 80.
Locate it through the main process
If you have started your Nginx process after the installation then we can get its configuration file through the master process:
|
|
The result should look like:
Output the configuration to the screen
We See the nginx.conf File’s Location As shown in the picture above, the complete path for nginx.conf is /etc/nginx/nginx.conf, so let’s print that content to console by using cat command:
|
|
Your result may differ from mine since I only pasted the executable code snippets:
|
|
Purpose of each instruction
Summary
I'm just going to make a brief and practical description of these instructions:user
The default user Nginx uses at run time is nginx, we usually don’t change this value. If you want Nginx to be able to access a directory, then you need to set the ownership of that directory to nginx.
worker_processes
Specifies the number of worker processes running in parallel after Nginx starts. This number is usually consistent with the number of logical CPUs on your server.
Usually, people just set it to auto, and this asks Nginx to use as many logical CPUs as it could to create a sufficient amount of worker processes.
error_log
The file saves any Nginx error information. We can also specify a logging level: warn, error, crit, alert, or emerg.
By default, Nginx uses error, this logs error, crit, alert, and emerg messages.
events
This block can specify the maximum number of connections per worker process, which event-driven model should be selected to handle connection requests, whether to serialise the network connection and so on.
worker_connections
The maximum number of connections each worker process can connect to and process at the same time, including connections between proxy servers. The maximum number of connections cannot exceed the current maximum number of open files, and the worker_rlimit_nofile directive can do the trick.
http
This directive is the most-used module, and its types of operations include io, hash, clients and socket.
include
This directive is used to load the mapping list of MIME(multipurpose internet mail extension) protocol types to the configuration file.
Of course, the same is true of the instructions used to include other configuration files, such as include /etc/nginx/sites-enabled/* includes all configuration files under the sites-enabled directory into the configuration file.
default_type
As we can see from the configuration above, Nginx’s default MIME type for responding a request is octet-stream, octet means 8 bits, and one byte is equal to 8 bits, so the octet-stream here can be understood as a stream of bytes.
If Nginx does not match the response request to the mime-type corresponding in the /etc/nginx/mime.types file included above, then the type specified by default_type is used.
log_format
log_format is the format of the output content to access log, and log_format can specify common variables of Nginx, common variables are the variables that exist only at the moment when Nginx writes the log into the access_log file.
access_log
The access_log instruction can specify the log file of the Nginx access request. We can specify the output format of the log, that is, the format arranged by log_format.
sendfile
The directive to disable or allow the call to the sendfile () function to send a file is called sendfile.
keepalive_timeout
The first parameter sets the timeout between the server and the client to maintain a keep-alive connection and to set zero means disabling the keep-alive connection between the server and the client.
The second parameter is used to set the time value in “keep-alive: timeout=time” to the response header information so that the client browser knows when to close its keep-alive connection to the server.
server
This directive specifies a virtual server-related configuration, and we can include some common virtual server-related configuration information into the context of server directive, including server name (i.e. domain name), the port number to listen on, project root path, address redirection, SSL security certificate, and so on.
listen
The directive we used to specify a port number of the virtual server, and if we don’t specify a port, the default is 80.
server_name
With this instruction, we can assign a name (domain name) to the virtual server, and we can also specify the wildcard (*) domain name (*.example.com or www.example.*), which is very convenient.
location
This directive can be used to configure a unified resource identifier (domain_name/xxx/query) that is accessible to the outside world, where we can configure flexible address matching patterns using regular expressions.
root
This directive configures the project root path of the virtual server, which uses with the built-in variables provided by Nginx.
index
This directive configures one or more home pages under the project’s root path, and the famous home page is index.html or index.php. When someone requests the location, Nginx matches back and forth from several of the front pages configured by the index directive. If the previous home page does not exist, use the next one, and so on.
References: Core functionality, Setting up hashes, Core Module, Log Module, Index Module
If you think the content of this article has helped you, and if you would like some more high-quality materials in the near future, please give me some modest support.
Author Dong Chen
LastMod Thu Oct 11 2018