Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/cn/docs/http/request_processing.xml
1 views
1
<!--
2
Copyright (C) Igor Sysoev
3
Copyright (C) Nginx, Inc.
4
-->
5
6
<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
7
8
<article name="Nginx如何处理一个请求"
9
link="/cn/docs/http/request_processing.html"
10
lang="cn"
11
rev="1"
12
translator="Jinglong &amp; cfsego"
13
author="Igor Sysoev"
14
editor="Brian Mercer">
15
16
17
<section name="基于名字的虚拟主机">
18
19
<para>
20
Nginx首先选定由哪一个<i>虚拟主机</i>来处理请求。让我们从一个简单的配置(其中全部3个虚拟主机都在端口*:80上监听)开始:
21
22
<programlisting>
23
server {
24
listen 80;
25
server_name example.org www.example.org;
26
...
27
}
28
29
server {
30
listen 80;
31
server_name example.net www.example.net;
32
...
33
}
34
35
server {
36
listen 80;
37
server_name example.com www.example.com;
38
...
39
}
40
</programlisting>
41
</para>
42
43
<para>
44
在这个配置中,nginx仅仅检查请求的<header>Host</header>头以决定该请求应由哪个虚拟主机来处理。如果Host头没有匹配任意一个虚拟主机,或者请求中根本没有包含Host头,那nginx会将请求分发到定义在此端口上的默认虚拟主机。在以上配置中,第一个被列出的虚拟主机即nginx的默认虚拟主机——这是nginx的默认行为。而且,可以显式地设置某个主机为默认虚拟主机,即在"<literal>listen</literal>"指令中设置"<literal>default_server</literal>"参数:
45
46
<programlisting>
47
server {
48
listen 80 <b>default_server</b>;
49
server_name example.net www.example.net;
50
...
51
}
52
</programlisting>
53
54
<note>
55
"<literal>default_server</literal>"参数从0.8.21版开始可用。在之前的版本中,应该使用"<literal>default</literal>"参数代替。
56
</note>
57
58
请注意"<literal>default_server</literal>"是监听端口的属性,而不是主机名的属性。后面会对此有更多介绍。
59
</para>
60
61
</section>
62
63
64
<section id="how_to_prevent_undefined_server_names"
65
name="如何防止处理未定义主机名的请求">
66
67
<para>
68
如果不允许请求中缺少<header>Host</header>头,可以定义如下主机,丢弃这些请求:
69
70
<programlisting>
71
server {
72
listen 80;
73
server_name "";
74
return 444;
75
}
76
</programlisting>
77
78
在这里,我们设置主机名为空字符串以匹配未定义<header>Host</header>头的请求,而且返回了一个nginx特有的,非http标准的返回码444,它可以用来关闭连接。
79
<note>从0.8.48版本开始,这已成为主机名的默认设置,所以可以省略<literal>server_name ""</literal>。而之前的版本使用机器的<i>hostname</i>作为主机名的默认值。</note>
80
</para>
81
82
</section>
83
84
85
<section id="mixed_name_ip_based_servers"
86
name="基于域名和IP混合的虚拟主机">
87
88
<para>
89
下面让我们来看一个复杂点的配置,在这个配置里,有几个虚拟主机在不同的地址上监听:
90
91
<programlisting>
92
server {
93
listen 192.168.1.1:80;
94
server_name example.org www.example.org;
95
...
96
}
97
98
server {
99
listen 192.168.1.1:80;
100
server_name example.net www.example.net;
101
...
102
}
103
104
server {
105
listen 192.168.1.2:80;
106
server_name example.com www.example.com;
107
...
108
}
109
</programlisting>
110
111
这个配置中,nginx首先测试请求的IP地址和端口是否匹配某个<link doc="ngx_http_core_module.xml" id="server"/>配置块中的<link doc="ngx_http_core_module.xml" id="listen"/>指令配置。接着nginx继续测试请求的Host头是否匹配这个<link doc="ngx_http_core_module.xml" id="server"/>块中的某个<link doc="ngx_http_core_module.xml" id="server_name"/>的值。如果主机名没有找到,nginx将把这个请求交给默认虚拟主机处理。例如,一个从192.168.1.1:80端口收到的访问<literal>www.example.com</literal>的请求将被监听192.168.1.1:80端口的默认虚拟主机处理,本例中就是第一个服务器,因为这个端口上没有定义名为<literal>www.example.com</literal>的虚拟主机。
112
</para>
113
114
<para>
115
默认服务器是监听端口的属性,所以不同的监听端口可以设置不同的默认服务器:
116
117
<programlisting>
118
server {
119
listen 192.168.1.1:80;
120
server_name example.org www.example.org;
121
...
122
}
123
124
server {
125
listen 192.168.1.1:80 <b>default_server</b>;
126
server_name example.net www.example.net;
127
...
128
}
129
130
server {
131
listen 192.168.1.2:80 <b>default_server</b>;
132
server_name example.com www.example.com;
133
...
134
}
135
</programlisting>
136
</para>
137
138
</section>
139
140
141
<section id="simple_php_site_configuration"
142
name="一个简单PHP站点配置">
143
144
<para>
145
现在我们来看在一个典型的,简单的PHP站点中,nginx怎样为一个请求选择<i>location</i>来处理:
146
147
<programlisting>
148
server {
149
listen 80;
150
server_name example.org www.example.org;
151
root /data/www;
152
153
location / {
154
index index.html index.php;
155
}
156
157
location ~* \.(gif|jpg|png)$ {
158
expires 30d;
159
}
160
161
location ~ \.php$ {
162
fastcgi_pass localhost:9000;
163
fastcgi_param SCRIPT_FILENAME
164
$document_root$fastcgi_script_name;
165
include fastcgi_params;
166
}
167
}
168
</programlisting>
169
</para>
170
171
<para>
172
首先,nginx使用前缀匹配找出最准确的location,这一步nginx会忽略location在配置文件出现的顺序。上面的配置中,唯一的前缀匹配location是"<literal>/</literal>",而且因为它可以匹配任意的请求,所以被作为最后一个选择。接着,nginx继续按照配置中的顺序依次匹配正则表达式的location,匹配到第一个正则表达式后停止搜索。匹配到的location将被使用。如果没有匹配到正则表达式的location,则使用刚刚找到的最准确的前缀匹配的location。
173
</para>
174
175
<para>
176
请注意所有location匹配测试只使用请求的URI部分,而不使用参数部分。这是因为写参数的方法很多,比如:
177
178
<programlisting>
179
/index.php?user=john&amp;page=1
180
/index.php?page=1&amp;user=john
181
</programlisting>
182
183
除此以外,任何人在请求串中都可以随意添加字符串:
184
185
<programlisting>
186
/index.php?page=1&amp;something+else&amp;user=john
187
</programlisting>
188
</para>
189
190
<para>
191
现在让我们来看使用上面的配置,请求是怎样被处理的:
192
193
<list type="bullet" compact="no">
194
195
<listitem>
196
请求"<literal>/logo.gif</literal>"首先匹配上location "<literal>/</literal>",然后匹配上正则表达式"<literal>\.(gif|jpg|png)$</literal>"。因此,它将被后者处理。根据"<literal>root /data/www</literal>"指令,nginx将请求映射到文件<path>/data/www/logo.gif</path>",并发送这个文件到客户端。
197
</listitem>
198
199
<listitem>
200
请求"<literal>/index.php</literal>"首先也匹配上location "<literal>/</literal>",然后匹配上正则表达式"<literal>\.(php)$</literal>"。 因此,它将被后者处理,进而被发送到监听在localhost:9000的FastCGI服务器。<link doc="ngx_http_fastcgi_module.xml" id="fastcgi_param"/>指令将FastCGI的参数<literal>SCRIPT_FILENAME</literal>的值设置为"<literal>/data/www/index.php</literal>",接着FastCGI服务器执行这个文件。变量<var>$document_root</var>等于<link doc="ngx_http_core_module.xml" id="root"/>指令设置的值,变量<var>$fastcgi_script_name</var>的值是请求的uri,"<literal>/index.php</literal>"。
201
</listitem>
202
203
<listitem>
204
请求"<literal>/about.html</literal>"仅能匹配上location "<literal>/</literal>",因此,它将使用此location进行处理。根据"<literal>root /data/www</literal>"指令,nginx将请求映射到文件"<path>/data/www/about.html</path>",并发送这个文件到客户端。
205
</listitem>
206
207
<listitem>
208
请求"<literal>/</literal>"的处理更为复杂。它仅能匹配上location "<literal>/</literal>",因此,它将使用此location进行处理。然后,<link doc="ngx_http_index_module.xml" id="index"/>指令使用它的参数和"<literal>root /data/www</literal>"指令所组成的文件路径来检测对应的文件是否存在。如果文件<path>/data/www/index.html</path>不存在,而<path>/data/www/index.php</path>存在,此指令将执行一次内部重定向到"<literal>/index.php</literal>",接着nginx将重新寻找匹配"<literal>/index.php</literal>"的location,就好像这次请求是从客户端发过来一样。正如我们之前看到的那样,这个重定向的请求最终交给FastCGI服务器来处理。
209
</listitem>
210
211
</list>
212
</para>
213
214
</section>
215
216
</article>
217
218