孙富阳, 江湖人称没人称。多年互联网运维工作经验,曾负责过孙布斯大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型博客网站运维工作。微信:s2795802440
1.如何检查nginx配置了那些模块
nginx -V
2.client_max_body_size模块
Syntax: | client_max_body_size size; |
---|
Default: | client_max_body_size 1m; |
---|
Context: | http , server , location |
---|
设置用户上传文件大小设
3.设置字符集,避免中文乱码
charset utf-8;(字符集,一般设置在main全局)
4.网页图标设置(favicon.ico)
[root@web01 ~]# cat /etc/nginx/conf.d/test.conf
server {
listen 80;
location /favicon.ico {
root /code;
index favicon.ico;
}
}
5.autoindex目录索引模块(类似阿里源的界面)
Syntax: | autoindex on | off; |
---|
Default: | autoindex off; |
---|
Context: | http , server , location |
---|
是否开启目录索引
Syntax: | autoindex_exact_size on | off; |
---|
Default: | autoindex_exact_size on; |
---|
Context: | http , server , location |
---|
是否以人类可读形式显示大小(off), on(精确显示字节)
Syntax: | autoindex_format html | xml | json | jsonp; |
---|
Default: | autoindex_format html; |
---|
Context: | http , server , location |
---|
支持的格式,暂时用不到
Syntax: | autoindex_localtime on | off; |
---|
Default: | autoindex_localtime off; |
---|
Context: | http , server , location |
---|
是否调整为本地时间,调整(on),不调整(off)
[root@web01 ~]# cat /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
server_name autoindex.com;
location / {
root /code;
index indexx.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
6.access访问控制模块
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 |
---|
禁止访问
[root@web01 ~]# cat /etc/nginx/conf.d/access.conf
server {
listen 80;
server_name access.com;
location / {
allow 10.0.0.7;####只允许10.0.0.7这个ip访问
deny all;###拒绝所有ip访问
root /code;
index indexx.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
7.auth_basic用户认证模块
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 |
---|
用户认证的密码文件
#让用户访问 www.sfy.com/status 需要输入用户名和密码
##创建nginx auth_basic_user_file 需要的密码文件
htpasswd -b -c /etc/nginx/conf.d/status.pass sfy 123456
####-b 将用户名密码写在命令行,非交互式
####-c 创建密码文件
不加-c默认追加到密码文件下方,加-c会覆盖
[root@web01 ~]# cat /etc/nginx/conf.d/access.conf
server {
listen 80;
server_name authbasic.com;
location / {
auth_basic on;
auth_basic_user_file auth_basic.pass;
root /code;
index indexx.html;
}
}
8.各种限制模块
limit_req 模块 限制请求(http)
limit_conn 模块 限制连接(tcp)
limit_rate 去core模块找 限速度
8.1limit_req 模块
Syntax: | limit_req_zone key zone=name:size rate=rate [sync]; |
---|
Default: | — |
---|
Context: | http |
---|
limit_req_zone #创建木桶空间以及木桶处理的速率,仅放在http层
Syntax: | limit_req zone=name [burst=number] [nodelay | delay=number]; |
---|
Default: | — |
---|
Context: | http , server , location |
---|
limit_req用于限制每个已定义密钥的请求处理速率,特别是来自单个IP 地址的请求的处理速率。限制是使用“漏桶”方法完成的
http {
······
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
······
#$binary_remote_addr 用户ip地址,占用空间更少
#zone=One:10m 指定空间名字:大小
#rate=1r/s 指定木桶处理速度
#不用$remote_addr,而是$binary_ remote_addr变量。 $binary_remote_addr变量的大小对于IPv4地址总是4字节,对于IPv6 地址总是16字节
server {
·····
location /search/ {
limit_req zone=one burst=5 nodelay;
#zone=one #指定limit_req_zone 创建的木桶空间,即上文定义的zone
#burst=5 #并发5
#平均每秒允许不超过1个请求,突发请求不超过5个
#nodelay #默认不加上nodelay,超过并发数后,排队(delay) #nodelay不等待,同时处理,超过并发数后,报错
}
}
}
8.2limit_conn模块
基于ip限制,每个ip地址的连接数量
ngx_http_limit_conn模块用于限制每个已定义密钥的连接数,特别是来自单个IP地址的连接数
并非所有连接都被计算在内。只有当一个连接有一个正在由服务器处理的请求 并且整个请求头已经被读取时,它才会被计数
Syntax: | limit_conn_zone key zone =name :size ;; |
---|
Default: | — |
---|
Context: | http |
---|
Syntax: | limit_conn zone number; |
---|
Default: | — |
---|
Context: | http , server , location |
---|
http层:limit_conn_zone $binary_remote_addr zone=addr:10m;
server层:limit_conn addr 1; 限制一个连接(用户下载,限制一次只能下载一个资源)
http {
limit_conn_zone $binary_remote_addr zone=addr:10m;
...
server {
...
location /download/ {
limit_conn addr 1;
}
8.3limit_rate模块
Syntax: | limit_rate rate; |
---|
Default: | limit_rate 0; |
---|
Context: | http , server , location , if in location |
---|
限制速度
Syntax: | limit_rate_after size; |
---|
Default: | limit_rate_after 0; |
---|
Context: | http , server , location , if in location |
---|
达到多少之后再限制速度
limit_rate_after 50m;下载前50M的资源不限速
limit_rate 200k;50M之后限速200k
9.stub_status状态模块
Syntax: | stub_status; |
---|
Default: | — |
---|
Context: | server , location |
---|
location = /basic_status {
stub_status;
}
stub_status | |
Active connections: 2 | #当前活动连接(已经连接的连接) |
server accepts | #已经接受的连接请求(总数) |
handled | #已经处理的连接请求(总数) |
requests | #一共向我发送了多少请求(总数) |
Reading: 0 | 当前正在读取用户请求头的数量 |
Writing: 1 | 当前正发送响应报文的数量 |
Waiting: 1 | 用户的请求,等待服务端数量(保持长连接,无请求正在等待断开连接) |
10.location匹配的优先级
Syntax: | location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... } |
---|
Default: | — |
---|
Context: | server , location |
---|
location = / {###精确匹配优先级最高
}
location ^~ /document {###匹配URL,不适用正则表达式,优先级第二
}
location ~ /documetn {###区分大小写的正则匹配优先级第三
}
location ~* /documetn {###不区分大小写的正则匹配优先级第三
}
location / {###通用匹配,其他location匹配失败,默认匹配到这里,优先级最低
}
评论已关闭