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 的时候, 会打开目录索引列表
1) 修改配置文件
- [[email protected] conf.d]# VIM game.conf
- server {
- listen 80;
- server_name www.xiao.com;
- location / {
- root /xiao;
- index index.html;
- }
- location /img {
- root /xiao;
- autoindex on;
- charset utf-8,gbk;
- autoindex_exact_size off;
- autoindex_localtime on;
- }
- }
2) 创建文档目录
mkdir -p /xiao/img
例 2: 实现作业上传系统 (作业上传需要 PHP 实现)
1) 获取源码
- mkdir -p /xiao/zuoye
- cd /xiao/zuoye
将作业页面的源码通过 xshell 拖拽进去
unzip kaoshi.zip
2) 创建页面配置文件
- VIM zuoye.conf
- server {
- listen 80;
- server_name upload.xiao.com;
- location / {
- root /xiao/zuoye;
- index index.HTML;
- }
- }
3) 语法测试, 重载 nginx
- nginx -t
- nginx -s reload
4) 添加 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: 2
- server accepts handled requests
- 65 65 144
- Reading: 0 Writing: 1 Waiting: 1
- Active connections # 当前活动的连接数
- accepts 65 # 当前的总连接数 TCP
- handled 65 # 成功的连接数 TCP
- reques 144 # 总的 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
- // 需要安装依赖组件
- [[email protected] ~]# yum install httpd-tools -y
- [[email protected] ~]# htpasswd -b -c /etc/nginx/auth_conf xiao 123456
- 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=10*1024K=10*1024*1024B/4
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 1;
- ...
- }
Nginx 请求限制配置实战
1)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
2)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=3 nodelay;
- }
- }
3) 使用 ab 工具进行压力测试
- yum install -y httpd-tools
- VIM /etc/hosts
- 10.0.1.7 www.xiao.com
- [[email protected] ~]# ab -n 50 -c 20 http://www.xiao.com/index.html
来源: http://www.bubuko.com/infodetail-3409749.html