“This is the 13th day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021”.

Previous articles:

I met Nginx

The installation of the nginx

Nginx core is the default configuration file in/usr/local/Nginx/conf/Nginx. Conf

The following is the main configuration

worker_processes 1; events { worker_connections 1024; } 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; Instruction name instruction value; Nginx {Nginx {name}; } # HTTP block is an important part of Nginx server configuration, proxy, caching, logging, third-party module configuration... HTTP {instruction name instruction value; Server {#server block, is Nginx configuration and virtual host related content directive name directive value; Location / {#location block, based on the Nginx server to receive the request string and the value of the location to match, the specific request processing instruction name instruction value; }}... }Copy the code

The nginx.conf configuration file has three default blocks: the global block, the Events block, and the HTTP block

HTTP blocks can be configured with multiple Server blocks, and each server block can be configured with multiple Location blocks.

Global block

The user instructions

User: used to configure the user and user group that runs the worker process on the Nginx server.

grammar user user [group]
The default value nobody
location Global block

/configure –user=user –group=group. If both Settings are set, the configuration in the configuration file will take effect.

The work process instructions

Master_process: Specifies whether to start the working process.

grammar master_process on/off;
The default value master_process on;
location Global block

Worker_processes: Used to configure the number of Nginx generated worker processes, which are key to Nginx servers implementing concurrent processing services. In theory, the larger the workder process value is, the more concurrent processing can be supported. However, the actual setting of this value is limited by the server itself. It is recommended to keep this value consistent with the number of cores on the server CPU.

grammar worker_processes num/auto;
The default value 1
location Global block

If you set worker_PROCESSES to 2, you see the following:

Other instructions

Daemon: Sets whether Nginx is started as a daemon.

A daemon process is a service process executed in the Linux background. It is independent of the control terminal and does not stop when the terminal is shut down.

grammar daemon on/off;
The default value daemon on;
location Global block

Pid: indicates the path to the file stored by the process ID of the Nginx master process.

grammar pid file;
The default value Defaults to: / usr/local/nginx/logs/nginx pid
location Global block

This property can be specified by./configure –pid-path= path

Error_log: used to configure the directory for storing Nginx error logs

grammar Error_log file [Log level];
The default value error_log logs/error.log error;
location Global block, HTTP, Server, Location

This property can be specified by./configure –error-log-path= path

The log level values are as follows: The debug | info | notice | warn | error | crit | alert | emerg, translated to try | | | | | warning notice error information critical warning | | emergency, the recommended Settings don’t set to the level of the info below, because will bring a lot of disk I/O consumption, Nginx performance is affected.

(5) include: used to introduce other configuration files, make Nginx configuration more flexible

grammar include file;
The default value There is no
location any

The events of

(1) accept_mutex: set the serialization of Nginx network connections

grammar accept_mutex on/off;
The default value accept_mutex on;
location events

This configuration can be used mainly to solve the so-called “stampede” problem. At a certain moment, the client sends a request for connection. Nginx background works in multi-process mode, which means that multiple worker processes will be woken up at the same time, but only one process will be able to obtain connection. If too many processes are woken up each time, the overall performance of Nginx will be affected. If you set this value to on, multiple Nginx processes will be serial numbered to wake up connections one by one, preventing multiple processes from competing for connections.

(2) multi_accept: Used to set whether to accept multiple network connections at the same time

grammar multi_accept on/off;
The default value multi_accept off;
location events

If multi_accept is disabled, nginx can accept only one new connection at a time. Otherwise, a worker process can accept all new connections simultaneously

(3) worker_connections: used to set the maximum number of connections for a single worker process

grammar worker_connections number;
The default value worker_commections 512;
location events

The number of connections here includes not just the number of connections to the front-end user, but all possible connections. In addition, the value of number cannot be greater than the maximum number of file handles supported by the operating system.

(4) Use: used to set which event driver the Nginx server chooses to handle network messages.

grammar use method;
The default value Depends on the operating system
location events

Note: The selected event processing model is an important part of the Nginx optimization section. The optional values of method include select/poll/epoll/kqueue, etc. Before preparing the centos environment, we emphasized that the Linux kernel should be used above 2.6. In order to optimize Nginx using the epoll function.

In addition, the selection of these values can be used at compile time

– with – select_module, – without – select_module,

–with-poll_module, –without-poll_module to set whether the corresponding event-driven module needs to be compiled into the Nginx kernel.

Events directive configuration instance

Open the Nginx configuration file nginx.conf and add the following configuration

events{
    accept_mutex on;
    multi_accept on;
    worker_commections 1024;
    use epoll;
}
Copy the code

Start the test

./nginx -t
./nginx -s reload
Copy the code

HTTP block

Defines the MIME Type

We all know that a browser can display a wide variety of files and media resources such as HTML, XML, GIF and so on. In order to distinguish these resources, the browser needs to use MIME Type. So MIME Type is the media Type of the web resource. As a Web server, Nginx also needs to be able to recognize the type of resource requested by the front end.

In the Nginx configuration file, there are two lines of configuration by default

include mime.types;
default_type application/octet-stream;
Copy the code

Default_type: used to configure the default MIME type for Nginx to respond to front-end requests.

grammar default_type mime-type;
The default value Default_type text/plain.
location HTTP, Server, location

Default_type is preceded by include mime.types, which we have covered previously. This is equivalent to adding the mapping between MIMT types in mime.types files and file extensions of related types to the current configuration file.

To illustrate:

Sometimes you need to return the specified text string or JSON string when requesting some interface. If the logic is very simple or simply a fixed string, then you can use Nginx fast implementation, so that there is no need to write programs to respond to the request, which can reduce the server resource footprint and response performance is very fast.

How to achieve:

Location /get_text {# this can also be set to text/plain default_type text/ HTML; return 200 "This is nginx's text"; } location /get_json{ default_type application/json; return 200 '{"name":"TOM","age":18}'; }Copy the code

Customize service logs

The log types in Nginx are access.log and error.log.

Access. log: records all access requests of users.

Error. log: records error information about the running of nginx, but not user access requests.

The Nginx server supports setting the format, size, and output of service logs using two directives: access_log and log_format.

(1) Access_log: Used to set the related attributes of user access log.

grammar access_log path[format[buffer=size]]
The default value access_log logs/access.log combined;
location http.server.location

(2) log_format: specifies the output format of logs.

grammar log_format name [escape=default/json/none] string…. ;
The default value log_format combined “…” ;
location http

Other Configuration instructions

(1) SendFile: Sets whether the Nginx server uses sendFile () to transfer files. This property can greatly improve Nginx’s performance in handling static resources

grammar Sendfile on/off;
The default value sendfile off;
location HTTP, Server, location

(2) Keepalive_timeout: used to set the timeout period for the long connection.

Why use Keepalive?

We all know that HTTP is a stateless protocol. A client sends a TCP request to a server, and the server responds and disconnects. The keepalive mode is used to tell the server to keep the TCP connection open after processing a request. If other requests are received from the client, the keepalive mode is used to tell the server to keep the TCP connection open. The server will make use of the unclosed connection without creating a new connection to improve efficiency. However, the connection cannot be maintained all the time. In this case, if there are too many connections, the performance of the server will deteriorate, and at this time, we need to set its timeout time.

grammar keepalive_timeout time;
The default value keepalive_timeout 75s;
location HTTP, Server, location

Keepalive_requests set the number of keep-alive requests.

grammar keepalive_requests number;
The default value keepalive_requests 100;
location HTTP, Server, location