目录
- Nginx的相关模块功能
- 状态统计模块
- 认证模块
- 访问控制模块
- 下载模块
- 核心模块的location
- 重定向模块
- 负载均衡模块
Nginx的相关模块功能
这篇文章记录在学习过程中了解到的模块,相比Nginx大堆模块,肯定是不全的,有兴趣可以直接查看官方文档。
状态统计模块
状态统计模块是ngx_http_stub_status_module,默认是没有安装的,使用--with-http_stub_status_module
在执行./configure时指定即可。
在http块里添加一个location,然后使用stub_status;
这条指令,就可以在浏览器看到连接的状态了。
location /status{
stub_status;
}
Active connections:当前活动客户端连接数,包括等待连接数。
accepts:接受的客户端连接总数。
handled:已处理连接的总数。 通常与accepts相同,除非已达到某些资源限制(例如,worker连接的限制)。
requests: 客户端请求的总数。
Reading: nginx读取请求头的当前连接数。
Writing:当前连接数nginx将响应写回客户端的地方。
Waiting:等待请求的当前空闲客户端连接数。
认证模块
ngx_http_auth_basic_module模块允许通过验证用户名和密码来限制对资源的访问,使用“HTTP 基本身份验证”协议。
根据文档说明,需要使用htpasswd命令,所以先安装一下yum install httpd-tools -y
工具包,然后在之前的/status添加如下。
location /status{
stub_status;
auth_basic "status author";
auth_basic_user_file htpasswd;
}
新建用户shengxia和密码123456,可以看到会出现用户名和加密的密码,如下所示。
[root@www conf]# htpasswd -c /usr/local/shengxia/conf/htpasswd shengxia
New password:
Re-type new password:
Adding password for user shengxia
[root@www conf]# cat htpasswd
shengxia:$apr1$6tARyB7L$AZ/Rq5D63aJKEVlEn5LI.0
访问控制模块
ngx_http_access_module模块允许限制对某些客户端地址的访问。
location /status {
deny 192.168.1.1;#禁止访问
allow 192.168.1.0/24;#允许访问
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
下载模块
download目录。
location / {
autoindex on;
}
[root@www html]# mkdir download
[root@www html]# cd download/
[root@www download]# cp /etc/passwd .
[root@www download]# tar zcvf passwd.tar.gz passwd
passwd
[root@www download]# ls
passwd passwd.tar.gz
[root@www download]# echo 123456 >test.txt
[root@www download]# ls
passwd passwd.tar.gz test.txt
核心模块的location
ngx_http_core_module的location是根据URI请求匹配网页的URL,不同的路由匹配也会存在优先级。
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
普通匹配
- = : 这代表精准匹配全路径, 命中它后直接返回, 不再进行后续匹配, 优先级最高.
- ^~ : 这代表精准匹配开头, 命中开头后直接返回, 不再进行后续匹配, 优先级第二.
- 无匹配方式符号 : 这代表通用性匹配, 命中后还会继续后续匹配, 最后选取路径最长的匹配, 并储存起来, 优先级第四.
正则匹配
- ~: 这是区分大小写的正则匹配, 命中后则不进行后续匹配, 立即返回, 优先级第三.
- ~*: 不区分大小写的正则匹配, 命中后则不进行后续匹配, 立即返回, 优先级第三.
“/” 请求会匹配configuration A
“/index.html” 请求会匹配configuration B
“/documents/document.html” 请求会匹配configuration C
“/images/1.gif”会匹配configuration D
“/documents/1.jpg”请求会匹配configuration E。
重定向模块
ngx_http_rewrite_module模块可以使用正则表达式改变请求的URI,返回重定向。
return
server{
……
return code [text];#返回状态码和响应的文本内容
return code URL;#返回状态码和URL
return URL;#返回指定的URL,状态码默认是302
}
rewrite
- last:执行完当前重写规则跳转到新的 URI 后继续执行后续操作。
- break:执行完当前重写规则跳转到新的 URI 后不再执行后续操作。
- redirect:返回响应状态码 302 的临时重定向,返回内容是重定向 URI 的内容。
- permanent:返回响应状态码 301 的永久重定向,返回内容是重定向 URI 的内容。
server {
...
rewrite ^(/download/.*) /test/abc last;
rewrite ^/a https://www.baidu.com break;
rewrite ^/(.*) https://www.baidu.com redirect;
rewrite ^/(.*) https://www.baidu.com permanet;
...
}
负载均衡模块
ngx_http_upstream_module可以使用nginx作为非常有效的HTTP负载均衡器来将流量分配到多个应用程序服务器并改进使用nginx的Web应用程序的性能、可扩展性和可靠性。
负载均衡算法
轮询 (round-robin): 分发对服务器的请求以循环的方式。
最少连接(least-connected):下一个请求分配给最少的活动连接数的服务器 。
ip-hash:哈希函数用于确定哪个服务器应该为下一个请求选择(基于客户端的 IP 地址)。
http {
upstream myapp1 {#定义负载均衡器
server srv1.example.com;#也可以是IP地址192.168.10.1
server srv2.example.com weight=3;#加权负载均衡,得到的访问量会加大
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;#用户访问网站时,转移到后端的服务器
}
}
}
效果