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 ...
随机推荐
- PowerDesigner创建索引
防止以后忘记怎么设置索引,记录下来方便查翻 1:选中Table 2:找到Table对应的Indexes 3:选中一条记录,点击红框中的小手(Properties)或双击该记录,进入到详细里面 4:找到 ...
- list实体数据分组
比如查询获取了60000条数据进行批量插入数据库,一次直接插入6万可能不是很好,可以将6万条数据按照5000分成几组,每组批量插入5000条 List<T> list = new List ...
- Java Integer Addition Subtration Overflow 整数加减溢出
leetCode有道题Reverse Integer,因为int的最大值为2的31次方减一,最小值为-2的31次方. 我一开始的代码将res递归加放在try中,以为溢出会有异常,然而并没有. 因为出传 ...
- springboot - 映射 HTTP Response Status Codes 到自定义 JSP Error 页面
1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...
- 51nod 1103:N的倍数 抽屉原理
1103 N的倍数 题目来源: Ural 1302 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 一个长度为N的数组A,从A中选出若干个数,使得这 ...
- 如何在Ubuntu 18.04上安装和卸载TeamViewer
卸载命令:sudo apt --purge remove teamviewer 安装:https://www.linuxidc.com/Linux/2018-05/152282.htm 如何在Ubun ...
- 【LeetCode】子集
[问题]给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).说明:解集不能包含重复的子集. 示例: 输入: nums = [,,] 输出: [ [], [], [], [,,] ...
- SpringBoot基于easyexcel导出和写入Excel
easyexcel是阿里巴巴旗下开源项目,主要用于Excel文件的导入和导出处理,今天我们利用SpringBoot和easyexcel实战演示如何导出和写入Excel文件. 一.加入我们需要的ea ...
- 字符,字符串,int之间互相转换
字符转换成字符串:String str = String.valueOf(ch); 字符转换成int: int a = ch; 字符串转换成字符:char ch = str.charAt(0); 字符 ...
- 解决TeamViewer提示商业用途
安装此插件 提取码:i8o3