精通Nginx应用篇之配置与Location详解

这一章我们来看看Nginx配置相关东东

一、虚拟主机详解

Nginx配置文件一般有如下几个配置段:

全局段:

#user  nobody;
worker_processes  1;
pid        /var/run/nginx.pid;

事件段:

events {
    worker_connections  1024;
}

协议段:

http {
    server {
        listen 80;
        server_name localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

    server {
        listen 8080;
        server_name localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

全局段相关配置说明:

user  nginx nginx;
#指定运行worker进程的用户和组

worker_processes  4;
#指定nginx运行的进程数,一般设置为(CPU数 * 单CPU核心数 = CPU总核心数),设置过多会抢占CPU,影响效率

pid /var/run/nginx.pid;
#指定nginx运行时的进程文件(我们可以通过此文件获取nginx运行的主进程号,而不用ps)

error_log  logs/error.log  info;
#指定nginx全局错误日志位置和类型,类型有:[ debug | info | notice | warn | error | crit ],可以省略类型

worker_rlimit_nofile 65535;
#单个nginx进程打开的文件描述符数目的最大值,理论值为:( (系统的值ulimit -n) 除以 nginx进程数 ),由于nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致即可。

事件段相关配置说明:

events {
    use epoll;
    #参考事件模型,有如下模型:[ kqueue | rtsig | epoll | /dev/poll | select | poll ], epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。

    worker_connections  1024;
    #单个进程连接数
}

协议段相关配置说明:

注:Nginx可用作http、 mail等服务器,这里我们只对http服务器进行讲解,其他相关代理服务请百度

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;
    #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。

    tcp_nopush     on;
    #防止网络阻塞

    keepalive_timeout  65;
    #长连接超时时间(秒)

    gzip  on;
    #gzip压缩输出

    #虚拟主机配置区
    server {
        listen       80;
        #监听端口

        server_name  localhost;
        #域名或ip

        charset koi8-r;
        #字符编码

        access_log  logs/host.access.log  main;
        #访问日志针对当前虚拟主机有效

        #请求定位
        location / {
            root   html;
            #根目录

            index  index.html index.htm;
            #索引文件名,找不到文件时默认找index.html
        }
    }
}

至此,Nginx虚拟主机的相关配置基本如此,如果具体到模块需要看其应用的上下文来确定其可以配置到哪个段里。更加详细的配置请上nginx官网查阅。

二、Location详解
location有定位的意思,是根据uri来定位到真实目录中【或者可以说是定位到服务提供方,比如php这类就是如此】

location共有3种匹配uri的方式:

  1. 精确匹配 语法:location = patt { }
  2. 普通匹配 语法:location patt { }
  3. 正则匹配 语法:location ~[~*] patt { }

具体语法location语法:

1.精确匹配:

location = / {
    root    /webdata/html;
    index   index.html index.htm;
}

从以上示例来看,精确匹配是来匹配 / 的。意思就是说只能匹配到 http://127.0.0.1/ ,其他所有的不符合此形式的uri都不能匹配到。

location = /abc.html {
    root    /webdata/html;
    index   index.html index.htm;
}

这样的匹配形式就只能匹配到 http://127.0.0.1/abc.html ,其他所有的形式都匹配不到。匹配到了之后怎么办呢?然后就定位到root目录下咯,接着就去找abc.html这个文件。找到了就200,找不到就404,然后结束整个location。

那我们再来推断一下上面一个精确匹配的结果?当我们地址栏输入 http://127.0.0.1/ 后,location = / 匹配到了,怎么办呢?我http是要访问文件的呀?这个时候发现location里面有一个index索引。那好就去找这个东东,然后就定位到了index.html文件咯。这个时候nginx内部重定向到了 http://127.0.0.1/index.html 。重定向后发现没有任何的location能够匹配。那好,nginx就定位到自己的html文件夹【这个html文件夹是nginx安装时自带的虚拟根目录】中的index.html。所以就会出现Welcome to nginx! 。

2.普通匹配:

location / {
    root     /webdata/html;
    index   index.html index.html;
}

普通匹配意思也就是尽量匹配。多个普通匹配在同一个server里的话,匹配最多的那个生效。普通匹配相对来说要简单得多,匹配到了然后就直接去找。

3.正则匹配:

区分大小写的匹配

location ~ /(\d{1,10})\.html {
    root    /webdata/html;
    index   index.html index.htm;
}

不区分大小写的匹配

location ~* /(\d{1,10})\.html {
    root    /webdata/html;
    index   index.html index.htm;
}

正则匹配和普通匹配类似,只要匹配到了就结束。

下面我们来看一下location三种匹配方式的先后顺序和匹配规则:

其实正确的匹配顺序是:
精确匹配 -> 普通匹配 -> 正则匹配 为什么很多时候我们得到的结果却不是这样的呢?

先看一个示例 (注意他们的root根目录):

#精确匹配
location = / {
    root    /webdata;
    index   default.html default.htm;
}

#普通匹配
location / {
    root    /webdata/html;
    index   index.html index.htm;
}

我们来分析一下:
当我们在地址栏输入 http://127.0.0.1/ ,nginx首先进行精确匹配,发现精确匹配成功,然后定位到index索引的default.html文件,nginx内部重定向到 http://127.0.0.1/default.html 。这个时候精确匹配不再生效,普通匹配反而生效了,然后就定位到root目录下的default.html文件。最终结果是定位到了 /webdata/html/default.html 。请注意这里的default.html其实是精确匹配中的index。

再来看一个示例(注意他们的root根目录):

#普通匹配
location /default.html {
    root    /webdata/html;
    index   default.html default.htm;
}

#精确匹配
location = /default.html {
    root    /webdata;
    index   default.html default.htm;
}

我们在地址栏输入 http://127.0.0.1/default.html ,单从配置上看可以看到精确匹配和普通匹配都能匹配到。生效的却是 /webdata/default.html ,由此证明优先匹配的是精确匹配。这里精确匹配和普通匹配在配置【配置代码】层面并无先后顺序之分,同样只会优先匹配精确匹配。

注意:精确匹配之间是有先后顺序之分,而普通匹配和正则匹配并无先后顺序之分。

下面再来分析一下普通匹配与正则匹配之间的关系。

#普通匹配
location /default.html {
    root    /webdata;
    index   index.html index.htm;
}

#正则匹配
location ~ /default.html{
    root    /webdata/html;
    index   index.html index.htm;
}

我们在地址栏输入 http://127.0.0.1/default.html ,location优先匹配普通匹配,发现命中后不会立即返回,继续进行正则匹配,发现命中后,返回正则命中结果。最终返回的应该是 /webdata/html/default.html 。如果发现正则匹配没有命中则返回普通匹配的最优结果(所谓最优结果就是匹配得最长的那个结果)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值