Article directory
Basic operating principle of Nginx
The Nginx process uses the classic "Master-Worker" model. After Nginx starts, there will be a master process and multiple worker processes. The master process is mainly used to manage the worker process, including: receiving signals from the outside world, sending signals to each worker process, monitoring the running status of the worker process, and automatically restarting the new worker process when the worker process exits (under abnormal circumstances). . The worker process mainly handles basic network events. Multiple worker processes are equal, and they compete equally for requests from clients. Each process is independent of each other. A request can only be processed in a worker process, and a worker process cannot process requests from other processes. The number of worker processes can be set. Generally, it will be set to be consistent with the number of CPU cores of the machine. The reason for this is inseparable from the nginx process model and event processing model.
Basic configuration file for Nginx
The default configuration file of Nginx can be generally summarized into three modules: global block, events block, and http block.
# 全局模块
events {
# events模块
}
http {
# http全局模块
server {
# server全局模块
location [PATTERN]{
# location模块
}
}
}
Global block: Configure directives that affect nginx globally. Generally, there are user groups running the nginx server, nginx process pid storage path, log storage path, configuration file import, and the number of worker processes allowed to be generated.
events块
: The configuration affects the nginx server or the network connection to the user. There are the maximum number of connections for each process, which event-driven model to choose to handle connection requests, whether to allow multiple network connections to be accepted at the same time, enable serialization of multiple network connections, etc.
http块
: You can nest multiple servers, configure most of the functions such as proxy, cache, log definition, and third-party module configuration. Such as file import, mime-type definition, log customization, whether to use sendfile to transfer files, connection timeout, number of single connection requests, etc.
server块
: Configure the relevant parameters of the virtual host. There can be multiple servers in one http.
location块
: Configure the routing of requests and the processing of various pages.
Nginx's default configuration file nginx/conf/nginx.conf is a simple version without comments as follows
worker_processes 1; # 允许进程数量,建议设置为cpu核心数或者auto自动检测,注意Windows服务器上虽然可以启动多个processes,但是实际只会用其中一个
events {
#单个进程最大连接数(最大连接数 = 连接数 * 进程数)
#根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表(是conf目录下的一个文件)
include mime.types;
#默认文件类型,如果mime.types预先定义的类型没匹配上,默认使用二进制流的方式传输
default_type application/octet-stream;
#sendfile指令指定nginx是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度。
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;
#/50x.html文件所在位置
location = /50x.html {
root html;
}
}
}
include
It means importing a configuration file. We can not only import types files, but also import conf files, such as encapsulating the server virtual host configuration into a separate configuration file for import. When the browser requests static resources from the Nginx server, the browser cannot know what kind of file it is through the suffix name. It can only be identified through the ContextType of the response, for example, text/html
etc. text/css
, so this file is needed to tell the browser what kind of file to render.
The mime.types file is as follows
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
}
server
The most important thing is the server virtual machine host
Virtual host is to virtualize one physical server into multiple servers, so that multiple sites can be configured on one server, that is, multiple domain names can be configured on one physical host. In Nginx, a server label is a virtual host, and multiple server labels are configured to virtualize multiple hosts. There are two ways to realize Nginx virtual host: domain name virtual mode and port virtual mode. The domain name virtual method means that different virtual machines use different domain names, and virtualize different hosts through different domain names; the port virtual method means that different virtual machines use the same domain name but different port numbers, and virtualize different hosts through different port numbers. the host. Port-based virtualization is not commonly used.
server_name
Indicates the address or domain name of the current nginx, the domain name needs to be purchased, or the host file is configured
location
It is equivalent to url routing. Nginx decides how to process a request according to the location configuration.
location
The specific syntax of location:
location [ = | ~ | ~* | ^~ ] uri { ... }
Focus on the ones in square brackets [ = | ~ | ~* | ^~ ]
, where |
the separated content indicates the syntax you may use, among them:
=
Indicates an exact match:
location = /test {
return 200 "hello";
}
For example:
/test ok
/test/ not ok
/test2 not ok
/test/2 not ok
~
Indicates a case-sensitive regular match:
location ~ ^/test$ {
[configuration]
}
For example:
/test ok
/Test not ok
/test/ not ok
/test2 not ok
~*
Indicates a case-insensitive regular match:
location ~* ^/test$ {
[configuration]
}
For example:
/test ok
/Test ok
/test/ not ok
/test2 not ok
^~
Indicates that the uri starts with a certain string:
location ^~ /images/ {
[configuration]
}
For example:
/images/1.gif ok
/
Indicates a generic match:
location / {
[configuration]
}
For example:
/index.html ok
location /test {
[configuration]
}
For example:
/test ok
/test2 ok
/test/ ok
The definition of Location is divided into two types:
prefix string
Regular expression (regular expression), specifically preceded by ~* and ~ modifiers
When there are multiple Locations, the matching order is:
Check the locations using the prefix string, select the longest match among the locations using the prefix string, and store the result;
If it matches a URI with the = modifier, stop matching immediately;
If it matches a URI with the ^~ modifier, it also stops matching immediately;
Then check the regular expression according to the order of the definition file, and stop when it matches;
When the regular expression cannot match, use the previously stored prefix string;
Summarize:
-
at
顺序上
:-
前缀字符串
The order is not important,按照匹配长度来确定
; -
正则表达式
then按照定义顺序
;
-
-
at
优先级上
:=
The modifier is the highest,^~
followed by the third,正则
and the last is前缀字符串
the match.
Let's give a few simple examples to illustrate
The request URI is as follows:
/document
Example one:
Configuration:
server {
location /doc {
[ configuration A ]
}
location /docu {
[ configuration B ]
}
}
Matching result:
configuration B
Note: Although /doc
can also match, but 在顺序上,前缀字符串顺序不重要,按照匹配长度来确定
.
Example two:
server {
location ~ ^/doc {
[ configuration A ]
}
location ~ ^/docu {
[ configuration B ]
}
}
Matching result:
configuration A
Example three:
server {
location ^~ /doc {
[ configuration A ]
}
location ~ ^/docu {
[ configuration B ]
}
}
Matching result:
configuration A
Note: Although ~ ^/docu
can also match, but ^~
the 优先级更高
.
Example four:
server {
location /document {
[ configuration A ]
}
location ~ ^/docu {
[ configuration B ]
}
}
Matching result:
configuration B
The difference between root and alias
When we set like this root
:
location /i/ {
root /data/w3;
}
When requested /i/top.gif
, /data/w3/i/top.gif
will be returned.
When we set like this alias
:
location /i/ {
alias /data/w3/images/;
}
When requested /i/top.gif
, /data/w3/images/top.gif
will be returned.
The difference between the two:
-
root
is a direct splicingroot + location
; -
alias
isalias
replaced bylocation
root in server and location
Root can be used in both server and location, for example:
server {
listen 80;
server_name 10.0.7.115;
root /data/app/;
location / {
root /data/web/;
index index.html;
}
}
如果两者都出现,是怎样的优先级呢?
To put it simply, 就近原则
if location
it can match in , use the configuration location
in root
, ignore the configuration server
in , and use the configuration root
in location
when it can’t match in .server
root
nginx welcome page
We can see from the default configuration file that Nginx listens to port 80 by default, and the default access to port 80 is for browser access without adding a port number, so when accessing http://ip
or , the welcome page http://ip:80
is returned ./html/index.html
Reference article for this article
Nginx's directory structure, basic operating principle and basic configuration file
Nginx Location configuration details
Nginx related articles
1 What concepts should be mastered when learning Nginx for beginners