栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Nginx-nginx配置文件详解<三>

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nginx-nginx配置文件详解<三>

状态界面配置

官方文档查看状态界面配置方法点击此处

语法和位置:

开启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总共处理了多少个请求
Readingnginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数
Writingnginx返回给客户端的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

官方文档查看if配置方法点击此处

语法和位置:

常见的condition:

  • 变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
  • 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
  • 正则表达式的模式匹配操作
    • ~:区分大小写的模式匹配检查
    • ~*:不区分大小写的模式匹配检查
    • !和!*:对上面两种测试取反
  • 测试指定路径为文件的可能性(-f,!-f)
  • 测试指定路径为目录的可能性(-d,!-d)
  • 测试文件的存在性(-e,!-e)
  • 检查文件是否有执行权限(-x,!-x)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/388947.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号