Writing in the front
Nginx is an excellent representative of high-performance lightweight WEB server. Because it provides HTTP proxy and reverse proxy, load balancing, caching and a series of important features, it is widely used in today’s WEB back-end services, and major Internet companies are also heavily used. Therefore, as a developer, It is essential to learn how to use and configure Nginx.
In this article, we’ll start with a sample configuration list to briefly review the functions and uses of various common configuration directives for Nginx servers.
Without further ado, serve!
This article is on GitHub open source repository “Programming Path” github.com/rd2coding/R… There are 6 major programming direction (post) I organized the self-study route + knowledge points, interview test points, my resume, several core PDF notes, and my programmer life, welcome to appreciate.
The overall structure of Nginx configuration files
Draw a picture directly here be clear at a glance, a few big configuration module see very clear.
As can be seen from the figure, it mainly includes the following parts:
1. The global block
This configuration affects Nginx globally and usually includes the following parts:
- Configuring the user (group) to run the Nginx server
- Number of worker processes
- Nginx process PID storage path
- Path for storing error logs
- Importing configuration files
- .
2. The events
This configuration mainly affects the network connection between the Nginx server and users, including:
- Set up serialization of network connections
- Whether to accept multiple network connections at the same time
- Selection of event-driven models
- Configure the maximum number of connections
- .
3. HTTP block
- Define MIMI – Type
- Customize service logs
- Whether files can be transferred in SendFile mode
- Connection timeout
- Maximum number of single connection requests
- .
4. Server block
- Configure network port listening
- Access logs and error pages
- Name-based virtual host configuration
- Ip-based virtual host configuration
- The location of configuration
- .
5. The location of
- The location configuration
- Request root configuration
- Change the URI of a location
- The default homepage is configured
- .
Example analysis of a configuration list
Here is an example of a brief Nginx configuration list:
The configuration code is as follows:
user nobody nobody; worker_processes 3; error_log logs/error.log; pid logs/nginx.pid; events { use epoll; 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; keepalive_timeout 65; server { listen 8088; server_name codesheep; access_log /codesheep/webserver/server1/log/access.log; error_page 404 /404.html; location /server1/location1 { root /codesheep/webserver; index index.server2-location1.htm; } location /server1/location2 { root /codesheep/webserver; index index.server2-location2.htm; } } server { listen 8089; Server_name 192.168.31.177; access_log /codesheep/webserver/server2/log/access.log; error_page 404 /404.html; location /server2/location1 { root /codesheep/webserver; index index.server2-location1.htm; } location /srv2/loc2 { alias /codesheep/webserver/server2/location2/; index index.server2-location2.htm; } location = /404.html { root /codesheep/webserver/; index 404.html; }}}Copy the code
The following is a detailed analysis of the meaning and usage of several main instructions in the configuration file against the sample configuration list.
Nginx user (group) configuration
Format of configuration items: user user [group];
- User: Specifies the user that can run Nginx
- Group: specifies the user group that can run Nginx (optional)
If the user directive is not configured or is set to user nobody nobody, all users can start the Nginx process by default.
Worker Specifies the number of worker processes
This is the key configuration for Nginx server to implement concurrent processing. The configuration item format is as follows:
Worker_processes number number;
- Number: maximum number of worker processes that the Nginx process can generate
- If set to auto, Nginx automatically detects
The number of worker_processes configured for worker_processes is as follows:
ps -aux | grep nginx
Copy the code
Clearly, it is easy to understand the meaning of the worker_PROCESSES directive
Error log path configuration
Format of configuration item: error_log file [Optional log level];
- File: indicates that logs are output to a file file
- Common optional log levels are INFO, DEBUG, WARN, error… Etc.
Configure the PID storage path for Nginx processes
Since the Nginx process runs in the background as a system daemon, this option is used to customize the path to save PID files.
Configuration item format: PID file;
- File: specifies the storage path and file name
- If the default path is not specified
logs/nginx.pid
Event-driven model configuration
Configuration item format: use model;
- Model Select, poll, kqueue, epoll, and rtsig……
Maximum number of connections
Format of the configuration item: worker_connections Number Number.
- Number The default value is 512, indicating the maximum number of connections that each worker process can open at the same time.
Importing configuration files
This configuration is used to import other or third-party Nginx profiles into the current master profile
Configuration item format: include conf_file;
Serialized configuration of network connections
The format of the configuration item is accept_mutex on;
- This configuration defaults to on, which means that multiple Nginx worker processes receive connections and serialize them, preventing multiple worker processes from competing for connections.
Speaking of this directive, it is necessary to first explain what is called the “stampede problem”. In terms of the Nginx scenario, it basically means: when a new network connection comes, multiple worker processes will wake up at the same time, but only one process can actually get the connection and process it. If you wake up too many processes at a time, it can actually affect some performance.
So here, if accept_mutex on is accepted, multiple workers will be processed in serial mode, and one of them will be woken up; Otherwise, if accept_mutex off, all workers will wake up, but only one worker can obtain a new connection, and the other workers will go back to sleep.
Whether this value is on or off depends on the specific scenario, which will affect the throughput of the system to some extent. Nginx turns accept_mutex on by default, which is also conservative.
Multiple network connection receive configuration
Configuration item format: multi_accept off;
- This configuration defaults to off, which means that each worker process can only receive one newly arrived network connection at a time. If you want each Nginx worker process to receive multiple network connections at the same time, you need to enable this configuration.
The MIME Type definition
Mime-type refers to the media Type of the network resource, that is, the resource Type requested by the front-end.
Configuration item format:
include mime.types; Default_type Type.Copy the code
- The include configuration is used to include mime.types files
Types is a types structure that contains mime types recognized by various browsers and file name extensions for those types, as shown below:
Access Log Configuration
Configuration item format:
access_log path [format];
Copy the code
- Path: user-defined access log path + name
- Format: Specifies the format (optional) for customizing service logs.
Connection Timeout Configuration
Configuration item format: Keepalive_timeout timeout [header_timeout];
- Timeout Indicates the hold time of the connection on the server
- Header_timeout Specifies the timeout period in the keep-alive field in the header of the reply packet. This parameter is optional.
Sendfile configuration
Configuration item format:
sendfile on;
Copy the code
- The sendFile configuration is used to turn on or off transferring files using the sendFile () system call, off by default
- Note: In many Web servers, sendFile mechanism is introduced to achieve high performance file transfer.
Configure network address listening
Configuration item format:
- Type 1: Configure the listening IP address:
listen IP[:PORT];
- Second: Configure the listening port:
listen PORT;
Practical examples:
Listen 192.168.31.177:8080; Listen 192.168.31.177; Listen 8080; Listen for connections to all IP addresses on a particular portCopy the code
Virtual host configuration based on name or IP
Format of the configuration item: Server_name name1 name2…
- Name can have multiple parallel names, and the name here supports regular expression writing
Practical examples:
server_name ~^www\.codesheep\d+\.com$;
Copy the code
For IP-based virtual host configuration is even simpler:
Configuration item format: server_name IP address
The location configuration
Configuration item format for: the location [= | | | ~ ~ * ^ ~] / uri / {… }
- The URI here can contain fuzzy matching of regular expressions.
The contents in square brackets before the URI are optional. Several common cases are as follows:
- “=” : used for standard URIs for exact string matching
- “~” : indicates a regular URI for case-sensitive matching
- ~* : indicates a regular URI that is case-insensitive
- ^~ : used for standard URIs. ^ matches the prefix. ~ indicates case-sensitive URIs
Root Configuration
Configuration item format: root path;
- Path: indicates the root directory path for Nginx to search for resources after receiving a request
Of course, you can also change the URI request path received by location via the Alias directive:
alias path; # path indicates the modified root pathCopy the code
Default Home Page Configuration
Format of the configuration item: index index_file……
- Index_file can contain multiple filenames separated by Spaces and will respond to whichever page is first found.
Afterword.
This article is on GitHub open source repository “Programming Path” github.com/rd2coding/R… There are 6 major programming direction (post) I organized the self-study route + knowledge points, interview test points, my resume, several core PDF notes, and my programmer life, welcome to appreciate.
See you next!