Write in front
Nginx is an excellent representative of high-performance and lightweight WEB servers. Because it provides a series of important features such as HTTP proxy and reverse proxy, load balancing, caching, etc., it is widely used in today's Web back-end services, and major Internet The company is also using it heavily, so as a developer, it is necessary to learn the use and configuration of Nginx.
In this article, we will start with a sample configuration list to briefly sort out the role and usage of various common configuration commands of the Nginx server.
Not much to say, serve!
This article has been included in the GitHub open source warehouse " Road to Programming " https://github.com/rd2coding/Road2Coding , which contains my own self-learning route + knowledge points combing. , interview test sites , my resume , few hardcore pdf notes , and my life programmer , welcome appreciation.
The overall structure of the Nginx configuration file
Draw a picture directly here and it is clear at a glance, and several large configuration modules can be seen very clearly.
It can be seen from the figure that it mainly contains the following parts:
1. Global block
This part of the configuration mainly affects the global Nginx, usually includes the following parts:
- Configure the user (group) to run the Nginx server
- Number of worker processes
- Nginx process PID storage path
- The storage path of the error log
- Introduction of configuration files
- ...
2. events block
This part of the configuration mainly affects the network connection between the Nginx server and the user, mainly including:
- Set the serialization of the network connection
- Whether to allow multiple network connections to be received at the same time
- Choice of event-driven model
- Configuration of the maximum number of connections
- ...
3. http block
- Define MIMI-Type
- Custom service log
- Whether to allow sendfile to transfer files
- Connection timeout
- Maximum number of single connection requests
- ...
4. server block
- Configure network port monitoring
- Access logs and error pages
- Name-based virtual host configuration
- IP-based virtual host configuration
- location block configuration
- ...
5. Location block
- location configuration
- Request root directory configuration
- Change location URI
- Website default homepage configuration
- ...
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;
}
}
}
Next, compare this sample configuration list to analyze in detail the meaning and usage of several main commands in the configuration file.
Nginx user (group) configuration
Configuration item format: user user [group];
- user: Specify the user who can run Nginx
- group: Specify the user group that can run Nginx (optional)
If the user command is not configured or configured as user nobody nobody
, all users can start the Nginx process by default.
Configuration of the number of worker processes
This is the key configuration for Nginx server to implement concurrent processing. The format of the configuration item is:
worker_processes number;
- number: The maximum number of worker processes that can be generated by the Nginx process
- If set to auto, Nginx will automatically detect
According to the experiment of the configuration list above, the number we configure for worker_processes is: 3. After starting the Nginx server, we can look at the Nginx process on the host in the background:
ps -aux | grep nginx
Obviously, it is easy to understand the meaning of the instruction worker_processes
Error log path configuration
Configuration item format: error_log file [optional log level];
- file: Specify the log output to a file file
- Common optional log levels include: info, debug, warn, error... etc.
Nginx process PID storage path configuration
Since the Nginx process runs in the background as a system daemon, this option is used to customize the save path of the configuration PID file.
Configuration item format: pid file;
- file: specify its storage path + file name
- If not specified, it will be placed in the path
logs/nginx.pid
Event-driven model configuration
Configuration item format: use model;
- Model options include: select, poll, kqueue, epoll, rtsig, etc...
Maximum number of connections configuration
Configuration item format: worker_connections number;
- The default value of number is 512, which means the maximum number of connections that each worker process can open at the same time.
Introduction of configuration files
This configuration is mainly used to introduce other or third-party Nginx configuration files into the current main configuration file
Configuration item format: include conf_file;
Serialized configuration of network connection
Configuration item format: accept_mutex on;
- This configuration is in the on state by default, which means that multiple Nginx worker processes receive connections are serialized to prevent multiple worker processes from competing for connections.
Speaking of this directive, we must first explain what the so-called "shock group problem" is. To explain the Nginx scenario, the general meaning is: when a new network connection comes, multiple worker processes will be awakened at the same time, but only one process can actually get the connection and process it. If the number of processes awakened each time is too large, it will actually affect part of the performance.
So here, if accept_mutex is on, then multiple workers will be processed in serial, and one of them will be awakened; if accept_mutex is off, then all workers will be awakened, but only one worker can get a new connection , Other workers will go to sleep again.
Whether this value is switched or not is actually linked to the specific scenario, which will affect the throughput of the system to a certain extent. Nginx turns on accept_mutex by default, which is also a conservative approach.
Multi-network connection receiving configuration
Configuration item format: multi_accept off;
- This configuration is off by default, 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.
MIME-Type definition
MIME-Type refers to the media type of network resources, that is, the type of resource requested by the front end.
Configuration item format:
include mime.types;
default_type type类型;
- The include configuration is used to include mime.types files
You can use cat mime.types
to view the content of the mime.types file. We found that it is a type structure, which contains the MIME types recognized by various browsers and the file suffixes of the corresponding types, as shown below:
Access log configuration
Configuration item format:
access_log path [format];
- path: path + name of custom access log
- format: Customize the format of the service log (optional).
Connection timeout configuration
Configuration item format: keepalive_timeout timeout [header_timeout];
- timeout represents the holding time of the server on the connection
- header_timeout means to set the timeout time in the Keep-Alive field in the header of the response message, which is optional.
sendfile configuration
Configuration item format:
sendfile on;
- The sendfile configuration is used to enable or disable the use of the sendfile() system call to transfer files, the default is off
- Note: In many Web Servers, the sendfile mechanism is introduced to achieve high-performance file transfer.
Network address monitoring configuration
Configuration item format:
- The first type: configure the listening IP address:
listen IP[:PORT];
- The second type: configure the listening port:
listen PORT;
Practical example:
listen 192.168.31.177:8080; # 监听特定IP和端口上的连接
listen 192.168.31.177; # 监听特定IP上所有端口的连接
listen 8080; # 监听特定端口上的所有IP的连接
Virtual host configuration based on name or IP
Configuration item format: server_name name1 name2 ...
- The name can have multiple parallel names, and the name here supports regular expression writing
Practical example:
server_name ~^www\.codesheep\d+\.com$;
As for the IP-based virtual host configuration, it is even simpler:
Configuration item format: server_name IP address
location configuration
The configuration item format is: location [ = | ~ | ~* | ^~ ] /uri/ {...}
- The uri here can contain fuzzy matching of regular expressions.
The content in square brackets in front of uri is optional. Several common situations are as follows:
- "=": Used for standard uri, for exact string matching
- "~": used for regular uri, indicating case-sensitive matching
- "~*": Used for regular uri, which means case-insensitive matching
- "^~": Used for standard uri, ^ is used for prefix matching, and ~ means case sensitive
Root directory configuration
Configuration item format: root path;
- path: Indicates the root directory path for Nginx to find resources after receiving the request
Of course, you can also change the URI request path received by location through the alias command. The command is:
alias path; # path为修改后的根路径
Default home page configuration
Configuration item format: index index_file ......
- index_file can contain multiple file names separated by spaces, and the first page is found, whichever page is used for response.
postscript
This article has been included in the GitHub open source repository " Road to Programming " https://github.com/rd2coding/Road2Coding , which contains the self-learning route + knowledge points , interview test sites , my resume , few hardcore pdf notes , and my life programmer , welcome appreciation.
See you next!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。