状态界面配置
语法和位置:

开启status:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /status {
stub_status;
}
......
[root@nginx ~]# nginx -s reload

此状态是nginx服务运行的状态,不应该所有人都可以访问,我们可以添加allow和deny
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /status{
allow 192.168.218.1; //允许特定的可以访问
deny all;
stub_status;
}
......
[root@nginx ~]# nginx -s reload
状态页面信息详解:
| 状态码 | 表示的意义 |
|---|---|
| Active connections 2 | 当前所有处于打开状态的连接数 |
| accepts | 总共处理了多少个连接 |
| handled | 成功创建多少握手 |
| requests | 总共处理了多少个请求 |
| Reading | nginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数 |
| Writing | nginx返回给客户端的Header信息数,表示请求已经接收完成,且正处于处理请求或发送响应的过程中的连接数 |
| Waiting | 开启keep-alive的情况下,这个值等于active - (reading + writing),意思就是Nginx已处理完正在等候下一次请求指令的驻留连接 |
如果Reading数值比较大,说明网站处理能力不足
如果Writing数值比较大,说明对端接受能力不足,接受比较慢
Waiting数值不能太大,小一点比较好
rewrite(URL重定向)
语法:rewrite regex replacement flag; 如:
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
//在目录images下所有以.jpg结尾的所有文件,$1就是前面匹配的.*\.jpg
举例演示rewrite:
[root@nginx html]# pwd
/usr/local/nginx/html
[root@nginx html]# mkdir images
[root@nginx html]# cd images/
[root@nginx images]# ls
1.jpg //上传一张图片

修改目录images名字
[root@nginx html]# pwd
/usr/local/nginx/html
[root@nginx html]# mv images imgs
再次访问,测试是否可以访问,此时是访问不到的

配置文件添加rewrite
//添加一个location,在location内添加rewrite
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /images {
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
}
......
[root@nginx ~]# nginx -s reload
网页访问,此时就可以再次访问了

使用新的URI也可以访问

语法中的replacement可以是某个路径,也可以是某个URL
以下举例演示:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /images {
rewrite ^/images/(.*\.jpg)$ http://image.baidu.com break; //替换为一个URL
}
......
[root@nginx ~]# nginx -s reload
网页访问,会自动跳转到配置文件中所写的URL


常见的flag
| flag | 作用 |
|---|---|
| last | 基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个,一旦此rewrite规则重写成后,就不再被后面其它的rewrite规则进行处理 而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程 |
| break | 中止Rewrite,不再继续匹配一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求,且不再会被当前location内的任何rewrite规则所检查 |
| redirect | 以临时重定向的HTTP状态302返回新的URL |
| permanent | 以永久重定向的HTTP状态301返回新的URL |
- last
//编写两个rewrite
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /images {
rewrite ^/images/(.*\.jpg)$ /imgs/$1 last;
}
location /imgs {
rewrite ^/imgs/(.*\.jpg)$ http://image.baidu.com last;
}
......
[root@nginx ~]# nginx -s reload
此时访问获取的界面应该是http://image.baidu.com的界面


- break
//break和last组合
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /images {
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
}
location /imgs {
rewrite ^/imgs/(.*\.jpg)$ http://image.baidu.com last;
}
......
[root@nginx ~]# nginx -s reload
此时访问获取的界面应该是http://192.68.218.132/imgs/1.jpg的界面

- rewrite错误配置
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /images {
rewrite ^/images/(.*\.jpg)$ /imgs/$1 last;
}
location /imgs {
rewrite ^/imgs/(.*\.jpg)$ /images/$1 last; //注意此处的replacement
}
......
[root@nginx ~]# nginx -s reload
此时访问网站就像是两个location在互相踢皮球,导致网页访问不到

nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:
| 标识符 | 意义 |
|---|---|
| ^ | 必须以^后的实体开头 |
| $ | 必须以$前的实体结尾 |
| . | 匹配任意字符 |
| [ ] | 匹配指定字符集内的任意字符 |
| [^] | 匹配任何不包括在指定字符集内的任意字符串 |
| l | 匹配 之前或之后的实体 |
| () | 分组,组成一组用于匹配的实体,通常会有 |
捕获子表达式,可以捕获放在()之间的任何文本,比如:
^(hello|sir)$ //字符串为“hi sir”捕获的结果:$1=hi$2=sir
//这些被捕获的数据,在后面就可以当变量一样使用了
if
语法和位置:

常见的condition:
- 变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
- 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
- 正则表达式的模式匹配操作
- ~:区分大小写的模式匹配检查
- ~*:不区分大小写的模式匹配检查
- !和!*:对上面两种测试取反
- 测试指定路径为文件的可能性(-f,!-f)
- 测试指定路径为目录的可能性(-d,!-d)
- 测试文件的存在性(-e,!-e)
- 检查文件是否有执行权限(-x,!-x)
本文详细介绍了如何配置Nginx的状态界面,以限制访问权限并展示服务运行状态。同时,通过实例讲解了Nginx的URL重写规则,包括last、break等flag的使用,以及常见错误配置的分析。通过理解这些概念,读者可以更好地管理和优化Nginx服务器。
150

被折叠的 条评论
为什么被折叠?



