Nginx配置浅析

Nginx是Igor Sysoev用C语言编写的一个web服务器,一般用于负载均衡、反向代理和HTTP缓存等。Nginx用异步的事件驱动(event-driven)的方式来处理请求,所以负载能力很强。html

Nginx使用Block(如 server block, location block)来组成配置文件的层级结构,并在接收到客户端请求以后根据请求的域名(domain name)端口(port)IP地址判断处理该请求的server block,而后根据请求的资源URI决定处理该请求的location blockweb

Server Block

管理员能够定义多个server block做为不相关的虚拟web服务器实体,而后经过listenserver_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后能够指定:服务器

  1. IP:port的IP地址和端口
  2. 仅IP(端口将默认为80)
  3. 仅port,将监听全部接口的这个port
  4. 到某个Unix socket的路径(在服务器间转发请求的时候会用到)

在将listen的值与请求进行匹配以前,Nginx会先将listen的值中所缺省的部分补充完整。而后将优先匹配准确的IP,若是不存在彻底准确匹配的IP才会匹配到0.0.0.0,若是有多个IP:port匹配度相同,Nginx将会继续检查server_name负载均衡

server_name指令

Nginx将server_name与请求头中的Host进行匹配,匹配的顺序:dom

  1. 优先选择第一个精确匹配到的block。异步

    server {
        listen 80;
        server_name host.example.com;
        ...
    }
  2. 选择以*开头的进行匹配,并优先选择最长的。socket

    server {
        listen 80;
        server_name *.example.com;
        ...
    }
  3. 选择以*结尾的进行匹配,并优先选择最长的。oop

    server {
        listen 80;
        server_name www.example.*;
        ...
    }
  4. 选择以~开头的用正则表达式进行匹配,并优先选择第一个。

    server {
        listen 80;
        server_name ~^(www|host).*\.example\.com$;
        ...
    }
  1. 若是以上规则都没法匹配,则选择default_server定义的默认的server_block(每一个server_block只能有一个default_server),默认的default_serverlocalhost

    server {
        listen 80 default_server;
        server_name _;
        ...
    }

Location Block

location blockserver block的一部分,决定了如何处理请求的URI,格式:

location [modifier] location_match {
    ...
}

modifier

modifier是一个可选的参数,决定了如何解析后面的location matchmodifier可选的值有:

  1. (none)

    前缀匹配, 如

    location /site {
        ...
    }

    将匹配以/site开头的URI

  2. =(equal sign)

    完整匹配,如

    location = /page {
        ...
    }

    将匹配/page,而不会响应/page/index.html的请求

  3. ~(tilde)

    大小写敏感的正则匹配, 如

    location ~ \.(jpe?g|png|gif|ico)$ {
    ...
    }

    将匹配以.jpg/.jpeg/.png/.gif/.ico结尾的URI, 但不会响应.JPG

  4. ~*(tilde + asterisk)

    大小写无关的正则匹配, 如

    location ~* \.(jpe?g|png|gif|ico)$ {
        ...
    }

    .jpg.JPG都会匹配

  5. ^~(carat + tilde)

    非正则匹配,如

    location ^~ /page {
        ...
    }

    可以匹配/page/index.html

匹配顺序

Nginx优先选择正则表达式进行匹配,可是使用=^~这两个modifier能够覆盖这一特性。排序对匹配过程也有必定的影响,由于Nginx在匹配到最长最精确的location以后就会中止匹配。

  1. 将全部非正则表达式的location_match与请求的URI进行对比。
  2. modifier=的进行完整匹配。
  3. 选择最长location_match前缀进行匹配,若是modifier^~则匹配成功。
  4. 进行正则表达式匹配
  5. 用其余前缀匹配

其余指令

  1. index

    语法:index file ...; 默认为index index.html;

    index指令指定了被做为index的文件,好比上面的index.html

    可是在下面这种状况下,对/index.html的请求将会被第二个location block处理,由于第一个与/index.html并非彻底匹配。

    location = / {
        index index.html;
    }
    
    location / {
        ...
    }
  2. 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

  3. rewrite

    经过Perl兼容的正则表达式改变请求的URI,语法:rewrite regex replacement [flag];

    flag的值能够是:

    • last

    结束当前的rewrite指令,并用修改过的URI去匹配其余的location block

    • break

    结束当前的rewrite指令。

    • redirect

    当替换的URI(replacement)不以 “http://”, “https://”, “$scheme”开头时进行状态码为302的暂时性的重定向。

    • permanent

    返回一个状态码为301的永久重定向。

  4. 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

相关文章
相关标签/搜索