nginx常用模块(一)
1.Nginx目录索引
1.1Nginx默认是不允许列出整个目录浏览下载。
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
# autoindex常用参数
autoindex_exact_size off;
默认为on,显示文件的确切大小,单位是bytes
修改为off,显示出文件的大概大小,单位是KB或MB或者GB。
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
修改为on,显示的文件时间为文件的服务器时间。
charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码。
例子1:需求:
1.当我们访问game.oldboy.com的时候打开首页
2.当我们访问game.oldboy.com/download的时候,会打开目录索引列表
)修改配置文件
[root@web01 conf.d]# vim game.conf
server {
listen ;
server_name www.xiao.com; location / {
root /xiao;
index index.html;
} location /img {
root /xiao;
autoindex on;
charset utf-,gbk;
autoindex_exact_size off;
autoindex_localtime on;
}
}
)创建文档目录
mkdir -p /xiao/img
例2:实现作业上传系统(作业上传需要php实现)
) 获取源码
mkdir -p /xiao/zuoye
cd /xiao/zuoye
将作业页面的源码通过xshell拖拽进去
unzip kaoshi.zip ) 创建页面配置文件
vim zuoye.conf
server {
listen ;
server_name upload.xiao.com; location / {
root /xiao/zuoye;
index index.html;
}
} )语法测试,重载nginx
nginx -t
nginx -s reload ) 添加hosts主机解析
10.0.1.7 www.xiao.com upload.xiao.com
排错,看日志:
tail /var/log/nginx/error.log
2.Nginx状态监控
2.1.ngx_http_stub_status_module用于展示Nginx连接状态信息,需要--with-http_stub_status_module模块支持 location /basic_status {
stub_status;
access_log off;
} Active connections:
server accepts handled requests Reading: Writing: Waiting: Active connections # 当前活动的连接数
accepts # 当前的总连接数TCP
handled # 成功的连接数TCP
reques # 总的http请求数
注意:
1)如果使用restart重置服务,会清空所有的连接数
2)reload重载不会情况之前的连接数
3)通过状态监控,可以区分长连接和短连接
vim /etc/nginx/nginx.conf 修改下面参数
keepalive_timeout 0; #将长连接变为短连接
3.Nginx访问控制
基于IP的访问控制 NGX_http_Access_module模块
基于用户登陆认证 ngx_http_auth_basic_module模块
3.1Nginx基于IP的访问控制NGX_http_Access_module
//允许配置语法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
//拒绝配置语法
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
访问控制规则查看流程:
从上往下,依次匹配,满足就停止
企业中访问控制的思路:
先写允许,默认拒绝所有
先写拒绝,默认允许所有
案例1:只允许10.0.1.1访问nginx_status,其他全拒绝
vim /etc/nginx/conf.d/game.conf
location /nginx_status {
stub_status;
access_log off;
allow 10.0.1.1;
deny all;
}
案例2:拒绝10.0.1.1访问nginx_status,其他全允许
vim /etc/nginx/conf.d/game.conf
location /nginx_status {
stub_status;
access_log off;
deny 10.0.1.1;
allow all;
}
3.2Nginx基于用户登陆认证 ngx_http_auth_basic_module
//配置语法
Syntax: auth_basic string | off;
Default:
auth_basic off;
Context: http, server, location, limit_except //用户密码记录配置文件
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
//需要安装依赖组件
[root@web01 ~]# yum install httpd-tools -y
[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf xiao
Adding password for user xiao //可在http,server,location下添加如下信息
auth_basic "don't test,get out";
auth_basic_user_file /etc/nginx/.auth.conf;
http是明文传输,抓包测试
4.Nginx访问限制
经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量的恶意的攻击访问,会带来带宽的浪费,服务器
压力,影响业务,往往考虑对同一个IP的连接数,并发数进行限制。
ngx_http_limit_conn_module模块可以根据定义的key来限制每个键值的连接数
limit_conn_module 连接频率限制
limit_req_module 连接请求限制
HTTP请求建立在一次TCP连接基础上,一次TCP连接至少产生一次HTTP请求
变量:
$binary_remote_addr 变量的长度是固定的4字节
$remote_addr 变量的长度是7-15字节 一个IP地址=32bit=4字节 10M=*1024K=**1024B/
Nginx连接限制实战
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http Syntax: limit_conn zone number;
Default: —
Context: http, server, location //http段配置限制,同一时刻只允许一个客户端IP连接
limit_conn_zone $binary_remote_addr zone=conn_game:10m; server {
...
limit_conn conn_game ;
...
}
Nginx请求限制配置实战
)Nginx请求限制语法 Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location
)Nginx请求限制实战
//http段配置请求限制,rate限制速率,限制一秒钟最多一个IP请求
limit_req_zone $binary_remote_addr zone=req_game:10m rate=1r/s;
...
server {
... location{
//1r/s只接受1个请求,其余请求拒绝处理并返回错误
limit_req zone=req_game;
//请求超过1r/s,剩下的将被延迟处理,请求数据超过burst定义的数量,多余的请求返回503
limit_req zone=req_game burst= nodelay;
}
}
)使用ab工具进行压力测试
yum install -y httpd-tools
vim /etc/hosts
10.0.1.7 www.xiao.com
[root@web01 ~]# ab -n -c http://www.xiao.com/index.html
最新文章
- nodejs中流(stream)的理解
- 在Windows下快速搭建SVN服务器 VisualSVN
- [BTS] SQL Adapter. New transaction cannot enlist in the specified transaction coordinator
- Java SE 第二十三讲----static关键字and final关键字
- 如何解决Android的SDK与ADT不匹配问题
- Java使用wkhtmltox实现HTML代码生成PDF文档或者图片
- ***SQL统计语句总结(运用场景:运营分析,财务分析等)
- JDBC向oracle插入数据
- openfire for mac 无法启动
- paip.php-gtk 桌面程序 helloworld总结
- Web前端优化需要注意的点
- linux 下修改etc/profile文件
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
- 理解mpvue的生命周期
- Linux 远程连接sftp与ftp
- Ubuntu 14.04 下安装 TFTP 艰辛之路【转】
- jQuery中 attr和Prop的区别
- JD-GUI
- O(∩_∩)O哈哈~
- 鸟哥的linux私房菜 - 第三章 主机规划与磁盘分区
热门文章
- 【BZOJ4237】稻草人
- Docker Python 例子
- springMVC上传文件和文件下载
- HDU 5280 BestCoder Round #47 1001:Senior's Array
- BZOJ 4888 [Tjoi2017]异或和
- redis主要配置项
- 大二暑假第五周总结--开始学习Hadoop基础(四)
- [转自官方文档] Django——render()
- C# 添加Log文件、记录Log
- 由于TableView的Section的头部和尾部高度设置的不规范引起的部分Section中的图片无法正常显示