33Nginx模块的从配置与优化
nginx常用模块
nginx区块关系有三级:
1:nginx.conf文件中的http模块在全局生效
2:指定业务配置文件下的server模块,对当前的server和location生效
3:location中的配置只对location中生效
用户名密码登录验证模块
auth_basic # 描述信息
auth_basic_user_file # 指定用户和密码文件所在的路径
配置流程:
1,安装htpasswd,密码文件需要用htpasswd生成
yum -y install httpd-tools
2,生成密码文件,用户123,密码456
[root@123456 conf.d]# htpasswd -b -c /etc/nginx/auth_pass 123 456
Adding password for user 123
3,配置nginx
location /download {alias /package;auth_basic "test";auth_basic_user_file /etc/nginx/auth_pass;
}
IP地址限制模块:
1,先允许,后拒绝:
allow 192.168.74.8;
deny all;
}location /download {alias /package;auth_basic "test";auth_basic_user_file /etc/nginx/auth_pass;allow 192.168.74.8;deny all;
}
2,先拒绝,后允许
deny 192.168.74.8;
allow all;
}location /download {alias /package;auth_basic "test";auth_basic_user_file /etc/nginx/auth_pass;deny 192.168.74.8;allow all;
}
nginx状态模块:
以用于查看 Nginx 的基本运行状态信息,通常称为 nginx_status
状态信息解释:
• Active connections:当前活跃的连接数
• accepts:总接受的连接数
• handled:总处理的连接数(通常与 accepts 相同,除非达到资源限制)
• requests:总处理的请求数
• Reading:正在读取客户端请求的连接数
• Writing:正在向客户端发送响应的连接数
• Waiting:处于空闲状态、等待客户端请求的连接数(keep-alive 连接)
配置流程
打开conf文件,新建一个location模块,写入stub_status,nginx_status不需要存在
location /nginx_status {stub_status;
}[root@123456 etc]# curl game1.com/nginx_status
Active connections: 1
server accepts handled requests16 16 66
Reading: 0 Writing: 1 Waiting: 0
请求,连接限制模块:
limit_conn_zone
和limit_req_zone是定义语句,需要写入主配置文件nginx.conf中的http模块
limit_conn_zone定义语法:
limit_conn_zone 键变量 空间:大小;例
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
注意$binary_remote_addr 是最常用的,表示二进制ip
limit_req_zone定义语法:
limit_conn_zone 键变量 空间:大小
限制速率,例
llimit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
这句话代表一秒最多一个ip请求
[root@123456 nginx]# vim nginx.conf http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;limit_conn_zone $binary_remote_addr zone=conn_limit:10m;limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;include /etc/nginx/conf.d/*.conf;
}
主文件配置完成,接下来来到具体业务文件
limit_conn conn_limit 1 #连接限制,限制同时最高1个连接
imit_req zone=req_zone burst=3 nodelay;# 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
[root@123456 conf.d]# vim game1.conf server {listen 192.168.74.128:80;server_name game1.com;#access_log /var/log/nginx/host.access.log main;root /code/;limit_conn conn_limit 1;-----------------------------------------最大一个连接limit_req zone=req_zone burst=3 nodelay;-------------------------请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503location / {#index index.html index.htm;}location /nginx_status {stub_status on;}location /download {alias /package;auth_basic "test";auth_basic_user_file /etc/nginx/auth_pass;allow 192.168.74.8;deny all;
}
开启目录模块
autoindex on
是 Nginx 中用于开启目录浏览功能的指令,当访问的路径是一个目录且没有默认索引文件(如 index.html
)时,Nginx 会自动生成该目录的文件列表页面,方便用户查看目录中的文件。
第一步:在块中开启autoindex on,可以加入一些参数方便阅读
charset utf-8,gbk; # 字符集解决中文乱码问题
alias /package;
autoindex on; # 开启目录列表 将代码目录下的index.html移除
autoindex_localtime on; # 以系统时间为准显示浏览器autoindex_exact_size off; # 人类可读文件大小
第二部:把 index
指令指定的文件(如 index.html
)移除,就可显示目录
location匹配规则
1.等号 =
2.以开头 ^~
3.区分大小匹配 ~*
4.不区分大小写 ~*
5.默认匹配 /
这是我的个人学习笔记,主要用于记录自己对知识点的理解和梳理。由于目前仍在学习探索阶段,内容中难免存在理解偏差或表述疏漏,恳请各位大佬不吝赐教,多提宝贵意见~ 若有不同看法,欢迎理性交流探讨,感谢包容与指正!