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
nginx常用模块(一)的更多相关文章
- nginx常用模块(三)
Nginx常用模块(三) ngx_http_proxy_module模块配置(http或https协议代理) proxy_pass URL; 应用上下文:location, if in locatio ...
- Nginx 常用模块
Nginx 常用模块 1. ngx_http_autoindex_module # ngx_http_autoindex_module模块处理以斜杠字符(' / ')结尾的请求,并生成一个目录列表. ...
- (转)nginx 常用模块整理
原文:http://blog.51cto.com/arm2012/1977090 1. 性能相关配置 worker_processes number | auto: worker进程的数量:通常应该为 ...
- Nginx常用模块安装命令
将目录切换至Nginx安装包目录下,使用./configure命令进行安装.一些第三方模块需要先下载过来,指定下解压后的目录即可. ./configure --prefix=/usr/local/ng ...
- nginx常用模块
Nginx模块介绍 核心模块:core module 标准模块:stand modules HTTP modules: Standard HTTP modules Optional HTTP modu ...
- 9.Nginx常用模块
1.nginx开启目录浏览 提供下载功能 默认情况下,网站返回index指定的主页,若该网站不存在主页,则将请求交给autoindex模块 如果开启autoindex模块,则提供一个下载的页面, 如果 ...
- Nginx常用模块及作用
Nginx模块详解 nginx模块分为两种,官方和第三方,我们通过命令 nginx -V 查看 nginx已经安装的模块! [root@localhost ~]# nginx -V nginx ver ...
- 07 . Nginx常用模块及案例
访问控制 用户访问控制 ngx_http_auth_basic_module 有时我们会有这么一种需求,就是你的网站并不想提供一个公共的访问或者某些页面不希望公开,我们希望的是某些特定的客户端可以访问 ...
- NGINX常用模块(二)
5.Nginx日志配置 Nginx有非常灵活的日志记录模式.每个级别的配置可以有各自独立的访问日志.日志格式 通过log_format命令定义格式 1.log_format指令 # 配置语法:包括:e ...
随机推荐
- C#图片闪烁
导致画面闪烁的关键原因分析: 一.绘制窗口由于大小位置状态改变进行重绘操作时 绘图窗口内容或大小每改变一次,都要调用Paint事件进行重绘操作,该操作会使画面重新刷新一次以维持窗 ...
- 操作系统类型&操作系统结构&现代操作系统基本特征
五大类型操作系统 (1). 批处理操作系统 用户脱机使用计算机 用户提交作业之后直到获得结果之前就不再和计算机打交道. 作业提交的方式可以是直接交给计算中心的管理操作员,也可以是通过远程通讯线路提交. ...
- distpicker.js 根据当前位置初始化select
学习参考的地址放在最醒目的地方: https://blog.csdn.net/idea_boy/article/details/58280076 百度官方实例:http://developer.bai ...
- List中bean某属性值转换为list
List<类> lst = new ArrayList<>() ; lst.stream().map(类::get需要取得仠的属性名).collect(Collectors.t ...
- html通配符
♠ ♠ ♠ 黑桃 ♣ ♣ ♣ 梅花 ♥ ♥ ♥ 红桃,心 ♦ ♦ ♦ 方块牌 ◊ ◊ ◊ 菱形 † † † 匕首 ‡ ‡ ‡ 双剑号 ¡ ¡ ¡ 反向感叹号 ¿ ¿ ¿ 反向问号 ← ← ← 左箭头 ...
- 数据库之存储过程Procedure
数据库之存储过程 一.概述 SQLserver中视图通过简单的select查询来解决复杂的查询,但是视图不能提供业务逻辑功能,而存储过程可以办到. 二.什么是存储过程 存储过程procedure是一组 ...
- spring boot集成mybatis(2) - 使用pagehelper实现分页
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- (BUILDER)建造者与(FACTORY)工厂模式 的比较
首先,说明下 参考博文 1. 建造者 http://www.cnblogs.com/zhili/p/BuilderPattern.html 2. 抽象工厂 http://www ...
- [ACTF2020 新生赛]Include
0x00 知识点 本地文件包含 ?file=php://filter/read/convert.base64-encode/resource=index.php ?file=php://filter/ ...
- FFmpeg的基本使用
1.FFmpeg理解 (1)FFmpeg是一个视屏.音频编码工具 (2)x项目名称mpeg来源mpeg编码标准,但不局限只能使用mpeg编码标准.FF 表示fast forward (3)被广泛使用. ...