Nginx: 配置项之root和alias的区别, 深入理解location, 以及stub_status模块的用法

root和alias的区别

  • root 和 alias 两者都是来指定我们的URI和我们磁盘文件上的一个具体静态资源文件的一个映射关系

语法结构

1 ) root

  • 语法:root path;
  • 上下文:http server location if

2 ) alias

  • 语法:alias path;
  • 上下文:location

共同点和区别

  • 相同点:URI到磁盘文件的映射
  • 区别:root会将定义路径与URI叠加;alias 则只取定义路径

区别示例

1 )root

location /picture {
	root /opt/nginx/html/picture;
}
  • 客户端请求 www.test.com/picture/1.jpg, 则对应磁盘映射
  • 路径 /opt/nginx/html/picture/picture/1.jpg

2 )alias

location /picture {
	alias /opt/nginx/html/picture;
}
  • 客户端请求 www.test.com/picture/1.jpg, 则对应磁盘映射
  • 路径 /opt/nginx/html/picture/1.jpg

5 ) 注意事项

  • 使用 alias 时,末尾一定加 /
  • alias 只能位于 location 块中

location

1 ) 基础用法

  • 语法:location [ = | ~ | ~* | ^~ ] uri { ... }
  • 上下文: server location
匹配规则含义示例
=精确匹配location = /images/ { … }
~正则匹配,区分大小写location ~ \.(jpg|gif)$ { … }
~*正则匹配,不区分大小写location ~* \.(jpg|gif)$ { … }
∧~匹配到即停止搜索location ^~ /images/ { … }
不带任何符号location / { … }
  • 上述,~* 用处不大,linux 默认区分大小写,与 nginx版本也有关
  • 上述,∧~ 中,如果 遇到 /images/xx?id=1 这种也会匹配

示例

http {
	server {
		listen 80;
		server_name  www.nginx-test.com;
		location = /match_all/ {
			root html;
			index index.html;
		}
		location ~ \.(jpegljpg) $ {
			root html/images;
		}
		location ^~ /bbs/ {
			root html;
			index index.html index.htm;
		}
	}
}
  • $ mkdir { match_all, bbs }

  • $ touch match_all/index.html

    match_all page
    
  • $ touch match_all/match.html

    match page
    
  • $ touch bbs/index.html

    bbs page
    
  • 配置 hosts

    192.168.1.23      www.nginx-test.com
    
  • 访问:http://www.nginx-test.com/match_all 显示 match_all page

  • 访问:http://www.nginx-test.com/match_all/match.html 显示 match page

  • 访问:http://www.nginx-test.com/1.jpg 显示 1.jpg

  • 访问:http://www.nginx-test.com/bbs 显示 bbs page

2 )指令中匹配规则的优先级

  • 优先级排序如下

    • = > ^~ > ~ > ~* > 不带任何字符
  • 官方示例

    location = / {
    	[ configuration A ]
    }
    
    location / {
    	[ configuration B ]
    }
    
    location /documents/ {
    	[ configuration C ]
    }
    
    location ^~ /images/ {
    	[ configuration D ]
    }
    
    location ~* \.(gif|jpg|jpeg)$ {
    	[ configuration E ]
    }
    
    • 请求 / 则会匹配 A,B 但 A 优先级高,最终选择A
    • 请求 /index.html 会优先匹配 B, 因为A是精确匹配 (A, B均可能匹配)
    • 请求 /documents/document.html 会优先匹配C, 因为B比较模糊 (B, C均可能匹配)
    • 请求 /images/1.gif 会优先匹配 D, 因为优先级 ^~ > ~* (D,E均可能匹配)
    • 请求 /documents/1.jpg 只匹配 E
  • 特别注意,以下不能重复出现,否则报错

    location /test/ {
    	[ configuration A ]
    }
    
    location ^~ /test/ {
    	[ configuration B ]
    }
    

3 ) 理解 location 中的 URL 结尾的反斜线

url 写法的区别

3.1 不带 /

location /test {
	...
}
  • 首先,还是将 /test 作为一个目录来进行处理,寻找相关test目录并且下面是否有index.html
  • 如果找不到,则会尝试查找 test 文件,存在,则直接返回给浏览器

3.2 带 /

location /test/ {
	...
}
  • 这种会直接将 test 作为目录,并寻找 index.html
  • 如果找不到,仍不会将 test 作为一个文件,而是直接返回 404

stub_status 模块

  • 它是一个能够给 nginx 提供监控页面的模块,可以实现在页面中实时查看 nginx 服务运行的状态
  • 比如说当前正在处理用户的一个请求,连接数量,已经处理的所有客户端的请求数量
  • 包括正在接受的还有正在处理的,处于响应状态等等各种状态的监控的一个web页面

1 )语法结构

  • 指令: stub_status; 如果低于 1.7.5 版本: stub_status on;
  • 上下文: server location

2 ) 配置示例

# 备注,这个 uri 尽可能 长一些, 复杂一些,防止用户无意间访问到,引发安全问题
location /uri {
	stub_status;
}

3 )效果示例

Active connections: 2
server accepts handled requests
  883 883 928
 Reading: 0 Writing: 1 Waiting: 1
  • 上面 883 883 928 分别表示

    • 已接受的用户连接数
    • 正在处理的用户连接数
    • 处理完的所有的客户端请求数
  • 状态项,如下:

    状态项含义
    Active Connections活跃的连接数量
    accepts接受的客户端连接总数量
    handled处理的客户端连接总数量
    requests客户端总的请求数量
    Reading读取客户端的连接数
    Writing响应数据到客户端的连接数
    Waiting空闲客户端请求连接数量
  • 内嵌变量

    变量名含义
    $connections_active同Active connections值
    $connections_reading同Reading值
    $connections_writing同Writing值
    $connections_waiting同Waiting值

4 )配置示例

  • 配置前明确自己 nginx 的版本是否低于 1.7.5, 这个模块默认不会编译到 nginx 中的
  • nginx想要去支持, 编译的时候,必须要加上 --with-http_stub_status_module
  • 可以使用 $ opt/nginx/sbin/nginx -V 检验是否含有上述参数
    • 可以显示当前版本的nginx编译了哪些模块
  • nginx 本身是一个模块化的设计,在 configure 编译的时候,有很多这样的指令
  • 可以通过 $ ./configure --help (注意,在有这个二进制的目录下执行) 查看类似指令

现在开始配置

server {
	location /monitor_status {
		stub_status;
	}
}
  • $ nginx -t
  • $ nginx -s reload
  • 访问 http://www.nginx-test.com/monitor_status
  • 就会出现内容,展示如下
    Active connections: 1
    server accepts handled requests
      44 44 51
     Reading: 0 Writing: 1 Waiting: 0
    
  • 当刷新页面,数据就会变化,响应速度很快
  • 24
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值