nginx特别篇2
- nginx
- 开启状态界面
- 状态页面监控与配置
- 配置zabbix页面
- rewrite
- if
- 基于浏览器实现分离案例
- 防盗链案例
nginx
开启状态界面
开启status:
location /status {
stub_status on;
allow 172.16.0.0/16;
deny all;
}
示例:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /status {
stub_status ;
allow 192.168.129.33;
}
#error_page 404 /404.html;
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
访问状态页面的方式:http://server_ip/status
注意
Reading:
Writing数值大的原因:对端处理能力低、网络慢
Waiting数值大的原因:说明他没干事
Waiting数值小的原因:说明他处于饱和状态
状态页面信息详解:
状态码 | 表示的意义 |
Active connections 2 | 当前所有处于打开状态的连接数 |
accepts | 总共处理了多少个连接 |
handled | 成功创建多少握手 |
requests | 总共处理了多少个请求 |
Reading | nginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数 |
Writing | nginx返回给客户端的Header信息数,表示请求已经接收完成, 且正处于处理请求或发送响应的过程中的连接数 |
Waiting | 开启keep-alive的情况下,这个值等于active - (reading + writing), 意思就是Nginx已处理完正在等候下一次请求指令的驻留连接 |
状态页面监控与配置
主机名 | ip | 服务 | 系统 |
localhost | 192.168.129.33 | nginx zabbix_agent | centos7 |
Server | 192.168.129.250 | zabbix_server | redhat8 |
准备工作
localhost 安装nginx、zabbix_agent服务
Server安装zabbix_server服务
详细步骤查看此文章配置
//修改agent配置文件/usr/local/etc/zabbix_agentd.conf
[root@localhost zabbix-5.4.4]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1 #取消注释并修改值为1
Server=192.168.129.250
ServerActive=192.168.129.250 #服务端IP
Hostname=NGINX
//启动服务
[root@localhost zabbix-5.4.4]# zabbix_agentd
[root@localhost zabbix-5.4.4]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:443 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//编写的脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# vim /scripts/check_status.sh
#!/bin/bash
if [ `curl -s http://192.168.129.33/status|awk 'NR==4 {print $6}' ` -ne 0 ]
then
echo "1"
else
echo "0"
fi
[root@localhost ~]# chmod +x /scripts/check_status.sh
[root@localhost ~]# chown -R zabbix.zabbix /scripts/check_status.sh
[root@localhost ~]# ll
总用量 4
-rwxr-xr-x. 1 zabbix zabbix 127 10月 28 22:30 check_status.s
//修改/usr/local/etc/zabbix_agentd.conf
测试脚本
[root@localhost ~]# ./scripts/check_status.sh
0
配置zabbix配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
写入以下:
UserParameter=check_keepalived[*],/scripts/check_status.sh $1 #取消331行的注释并添加内容
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
服务端测试
[root@Server ~]# zabbix_get -s 192.168.129.33 -k check_status #服务端测试
0
配置zabbix页面
创建主机
创建监控项
创建触发器
添加触发器
rewrite
语法:rewrite regex replacement flag;
,如:
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
此处的$1用于引用(.*.jpg)匹配到的内容,又如:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
如上例所示,replacement可以是某个路径,也可以是某个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 |
rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)
break示例1
break 本条规则匹配完成即终止,不再匹配后面的任何规则
[root@localhost ~]# /usr/local/nginx/html/
[root@localhost html]# mkdir imgs
[root@localhost html]# ls
50x.html imgs index.html test
[root@localhost html]# ls images/
1.gif 2.webp
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /images {
rewrite ^/images/(.*\.webp)$ /imgs/$1 break;
}
#error_page 404 /404.html;
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
浏览器访问测试
break示例2
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /images {
rewrite ^/images/(.*\.webp)$ https://www.linuxprobe.com/wp-content/uploads/2020/05/2653e3c945f3ca8b91108ccf35b8aa81.jpg-wh_651x-s_3754039934.jpg break;
}
#error_page 404 /404.html;
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
浏览器访问测试
redirect示例1
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir imgs
[root@localhost html]# ls
404.html 50x.html imgs index.html
[root@localhost imgs]# ls
1.gif 2.webp
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /images {
rewrite ^/images/(.*\.webp)$ http://images.baidu.com/ redirect;
}
#error_page 404 /404.html;
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
浏览器访问测试
last和break组合使用示例1
last 本条规则匹配完成后,继续向下匹配新的location URI规则
break 本条规则匹配完成即终止,不再匹配后面的任何规则
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir imgs
[root@localhost html]# ls
404.html 50x.html imgs index.html
[root@localhost imgs]# ls
1.gif 2.webp
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /images {
rewrite ^/images/(.*\.webp)$ /imgs/$1 last;
}
location /imgs {
rewrite ^/imgs/(.*\.webp)$ http://images.baidu.com/ last;
}
#error_page 404 /404.html;
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
浏览器访问
发现访问到第二条规则的页面
last和break组合使用示例2
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir imgs
[root@localhost html]# ls
404.html 50x.html imgs index.html
[root@localhost imgs]# ls
1.gif 2.webp
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /images {
rewrite ^/images/(.*\.webp)$ /imgs/$1 break;
}
location /imgs {
rewrite ^/imgs/(.*\.webp)$ http://images.baidu.com/ last;
}
#error_page 404 /404.html;
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
浏览器访问
发现访问到第一条规则的页面
redirect示例
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /images {
rewrite ^/images/(.*\.webp)$ /imgs/$1 redirect;
}
#error_page 404 /404.html;
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
浏览器访问 状态码变成302
permanent示例
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /images {
rewrite ^/images/(.*\.webp)$ /imgs/$1 permanent;
}
#error_page 404 /404.html;
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
浏览器访问 状态码变成301
nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:
标识符 | 意义 |
^ | 必须以^后的实体开头 |
$ | 必须以$前的实体结尾 |
. | 匹配任意字符 |
[] | 匹配指定字符集内的任意字符 |
[^] | 匹配任何不包括在指定字符集内的任意字符串 |
| | 匹配 | 之前或之后的实体 |
() | 分组,组成一组用于匹配的实体,通常会有 | 来协助 |
捕获子表达式,可以捕获放在()之间的任何文本,比如:
^(hello|sir)$ //字符串为“hi sir”捕获的结果:$1=hi$2=sir
//这些被捕获的数据,在后面就可以当变量一样使用了
if
语法:if (condition) {...}
应用场景:
- server段
- location段
常见的condition
- 变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
- 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
- 正则表达式的模式匹配操作
- ~:区分大小写的模式匹配检查
- ~*:不区分大小写的模式匹配检查
- !和!*:对上面两种测试取反
- 测试指定路径为文件的可能性(-f,!-f)
- 测试指定路径为目录的可能性(-d,!-d)
- 测试文件的存在性(-e,!-e)
- 检查文件是否有执行权限(-x,!-x)
基于浏览器实现分离案例
if ($http_user_agent ~ Firefox) {
rewrite ^(.*)$ /firefox/$1 break;
}
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_user_agent ~ Chrome) {
rewrite ^(.*)$ /chrome/$1 break;
}
防盗链案例
location ~* \.(jpg|gif|jpeg|png)$ {
valid_referers none blocked www.idfsoft.com;
if ($invalid_referer) {
rewrite ^/ http://www.idfsoft.com/403.html;
}
}