Nginx基础入门(二)
八、Nginx WEB模块
1.1 stub_status_moudule
目的 :展示用户和nginx链接数量信息。
#查询模块是否安装
[root@xuleilinux ~]# nginx -V 2>&1 | grep stub_status
--with-http_stub_status_module
启动状态模块,默认该功能没启动
#在server下面编写
location /nginx_status {
stub_status;
allow all;
}
"注意空格"
重启服务登录
192.168.238.132/nginx_status
F5刷新
分表代表:1:总连接数 1:成功连接数 9:总共处理的请求数
Active connections: 1 当前活动的连接数
server accepts handled requests
服务器接受处理请求
Reading: 0 读取客户端Header的信息数 请求头
Writing: 1 返回给客户端的header的信息数 响应头
Waiting: 0 等待的请求数,开启了keepalive
1.1.1 关于链接的问题
什么是链接
1.2 随机主页
ramdom_index_module
目的:将主页设置成随机页面,是一种微调更新机制
- 创建主页目录 mkdir /app
- 创建多个主页
/app/{blue.html,green.html,red.html,.yellow.html}
#在不同页面写不同的内容,eg:
<html>
<head>
<title>green color</title>
</head>
<body style="background-color:green">
<h1>green color!</h1>
</body>
</html>
- 启动随机主页
vim /etc/nginx/conf.d/default.conf
server{#在server里面添加
location / {#root /usr/share/nginx/html;#index index.html index.htm;root /app;random_index on;
}
}
重启
F5刷新
注意隐藏文件并不会被随机选取
需要调用:app.com/yellow.html
1.3 替换模块
sub_module
目的:网页内容替换
如果我们用模板生成网站的时候,因为疏漏或者别的原因造成代码不如意,但是此时因为文件数量巨大,不方便全部重新生成,那么这个时候我们就可以用此模块来暂时实现纠错。另一方面,我们也可以利用这个实现服务器端文字过滤的效果。(当服务器断电或者出错,可以快速使用此模块替换)
vim /etc/nginx/conf.d/default.conf 启动nginx默认页面server {sub_filter nginx 'QianFeng';
sub_filter_once on;
location / {root /usr/share/nginx/html;
index index.html index.htm;}
重启服务
1.4 文件读取
ngx_http_core_module
语法:
Syntax:sendfile on | off;
Default:sendfile on;
Context:http,server,location,if in location
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location
原理:sendfile
未使用sendfile() 的传统网络传输过程:
使用 sendfile() 来进行网络传输的过程:
硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈 sendfile() 不但能减少切换次数而且还能减少拷贝次数。
使用这个变量可以直接进入内核缓冲区进行传输
tcp_nopush
未使用tcp_nopush()网络资源浪费
用处:延长包的时间,当一个包达到40字节发送
应用程序每产生一次操作就会发送一个包,而典型情况下一个包会拥有一个字节的数据以及40个字节长的包头,于是产生4000%的过载,很轻易地就能令网络发生拥塞。同时也浪费资源
使用tcp_nopush()网络资源浪费
tcp_nopush()网络资源浪费
tcp_nodelay
开启或关闭nginx使用TCP_NODELAY选项的功能。 这个选项仅在将连接转变为长连接的时候才被启用。
TCP_NODELAY是禁用Nagle算法,即数据包立即发送出去。
由于Nagle和DelayedACK的原因,数据包的确认信息需要积攒到两个时才发送,长连接情况下,奇数包会造成延时40ms,所以tcp_nodelay会将ack立刻发出去。 如果不在长连接时,可以关闭此模块,因为ack会被立刻发出去。
1.5 文件压缩
原理介绍
启动该模块,使文件传输前进行压缩,提升传输效率。
模块:ngx_http_gzip_module
语法:
Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in location
Syntax: gzip_comp_level level;
Default: gzip_comp_level 1;(1~9) #压缩级别
Context: http, server, location
Syntax: gzip_http_version 1.0 | 1.1;
Default: gzip_http_version 1.1;
Context: http, server, location
启动模块····································································································································
观察未压缩传输
拷贝图片至文件主目录,并编写代码
vim /xuleilinux/index.html
<!DOCTYPE html><html> <head> <meta charset="utf-8"></head> <body><img src="1.jgp" width="200" /> #src=“1.jpg” 文件路径 </body>
通过浏览器下载后查看大小
![在这里插入图片描述](https://img-blog.csdnimg.cn/24c796d65ce64d4fa73ad0a19196c14d.png)
vim /etc/nginx/nginx.conf 主配置文件下,开启压缩功能
http {gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_static on;
}
开启压缩功能后
1.6 页面缓存
模块:ngx_http_headers_module
expires起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求要配置expires,可以在http段中或者server段中或者location段中加入。
Nginx(expires 缓存减轻服务端压力),
语法:
Syntax:expires [modified] time; 解释:到期有效时间;
expires epoch | max | off; || epoch:指定“Expires”的值为 1 January,1970,00:00:01 GMT
Default: expires off;
Context: http, server, location, if in location
epoch:指定“Expires”的值为 1 January,1970,00:00:01 GMT
max:指定“Expires”的值为10年
-1:指定“Expires”的值为当前服务器时间-1s,即永远过期。
off:不修改“Expires”和"Cache-Control"的值
原理介绍
无缓存,每次访问服务器,均是全文传输。
开启缓存可以加速浏览网站。
![在这里插入图片描述]
服务器回复的缓存时间24小时;缺点时效性降低
1.7 防盗链
简单的说就是:别的网站可能用你网站的链接来访问你的网页,使用的流量最后是你的;
模块:ngx_http_referer_module
语法
Syntax: valid_referers none | blocked | server_names | string …;
Default: —
Context: server, location
log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ remoteuser[time_local] “KaTeX parse error: Double superscript at position 37: … '̲status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer” ’
‘“ h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""http_x_forwarded_for”’;
日志格式中的http_referer是记录,访问点引用的URL。也就是超链接的上一级地址。
通过这段地址,可以发现一种网络行为——盗链。非法盗链会影响站点的正常访问。
通过http_referer模块可以控制这一点。防止非法盗链现象。
展现防盗链现象
- 搭建a/b网站
- 在B网站盗用A网站图片
- 访问b.com 注意b网站日志
tail -f /var/log/nginx/access.log
总结B网站访问xuleilinux.com没有访问地址,也没有访问目录
启用xuleilinux.com的防盗功能
vim /etc/nginx/conf.d/xuleilinux.conf
location / {
valid_referers none blocked *.xuleilinux.comif ($invalid_referer) {return 403; }
`注意:空格 分号`
这时网站a不能访问xuleilinux.com
防盗链白名单
location / {
valid_referers none blocked *.xuleilinux.com
#添加网站白名单
server_name a.com; *****if ($invalid_referer) {return 403; }
这时a网站又可以访问xuleilinux.com
九、访问限制
模块:ngx_http_limit_rep_module
作用:
启动请求频率限制
vim /etc/nginx/nginx.confhttp {limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; 定义#限制请求 二进制地址 限制策略名称 占用10M空间 允许每秒1次请求server {location / {root /usr/share/nginx/html;index index.html index.htm;limit_req zone=req_zone; 引用#limit_req zone=req_zone burst=5;#limit_req zone=req_zone burst=5 nodelay; }}
}
#burst=5 表示最大延迟请求数量不大于5。 如果太过多的请求被限制延迟是不需要的 ,这时需要使用nodelay参数,服务器会立刻返回503状态码。
root@xuleilinux ~]# ab -n 100 -c 10 http://www.tianyun.me/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking www.tianyun.me (be patient)...^C
[root@xuleilinux ~]# ab -n 100 -c 10 http://www.apache.org/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking www.apache.org (be patient).....doneServer Software: Varnish
Server Hostname: www.apache.org
Server Port: 80Document Path: /
Document Length: 0 bytesConcurrency Level: 10
Time taken for tests: 18.838 seconds
Complete requests: 100
Failed requests: 0 #失败请求
Write errors: 0
Non-2xx responses: 100 #有问题的响应
Total transferred: 32122 bytes
HTML transferred: 0 bytes
Requests per second: 5.31 [#/sec] (mean)
Time per request: 1883.821 [ms] (mean)
Time per request: 188.382 [ms] (mean, across all concurrent requests)
Transfer rate: 1.67 [Kbytes/sec] receivedConnection Times (ms)min mean[+/-sd] median max
Connect: 128 726 1360.4 168 10147
Processing: 136 617 810.0 192 4985
Waiting: 136 460 781.4 168 4985
Total: 269 1343 1643.3 817 10298Percentage of the requests served within a certain time (ms)50% 81766% 129475% 135080% 216990% 334395% 438498% 814599% 10298100% 10298 (longest request)
ngx_http_limit_conn_module
1.
2.
十、访问控制
ngx_http_access_module
Dirrctives
:
allow 允许某些主机
deny 拒绝
Syntax
Syntax: allow address | CIDR | unix: | all;
Context: http, server, location, limit_except
基于用户(username &passwd)
1.建立认证文件
yum install -y httpd-toolshtpasswd -cm /etc/nginx/conf.d/passwd user10
htpasswd -m /etc/nginx/conf.d/passwd user20 #会话密码cat /etc/nginx/conf.d/passwd
2.启动认证
vim /etc/nginx/conf.d/default.confserver {auth_basic "nginx access test!";auth_basic_user_file /etc/nginx/conf.d/passwd;
}
Nginx基础入门(二)相关推荐
- Vue基础入门(二)
Vue基础入门目录 一.过滤器vue3已经删除!!!!!! 1.什么是过滤器(Filters) 2.代码展示 3.私有过滤器和全局过滤器 4.过滤器的注意点 5.优化时间代码示例 二.watch 侦听 ...
- Nginx基础入门(一)
一.Nginx的优势 1.1 发展趋势 2016 2019 1.2 简介 Nginx (engine x) 是一个高性能的HTTP(解决C10k的问题)和反向代理服务器,也是一个IMAP/POP3/S ...
- 科协arduino二:基础入门二
一:电路基础: 1,电流:电子在导体中流动,形成电流.电流的计量单位是 安培(A), 1 A = 1000 mA (毫安) 2,电压:指电路两端的电势差或电位差.这个电位差引发电子的流动,产生电 ...
- Nginx基础入门之nginx基础配置项介绍(2)
前面我们讲到关于nginx配置文件(nginx.conf)运行,工作模式,及相关调试的等等配置语法,本小节我们将会讲到怎么去配置一个基本的web,以及相关配置项的定义,虚移主机以及请求的分发的语法配置 ...
- [转载] python基础入门二
参考链接: Python集合Set 写代码,有如下变量,请按照要求实现每个功能 (共6分,每小题各0.5分) name = " aleX" 1)移除 name 变量对应的值两边 ...
- 36.Nginx 基础入门篇
1. 简介: Nginx(engine x)是一个高性能的HTTP(解决C10k的问题)和反向代理服务器,也是一个IMAP/POP3/SWTP服务器. 2. Nginx的w ...
- 002_HTML基础入门二【列表 + 表格】
文章目录 一.列表 List 1.有序列表 ol.li 2.无序列表 ul.li 3.定义列表 dl.dt.dd 二.表格 Table 1.基本标签 2.合并单元格 3.thead.tbody.tfo ...
- 黑马程序员C++基础入门(二)——程序流程结构 - 数组
本文转载自:https://github.com/AnkerLeng/Cpp-0-1-Resource.git 视频见:https://www.bilibili.com/video/av4155972 ...
- Canvas基础入门 - 二 时钟与刮刮卡
文章目录 前言 一.时钟绘制思路及代码实现 1.代码 2.注意事项 二.刮刮卡绘制思路及代码实现 2.代码实现 前言 提示:以下是本篇文章正文内容,下面案例可供参考 一.时钟绘制思路及代码实现 思路与 ...
最新文章
- 【转】【C#】判断两个文件是否相同
- win10解决Mysql net start mysql启动,提示发生系统错误 5 拒绝访问
- Oracle WebCenter 11g 快速开发指南--翻译(二)
- MySQL索引和SQL调优手册
- Bootstrap 排版强调
- Leetcode134.加油站
- C# IMEI15位转换成8位密码
- 烽火fr2600怎么web登录_烽火路由器回收,烽火交换机回收,烽火无线AP回收
- 2年前端 杭州 面试 集合 面经 前端
- ios企业证书过期更新及推送证书更新(推荐)
- 2011软专高级程序语言T4(二维数组按一维数组访问)
- 从事文字工作和经常使用电脑的人要注意保护好自己的眼睛
- python解常微分方程龙格库_数值常微分方程-欧拉法与龙格-库塔法
- Linux画图工具gnuplot
- python操作CAD转存dwg文件
- Java、JSP基于Web的师生互动系统的设计与实现
- Breed Counting(水?)
- STM32一个定时器同时捕获4路PWM波
- 股票分仓资管跟单软件代码运行程序用那种语言写比较稳定
- 个人计算机的基本配置显卡,计算机最基本配置之一显卡bios是怎样设置的?
热门文章
- PART 2.1 风控模型种类变量选择范围变量衍生特征交叉
- Prolog教程 14--cut的功能
- Presto下载地址
- 武汉市洪山区高新技术企业认定奖励标准及申请材料、条件、要求
- matlab butter stop,Matlab butter函数设计滤波器
- 这个水卡算法规律有会的吗?
- Reinforcement Learning,微信公众号:DRL学习
- 最大的疑问:下一跳???
- UVA-10929-You can say 11(秦九昭算法+同余与模算术)
- “SqlSession[xxx] was not registered for synchronization because synchronization is not active”问题成功解决