[Nginx things] description of Nginx configuration file

Profile path

cd /usr/local/nginx/conf
cat nginx.conf

The default configuration is as follows

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

The configuration file consists of three parts: Global block, events block and http block

Global block

From the beginning of the configuration file to the events block, some configuration instructions affecting the overall operation of the Nginx server will be set, mainly including configuring the users (groups) running the Nginx server, the number of worker process es allowed to be generated, the process PD storage path, log storage path and type, and the introduction of the configuration file.

# This is the key configuration of Nginx server concurrent processing service, worker_ The larger the processes value, the more concurrent processing can be supported, but it will be restricted by hardware, software and other devices
worker_processes  1;

events block

The instructions involved in the events block mainly affect the network connection between nginx service and users. Common settings include whether to enable the serialization of network connections under multiple work process es, whether to allow multiple network connections to be received at the same time, which event driven model is selected to process connection requests, and the maximum number of connections each word process can support at the same time.

The configuration of this part has a great impact on the performance of Nginx, so it should be configured flexibly in practice.

# Indicates that the maximum number of connections supported by each work process is 1024
events {
    worker_connections  1024;
}

http block

http block is the most frequent part of Nginx server configuration. Most functions such as proxy, cache and log definition and the configuration of third-party modules are here.

It should be noted that htp blocks can also include http global blocks and server blocks.

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

① http global block

http global block configuration instructions include file import, MIME-TYPE definition, log customization, connection timeout, maximum number of single link requests, etc.

② server block

This is closely related to the virtual host. From the perspective of users, the virtual host is exactly the same as an independent hardware host. This technology is produced to save the hardware cost of Internet server.

Each http block can include multiple server blocks, and each server block is equivalent to a virtual host.

Each server block is also divided into global server blocks and can contain multiple locaton blocks at the same time.

a. Global server block

The most common configurations are the listening configuration of the virtual machine host and the name or IP configuration of the virtual host.

b.location block

A server block can be configured with multiple location blocks.

The main function of this part is to match the strings other than the virtual host name (or IP alias) (such as the previous / url string) based on the request string received by the nginx server (such as server_name / URI string), and process specific requests. Address orientation, data caching, response control and other functions, as well as the configuration of many third-party modules are also carried out here.

	server {
  			# Listening port number
        listen       80;
        # host name
        server_name  localhost;
				# Proxy localhost/
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

location instruction description

# Used to match URI(Uniform Resource Identifier)
location [ = | ~ | ~* | ^~ ] uri {

}

1. =: before using the uri without regular expression, the request string is required to strictly match the uri. If the match is successful, stop the downward search and process the request immediately.

2. ~: used to indicate that the uri contains regular expressions and is case sensitive

3. ~ *: used to indicate that the uri contains regular expressions and is case insensitive.

4. ^ ~: before using the uri without regular expression, the nginx server is required to find the location with the highest matching degree between the identification uri and the request string, and then use this location to process the request immediately instead of using the regular uri in the location block to match the request string.

Note: if the uri contains regular expressions, it must be marked with or *.

Keywords: Nginx

Added by Humpty on Fri, 18 Feb 2022 15:23:29 +0200