创建一个网站
server {
listen 80;
server_name mirror.oldxu.com;
charset utf-8; #字符集
root /mirror;
location / {
index index.html;
}
#提供yum源的仓库
location /repo {
autoindex on; #开启autoindex,当访问网站找不到默认index.html站点界面时,就会把目录的文件以列表返回
autoindex_exact_size off; #关闭文件大小(默认kb显示),关闭后一mb显示
autoindex_localtime on; #开启显示文件上传时间,关闭显示文件创建时间
}
}
2.访问控制
2.1 基于来源的IP实现访问控制
1.仅允许哪些IP可以访问 allow 允许 deny all 拒绝
2.拒绝摸个IP访问,其他IP都可以 正常访问
deny 10.0.0.100/32; 拒绝
allow all; 允许
注意: deny和allow的顺序是有 影响的。
默认情况下从第一条规则开始匹配
如果匹配成功,检查规则是允许还是拒绝。但不在继续匹配下面的内容
如果匹配不成功,则继续往下匹配。
2.2.使用秘密登录验证访问
2.2.1.准备一个密码文件
[root@web01 mirror]# yum install httpd-tools -y
[root@web01 mirror]# htpasswd -cb /etc/nginx/auth_pass oldxu 123456
2.2.2给mirror.oldxu.com 的 /repo 添加用户密码认证(可以添加到全局也可以添加到location)
auth_basic “hello”;
auth_basic_user_file “/etc/nginx/auth_pass”;
3.访问权限
请求限制 limit_req
3.1请求限制
#定义了一个10m内存空间,名称叫req_one 限制的速率是 每S 1个请求,针对的来源的IP地址
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s; rate限制速率,限制一秒钟最多一个ip请求
server {
listen 80;
server_name limit.oldxu.com;
# 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量,则返回503
limit_req zone=req_one burst=3 nodelay;
location / {
root /limit;
index index.html;
}
}
limit_req_zone
b
i
n
a
r
y
r
e
m
o
t
e
a
d
d
r
z
o
n
e
=
r
e
q
o
n
e
:
10
m
r
a
t
e
=
1
r
/
s
;
第
一
个
参
数
:
binary_remote_addr zone=req_one:10m rate=1r/s; 第一个参数:
binaryremoteaddrzone=reqone:10mrate=1r/s;第一个参数:binary_remote_addr一个变量,表示通过这个标识来做限制,限制同一客户端ip地址
第二个参数:zone=req_one:10m表示生成一个大小为10M,名为req_one的内存区域 用来存储访问的频次信息.
第三个参数:rate=1r/s表示允许相同标识的客户端的访问频次,这里限制的每秒一次
limit_req zone=req_one burst=3 nodelay;
第一个参数:zone=req_one设置使用哪个配置区域来做限制,与上面limit_req_zone里的name对应.
第二个参数:burst=3 设置一个大小为3的缓冲区,当有大量请求过来时,超过访问频次限制的请求可以先放到这个缓冲区内.
第三个参数:nodelay 超过访问频次且缓冲区也满了的时候,则会返回503,如果没有设置,则所有请求会等待排队.
3.2连接限制limit_conn 限制下载速度 访问速度
[root@web01 mirror]# cat /etc/nginx/conf.d/limit.oldxu.com.conf
#请求限制:定义了一个10m内存空间,名称叫req_one 限制的速率是 每S 1个请求,针对的来源的IP地址
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
#连接限制: 定义了一个10m内存空间,名称叫conn_od,针对的是来源的IP
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server {
listen 80;
server_name limit.oldxu.com;
limit_conn conn_od 2; #连接限制,限制两个
limit_rate_after 100m; #先让其最快速下载100m,然后开始限速
limit_rate 200k; #限速
location / {
root /limit;
index index.html;
}
}
4.综合案例
限制web服务器请求数处理为1秒一个,触发值为5、 #请求限制
限制用户仅可同时下载一个文件。 #连接限制
当下载超过100M则限制下载速度为500k。如果同时下载超过2个视频,则返回提示 “请联系oldxu进行会员充值”。
[root@web01 mirror]# cat /etc/nginx/conf.d/limit.oldxu.com.conf
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server {
listen 80;
server_name limit.oldxu.com;
charset utf-8;
limit_req zone=req_one burst=5 nodelay; #请求限制
limit_conn conn_od 1; #连接限制
limit_rate_after 100m; #100m不限速
limit_rate 500k; #限速500k
location / {
root /limit;
index index.html;
}
#错误接收 @err是一个特殊的重定向
error_page 503 @err;
location @err {
default_type text/html;
return 200 '请联系管理员充值,联系qq:552408925';
}
}
5.状态模块stub_stauts
5.1监控nginx连接
5.2监控nginx请求
4.3模拟长连接和短连接效果
#开启Nginx的状态监控
location /status {
stub_status;
}
提供以下状态信息 7种状态指示
状态 含义
Active connections 当前活跃连接数,包括Waiting等待连接数。
accepts 已接收的总TCP连接数量。
handled 已处理的TCP连接数量。
requests 当前总http请求数量。
Reading 当前读取的请求头数量。
Writing 当前响应的请求头数量。
Waiting 当前等待请求的空闲客户端连接数
6.location匹配
6.1location匹配规则
#匹配符 匹配规则 优先级
#= 精确匹配 1 必须是百分百匹配才行
#^~ 以某个字符串开头 2
#~ 区分大小写的正则匹配 3
#~* 不区分大小写的正则匹配 4
#/ 通用匹配,任何请求都会匹配到 5
6.2location优先级:
The “/” request will match configuration A,
the “/index.html” request will match configuration B
the “/documents/document.html” request will match configuration C,
the “/images/1.gif” request will match configuration D,
the “/documents/1.jpg” request will match configuration E.
[root@web01 mirror]# cat /etc/nginx/conf.d/location.oldxu.com.conf
server {
listen 80;
server_name location.oldxu.com;
location = / {
default_type text/html;
return 200 'location = /';
}
location / {
default_type text/html;
return 200 'location /';
}
location /documents/ {
default_type text/html;
return 200 'location /documents/';
}
location ^~ /images/ {
default_type text/html;
return 200 'location ^~ /images/';
}
location ~* \.(gif|jpg|jpeg)$ {
default_type text/html;
return 200 'location ~* \.(gif|jpg|jpeg)';
}
}
6.3location @ 内部重定向
location @err {
default_type text/html;
return 200 ‘你可能是不小心走丢了。’;
}
7.nginx日志
7.1日志格式log_format
7.2访问日志access_log
7.3错误日志error_log
log_format: 定义日志格式:
关键字 变量
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
访问日志如何调用
access_log /var/log/nginx/access.log main;
$remote_addr # 记录客户端IP地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录ISO8601标准格式下的本地时间
$request # 记录请求的方法以及请求的http协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数
$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端IP地址
$request_length # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
access_log 可以定义在 http、server、location?
每一个server都会定义一个access_log。为了区分网站的访问记录
http {
access_log /var/log/nginx/access.log main;
server {
# access_log /var/log/nginx/log.oldxu.com.log main;
}
#如果某个网站不想记录日志,则可以通过如下两种方式去实现?
server {
access_log off;
#access_log /dev/null;
}
}
error_log:错误日志
error_log /var/log/nginx/error.log warn; #几乎是所有Nginx统一的一个位置。(全局,作用于所有的网站)
如果Nginx在访问的时候没有达到你的预期,请检查错误日志,看是否能发现什么蛛丝马迹,如果发现不了,请截图出来。
常用模块:
autoindex
allow、deny ip访问控制
auth_basic 密码登录验证
limit_req 请求限制
limit_conn 连接限制
stub_status 监控状态
location 匹配规则
log 日志格式