利用limit_conn模块限制并发连接数
配置方法:
- http区块 开启一块共享内存
- 添加连接数限制
定义共享内存:
http模块定义
http {
...
limit_conn_zone $binary_remote_addr zone=addr:10m;
...
server或 localtion区块中应用
server {
...
...
location /{
limit_conn_status 500; ---定义状态码
limit_conn_log_level warn; --- 定义日志级别
limit_rate 50; --- 定义发送速率 b
limit_conn addr 1; --- 定义并发连接数
}
...
...
}
限制并发连接数配置:
vim /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
limit_conn_zone $binary_remote_addr zone=addr:10m; ##开辟一块共享内存
server {
listen 80;
server_name 10.0.0.20;
root html/leilei01;
index index.html index.htm;
error_log logs/leilei_error.log info; ## 配置日志级别
location /{
limit_conn_status 500; ## 自定义返回码
limit_conn_log_level warn; ## 配置日志级别为 warn
limit_rate 50; ## 最高速度限制为每秒50字节 方便第二个访问出错的出现
limit_conn addr 1; ## 限制最高连接数为 1
}
}
curl 同时访问:
[root@leilei conf]# curl 10.0.0.20
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
第二台访问:
[root@leilei ~]# curl 10.0.0.20
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.16.0</center>
</body>
</html>
限制用户请求 利用limit_req模块限制请求次数
生效阶段: NGX_HTTP_PREACCESS_PHASE
模块: http_limit_req_module
默认编译到了nginx中,如果需要取消该模块 通过重新编译添加参数
--without-http_limit_req_module
禁用此功能算法: leaky bucket 算法
生效范围:
全部的woker进程(基于共享内存)
进入preaccess阶段前不生效
http {
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=2r/m;
server {
...
location /{
limit_conn_status 500;
limit_conn_log_level warn;
limit_req zone=one burst=3 nodelay; ##限制3次
limit_req zone=one; 指定共享内存使用 one
}
...
}
}
思考: 同时打开限制连接数和限制请求数,哪个先生效?
模块放在之前和之后决定限制的优先级
access模块 限制IP请求 allow deny
模块名: http_access_module
默认编译进了nginx 通过重新编译添加
--without-http_access_module
禁用此功能
他在deny之后也会生效
用法:
deny ip地址;
allow ip地址;
allow deny 限制可以出现在:
http
server
location
limit_except
模块中
allow例子:
默认拒绝优先,一旦被拒绝,后续有allow也不允许访问
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
}
限制不仅仅可以是ip或网段,还可以是 mac地址 如:
allow 2001:0b8h::/32;
通过auth_basrc模块实现http用户密码认证
模块名: http_access_auth_basrc_module
默认编译到了nginx中
取消模块可重新编译中添加:
--without-http_auth_basic_module
禁用此功能可以应用在
http
server
location
limit_except
区块中配置中默认不开启此功能 如需开启:
auth_basic on;
实现用户密码认证需要 apache中的 httpd-tools 依赖包
添加认证用户:
htpasswd -b 1.pass leilei 123
用户帐号密码格式:
name1:passwd1
name2:passwd2
查看创建好的用户:
[root@leilei html]# cat 1.pass
leilei:$apr1$1wFjtPA3$Qxhz.3cQhNW0jaEWmd7T3/
在nginx中添加用户认证
第一步:添加httpd认证工具包
yum install -y httpd-tools
第二步:创建认证用户和密码:
进入网页目录:
cd /application/nginx/html
htpasswd -b 1.pass leilei 123
[root@leilei html]# ls -lrt
total 12
-rw-r--r-- 1 root root 612 Jul 7 18:48 index.html
-rw-r--r-- 1 root root 494 Jul 7 18:48 50x.html
drwxr-xr-x 2 root root 24 Jul 10 18:20 www.chenleilei.net
drwxr-xr-x 2 root root 24 Jul 18 14:31 leilei01
-rw-r--r-- 1 root root 45 Jul 18 15:55 1.pass ##认证配置文件
第三步:nginx的区块中添加配置
location /{
##location模块下添加用户密码认证
satisfy any;
auth_basic "test auth_basic";
auth_basic_user_file /application/nginx/html/1.pass;
deny all;
##添加用户密码认证
}
##注意 该配置可以在http` `server` `location` `limit_except` 区块中配置
第四步: 访问测试
输入密码后:
使用auth_request模块配置统一验证应用服务器
它主要是向上游服务器转发请求,如果上游服务器返回的响应码为2xx 则继续执行,若上游服务器返回的是401或者403,则将响应返回给客户端
它的原理:
收到请求后,生成子请求,通过反向代理技术把请求传递给上游服务器。通过上游服务器的响应决定是否处理请求,如:返回码为 200则放行
auth_request模块用法
它默认为关闭状态,如果需要开启,可以使用 auth_request on;
默认状态: auth_request off;
可以使用在:
http
server
location
三个模块中
模块名 –with-http_auth_request_module 默认没有编译到nginx中
按序访问资源的try_files模块
测试代码:
server {
server_name 10.0.0.20;
error_log logs/myerror.log info;
root html/;
default_type text/plain;
location /a {
try_files /system/1.html
$uri $uri/index.html $uri.html
@lasturl;
}
location @lasturl {
return 200 'lasturl!\n';
}
location /second {
try_files $uri $uri/index.html $uri.htm =404;
}
}
解释:
首先会尝试访问
html/a/
目录中的1.html
文件,如果没有的话就会去找
@uri
这个变量,这个变量对应的index.html,如果这个也没有就会去找$uri.html如果没有就去访问
@lasturl
,这个@lasturl
中定义,如果访问到lasturl就直接返回200状态码
访问/second 目录的index.html 如果没有就直接返回404
该模块作用:
常用于反向代理,首先尝试获取磁盘文件,如果没有文件就代理到新的文件中。
实时拷贝流量: 镜像模块 mirror模块
他是属于precontent阶段的模块
默认编译到了nginx中,通过 --whitout-http_mirror_module 移除模块
作用:
在处理请求时,生成子请求访问其他服务,对子请求的返回值不做处理
默认关闭
生效在:
http
server
location
三个模块中开启方式:
mirror no;
nginx 反向代理缓存
配置方法:
pc1:
upstream leilei { #定义池子
server 127.0.0.1:81;
}
server {
listen 80;
server_name 10.0.0.20;
location / {
proxy_pass http://leilei; #引入池子
proxy_set_header Host $host; #客户端请求的域名
proxy_set_header X-Real-IP $remote_addr; # 传递用户真实IP
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for; #将真实IP传递给nginx时使用的配置
}
}
=============================
http 区块定义 反向代理缓存
levels=1:2 层级
keys_zone=leilei_cache:10m 缓存名和大小
max_size=20m
inactive=100s #定义100秒不访问删除缓存
http {
...
proxy_cache_path /application/nginx/nginx_cache levels=1:2 keys_zone=leilei_cache:10m max_size=20m inactive=100s;
...
}
server location 区块引用
server {
...
proxy_cache leilei_cache;
...
}


发表评论