Nginx模块

nginx版本为:nginx/1.18.0
全局配置文件:/etc/nginx/nginx.conf
虚拟主机配置:/etc/nginx/conf.d/*.conf

1.搭建虚拟主机

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# ls
default.conf
[root@localhost conf.d]# mv default.conf default.conf.bak
[root@localhost ~]# vim /etc/nginx/conf.d/www.conf

server {
        listen 80;
        server_name www.qzt.com;  #域名
location / {
        root /www; #根目录
        index index.html index.htm; #支持格式

 }      

}

[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx

在根目录添加些内容然后访问测试

[root@localhost ~]# mkdir /www
[root@localhost ~]# cd /www/
[root@localhost www]# echo  "1.61"  > index.html 
[root@localhost www]# cat index.html 
1.61

测试
在这里插入图片描述

2.autoindex自动索引模块
模块处理以斜杠字符(' /')结尾的请求,并生成目录列表。(nginx默认不起用目录索引)

[root@localhost download]# vim /etc/nginx/conf.d/www.conf
server{
   ............
location /download {
        root /www;
        autoindex on;  #启用索引
        charset utf-8,gdk; #字符编码为中文
        autoindex_exact_size on; #显示文件大小
        autoindex_localtime on;  #显示文件创建时间
}

[root@localhost ~]# cd /www/
[root@localhost www]# mkdir download
[root@localhost www]# cd download/
[root@localhost download]# touch 1 2 3 4 #随便放点文件方便测试
[root@localhost download]# !s
systemctl reload nginx

Syntax: autoindex on | off;索引功能的开或关
Default: autoindex off; 默认关闭
Context: http, server, location 场景:全局、某个虚拟主机、某个虚拟主机的目录

测试
访问:http://192.168.1.61/download/
在这里插入图片描述3.status模块,nginx状态监控
该ngx_http_stub_status_module模块提供对基本状态信息的访问。

[root@localhost ~]# vim /etc/nginx/conf.d/www.conf 

server {
      	.......
location /status {
        stub_status;  #启用状态化追踪
        access_log off; #关闭status的日志记录
        
[root@localhost ~]# !s
systemctl reload nginx

Syntax: stub_status; 启用状态化追踪
Default: — 默认关闭
Context 场景: server, location

测试
访问:http://192.168.1.61/status
在这里插入图片描述

客户端显示结果如下:
Active connections: 当前活跃的连接数
server accepts : 当前的总tcp连接数
handled : 成功的连接数
requests : 总HTTP请求数

4.access模块,nginx基于ip的访问控制
例子:仅允许内部网段或vpn访问status

[root@localhost ~]# vim /etc/nginx/conf.d/www.conf 

location /status {  #针对status生效
        stub_status;
        access_log off;
        allow 192.168.1.0/24; #允许1.0网段访问
        deny all;             #拒绝所有
}

[root@localhost ~]# !s
systemctl reload nginx

5.nginx基于用户的访问控制(auth模块)
在访问网站时需要账号密码认证后才可以访问。

[root@localhost ~]# yum -y install httpd-tools^C
[root@localhost ~]# htpasswd -b -c /etc/nginx/.auth_conf webadmin 123456
#创建用户webadmin 密码123456 用户验证文件路径为 /etc/nginx/.auth_conf
Adding password for user webadmin
[root@localhost ~]# vim /etc/nginx/conf.d/www.conf 

location /status {  #针对status
			stub_status;
			access_log off;
			auth_basic    "input your passwd:";	#用户验证启用描述
			auth_basic_user_file /etc/nginx/.auth_conf;	#用户验证文件路径
		}

[root@localhost ~]# !s
systemctl reload nginx

测试
在这里插入图片描述6.连接频率限制(limit_conn_module模块)
该ngx_http_limit_conn_module模块用于限制每个已定义密钥的连接数,特别是来自单个IP地址的连接数,并非所有连接都被计数。仅当连接具有服务器正在处理的请求并且已经读取了整个请求标头时,才对连接进行计数。

[root@localhost ~]# vim /etc/nginx/nginx.conf 
   	   ........
http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;   
 #创建zone区域名为addr,大小10m,保存客户端的二进制ip
[root@localhost ~]# vim /etc/nginx/conf.d/www.conf
 		.......
server {
  location /download/ {  #针对download引用
		limit_conn addr 1; #一个ip同一时间点只允许建立一个连接
[root@localhost ~]# !sys
systemctl reload nginx.service 

$binary_remote_addr: 将ip地址转换成二进制

7.请求频率限制(limit_req_module模块)
用于限制每一个定义的密钥的请求的处理速率,特别是从一个单一的IP地址的请求的处理速率。使用“漏斗”方法完成限制。

[root@localhost ~]# vim /etc/nginx/nginx.conf
		........
http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;	
 #创建zone区域名为one,大小10m,保存客户端的二进制ip,限制请求速率每秒1次
[root@localhost ~]# vim /etc/nginx/conf.d/www.conf 
		........
server {
			location /download {  #引用到download
				limit_req zone=one burst=5;
				
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# !sys
systemctl reload nginx.service 

验证
再去访问http://192.168.1.61/download/的时候明显比之前慢。

8.nginx日志格式(ngx_http_log_module模块)
该ngx_http_log_module模块中指定的格式写入请求的日志。

[root@localhost ~]# vim /etc/nginx/nginx.conf 
		
http {
	log_format  main  '$remote_addr - $remote_user [$time_iso8601] "$request" ' #定义日志输出格式main
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main; #调用日志格式main
	}

nginx日志格式的变量:
$remote_addr #记录客户端的ip地址
$remote_user #记录客户端的用户名
$time_local #通用的时间格式
$time_iso8601 #iso8601时间格式
$request #请求的方法和请求的HTTP协议
$status #请求状态码
$body_bytes_sent #服务器回应的字节数,不包含头部大小
$bytes_sent #服务器回应的总字节数
$msec #日志写入时间,单位为秒,精度为毫秒
$http_referer #记录链接访问源地址
$http_user_agent #记录客户端浏览器信息
$http_x_forwarded_for #跨越代理服务器,记录客户机的原始ip
$request_length #请求包的长度(请求头+请求正文)
$request_time #请求花费的时间,单位为秒,精度为毫秒

访问网站测试访问日志是否对应

location ~*[root@localhost ~]# curl 192.168.1.61
location ~*[root@localhost ~]# curl 192.168.1.61
location ~*[root@localhost ~]# curl 192.168.1.61
[root@localhost ~]# tail /var/log/nginx/access.log 
192.168.1.1 - - [2020-06-18T23:22:15+08:00] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0" "-"
192.168.1.1 - - [2020-06-18T23:24:29+08:00] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0" "-"
192.168.1.1 - - [2020-06-18T23:24:34+08:00] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0" "-"
192.168.1.1 - - [2020-06-18T23:25:02+08:00] "GET / HTTP/1.1" 200 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0" "-"
192.168.1.1 - - [2020-06-18T23:28:46+08:00] "GET / HTTP/1.1" 200 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0" "-"
192.168.1.1 - - [2020-06-18T23:28:54+08:00] "GET / HTTP/1.1" 200 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0" "-"
192.168.1.61 - - [2020-06-19T00:36:00+08:00] "GET / HTTP/1.1" 200 11 "-" "curl/7.29.0" "-"
192.168.1.61 - - [2020-06-19T00:36:03+08:00] "GET / HTTP/1.1" 200 11 "-" "curl/7.29.0" "-"
192.168.1.61 - - [2020-06-19T00:36:04+08:00] "GET / HTTP/1.1" 200 11 "-" "curl/7.29.0" "-"
192.168.1.61 - - [2020-06-19T00:36:09+08:00] "GET / HTTP/1.1" 200 11 "-" "curl/7.29.0" "-"

9.nginx的location字段

语法规则: location [=|~|~*|^~] /uri/ { … }

下列以优先级从高到低排序

= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到。

案例:测试匹配符的优先级

[root@localhost ~]# vim /etc/nginx/conf.d/test.conf 

server {
        listen 80;
        server_name test.benet.com;

        location / {
                default_type text/html;
                return 200 "location /";
        }
        location =/ {
                default_type text/html;
                return 200 "location =/";
        }
        location ~ / {
                default_type text/html;
                return 200 "location ~ /";
        }
        location ~* / {
                default_type text/html;
                return 200 "location ~* /";
        }
}

访问测试
http://192.168.1.61/(第一个访问到的就是优先级最高的也就是'=')

真实企业场景配置案例:

#通用匹配,任何请求都会匹配到。
location / {

}

#严格区分大小写,匹配.php结尾
location ~ \.php$ {
	fastcgi_pass http://127.0.0.1:9000;
}

#严格区分大小写,匹配.jsp结尾
location ~ \.jsp$ {
	proxy_pass http://127.0.0.1:8080;
}

#不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$ {
	default_type text/html;
	return 403 "启用访问控制";
}
posted @ 2023-04-18 09:52  乱七八糟博客备份  阅读(20)  评论(0编辑  收藏  举报