Nginx是Igor Sysoev用C语言编写的一个web服务器,一般用于负载均衡、反向代理和HTTP缓存等。Nginx用异步的事件驱动(event-driven)的方式来处理请求,所以负载能力很强。html
Nginx使用Block(如 server block
, location block
)来组成配置文件的层级结构,并在接收到客户端请求以后根据请求的域名(domain name),端口(port),IP地址判断处理该请求的server block
,而后根据请求的资源和URI决定处理该请求的location block
web
管理员能够定义多个server block
做为不相关的虚拟web服务器实体,而后经过listen
和server_name
决定处理请求的server block
正则表达式
listen
指令Nginx首先会检查请求的IP地址和端口,并根据全部server block
创建一个列表来处理请求。每一个server block
中的listen
定义了这个server block
能处理的IP和端口(root用户运行默认为0.0.0.0:80,非root用户运行的为0.0.0.0:8080)缓存
listen
后能够指定:服务器
IP:port
的IP地址和端口Unix socket
的路径(在服务器间转发请求的时候会用到)在将listen
的值与请求进行匹配以前,Nginx会先将listen
的值中所缺省的部分补充完整。而后将优先匹配准确的IP,若是不存在彻底准确匹配的IP才会匹配到0.0.0.0
,若是有多个IP:port
匹配度相同,Nginx将会继续检查server_name
负载均衡
server_name
指令Nginx将server_name
与请求头中的Host
进行匹配,匹配的顺序:dom
优先选择第一个精确匹配到的block。异步
server { listen 80; server_name host.example.com; ... }
选择以*开头的进行匹配,并优先选择最长的。socket
server { listen 80; server_name *.example.com; ... }
选择以*结尾的进行匹配,并优先选择最长的。oop
server { listen 80; server_name www.example.*; ... }
选择以~开头的用正则表达式进行匹配,并优先选择第一个。
server { listen 80; server_name ~^(www|host).*\.example\.com$; ... }
若是以上规则都没法匹配,则选择default_server
定义的默认的server_block
(每一个server_block
只能有一个default_server
),默认的default_server
是localhost
server { listen 80 default_server; server_name _; ... }
location block
是server block
的一部分,决定了如何处理请求的URI,格式:
location [modifier] location_match { ... }
modifier
是一个可选的参数,决定了如何解析后面的location match
,modifier
可选的值有:
(none)
前缀匹配, 如
location /site { ... }
将匹配以/site
开头的URI
=(equal sign)
完整匹配,如
location = /page { ... }
将匹配/page
,而不会响应/page/index.html
的请求
~(tilde)
大小写敏感的正则匹配, 如
location ~ \.(jpe?g|png|gif|ico)$ { ... }
将匹配以.jpg/.jpeg/.png/.gif/.ico
结尾的URI, 但不会响应.JPG
~*(tilde + asterisk)
大小写无关的正则匹配, 如
location ~* \.(jpe?g|png|gif|ico)$ { ... }
.jpg
和.JPG
都会匹配
^~(carat + tilde)
非正则匹配,如
location ^~ /page { ... }
可以匹配/page/index.html
Nginx优先选择正则表达式进行匹配,可是使用=
和^~
这两个modifier
能够覆盖这一特性。排序对匹配过程也有必定的影响,由于Nginx在匹配到最长最精确的location以后就会中止匹配。
location_match
与请求的URI进行对比。modifier
为=
的进行完整匹配。location_match
前缀进行匹配,若是modifier
为^~
则匹配成功。index
语法:index file ...;
默认为index index.html;
index
指令指定了被做为index的文件,好比上面的index.html
可是在下面这种状况下,对/index.html
的请求将会被第二个location block
处理,由于第一个与/index.html
并非彻底匹配。
location = / { index index.html; } location / { ... }
try_files
root /var/www/main; location / { try_files $uri $uri.html $uri/ /fallback/index.html; } location /fallback { root /var/www/another; }
对/page
的请求将会首先进入第一个location, 而后尝试在/var/www/main
下依次查找page
, page.html
, page/
,若是都没有找到的话将会被重定向到/fallback/index.html
,并由第二个location提供/var/www/another/fallback/index.html
rewrite
经过Perl兼容的正则表达式改变请求的URI,语法:rewrite regex replacement [flag];
flag的值能够是:
结束当前的rewrite指令,并用修改过的URI去匹配其余的location block
。
结束当前的rewrite指令。
当替换的URI(replacement
)不以 “http://”
, “https://”
, “$scheme”
开头时进行状态码为302的暂时性的重定向。
返回一个状态码为301的永久重定向。
error_page
root /var/www/main; location / { error_page 404 /another/whoops.html; } location /another { root /var/www; }
除了/another
以外的请求都会在/var/www/main
查找请求的资源,若是没有找到相关资源将会重定向到/another/whoops.html
,由第二个location block
处理,查找/var/www/another/whoops.html