nginx中有一些比较常见的模块,我们可以列出来一些常用的
autoindex:列出目录索引
| |
| [root@web01 module] |
| server { |
| listen 80; |
| server_name module..com; |
| charset utf-8,gbk; |
| location / { |
| root /module; |
| autoindex on; |
| autoindex_exact_size off; |
| autoindex_localtime on; |
| } |
}
| |
| 2.准备对应的目录,往目录中上传一点文件 |
| ```bash |
| mkdir /module/{centos,ubuntu,redhat}/ -p |
3.检查语法并重新加载Nginx
| nginx -t |
| systemctl restart nginx |
| location /nginx_status { |
| stub_status; |
| } |
最终展示的结果:
| Active connections: 2 |
| server accepts handled requests |
| 3 3 33 |
| Reading: 0 Writing: 1 Waiting: 1 |
| |
| Active connections |
| accepts |
| handled |
| requests |
| |
| Reading |
| Writing |
| Waiting |
注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接
1.拒绝10.0.0.1
来源IP访问,其他人允许。
| location /nginx_status { |
| stub_status; |
| deny 10.0.0.1/32; |
| allow all; |
| } |
2.允许10.0.0.1
来源IP访问,其他人全部拒绝。
| location /nginx_status { |
| stub_status; |
| allow 10.0.0.1/32; |
| deny all; |
| } |
3.实际配置监控Nginx状态时,仅允许该服务器的回环地址访问127.0.0.1
| location /nginx_status { |
| stub_status; |
| allow 127.0.0.1; |
| deny all; |
| } |
1.生成一个密码文件,密码文件的格式 name:password(加密) (建议使用htpasswd)
| [root@web01 conf.d] |
| [root@web01 conf.d] |
| [root@web01 conf.d] |
| oldboy:$apr1$Kp87VSae$658Nt5bm4iiblQkUvP7u61 |
2.配置Nginx,限制对应的资源
| location /download { |
| root /module; |
| autoindex on; |
| autoindex_exact_size off; |
| autoindex_localtime on; |
| |
| auth_basic "Please Password!!!"; |
| auth_basic_user_file /etc/nginx/auth_conf; |
| } |
设置共享内存区域和给定键值的最大允许连接数。超过此限制时,服务器将返回错误以回复请求
http标签段定义连接限制
| http{ |
| limit_conn_zone $binary_remote_addr zone=conn_zone:10m; |
| } |
| server { |
| |
| limit_conn conn_zone 1; |
| |
| location / { |
| root /code; |
| index index.html; |
| } |
使用ab工具进行压力测试
| [root@localhost ~] |
| [root@localhost ~] |
| |
| 2019/01/14 11:11:22 [error] 29962 |
| 2019/01/14 11:11:23 [error] 29962 |
| 2019/01/14 11:11:25 [error] 29962 |
| 2019/01/14 11:11:25 [error] 29962 |
设置共享内存区域和请求的最大突发大小。过多的请求被延迟,直到它们的数量超过最大突发大小,在这种情况下请求以错误终止。默认情况下,最大突发大小等于零。
1.定义限制的key(基于什么来做限制,IP地址)
| [root@web01 conf.d] |
| limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; |
| server { |
| listen 80; |
| server_name test1.oldboy.com; |
| |
| limit_req zone=req_zone burst=5 nodelay; |
| limit_req_status 412; |
| error_page 412 /err.html; |
| |
| location / { |
| root /code/test1; |
| index index.html; |
| } |
| } |
2.填写hosts域名解析
| echo "10.0.0.7 test1.oldboy.com" >> /etc/hosts |
3.进行简单的压力测试
| ab -n 50 -c 20 http://test1.oldboy.com/index.html |
1.Location语法示例
| location [=|^~|~|~*|!~|!~*|/] /uri/ { ... |
| } |
匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~ 不区分大小写的正则匹配 4
!~ 区分大小写不匹配的正则 5
!~ 不区分大小写不匹配的正则 6
/ 通用匹配,任何请求都会匹配到 7
通用匹配,任何请求都会匹配到
严格区分大小写,匹配以.php结尾的都走这个location
严格区分大小写,匹配以.jsp结尾的都走这个location
不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
| location ~* .*\.(jpg|gif|png|js|css|mp4)$ { |
| ... |
| } |
不区分大小写匹配
| location ~* "\.(sql|bak|tgz|tar.gz|.git)$" { |
| ... |
| } |