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:

1
ps -ef | grep nginx

The result should look like:

img

Nginx Process

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:

1
cat /etc/nginx/nginx.conf

Your result may differ from mine since I only pasted the executable code snippets:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log  warn;
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;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}

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.

More…

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.

More…

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.

More…

pid

The file stores Nginx’s main process ID.

More…

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.

More…

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.

More…

http

This directive is the most-used module, and its types of operations include io, hash, clients and socket.

More…

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.

More…

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.

More…

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.

More…

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.

More…

sendfile

The directive to disable or allow the call to the sendfile () function to send a file is called sendfile.

More…

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.

More…

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.

More…

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.

More…

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.

More…

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.

More…

root

This directive configures the project root path of the virtual server, which uses with the built-in variables provided by Nginx.

More…

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.

More…

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.

Buy me a coffeeBuy me a coffee