实验环境:初始快照,关闭防火墙和selinux,ping通外网
在nginx基础上(使用域名成功访问nginx网页)
实验步骤:
- nginx目录索引(autoindex自动索引模块)
nginx默认不起用目录索引,更不允许列出网站目录提供下载。
Syntax: autoindex on | off; 索引功能的开或关
Default: autoindex off; 默认关闭
Context: http, server, location 场景:全局、某个虚拟主机、某个虚拟主机的目录
例:
在www网站下,创建download下载目录,索引显示
- 创建下载目录
mkdir -p /www/download
- 进入下载目录
cd /www/download
- 创建三个目录(测试的时候可以查看到以下三个目录)
mkdir yun71
mkdir yun72
mkdir yun73
- 编辑虚拟主机配置
vim /etc/nginx/conf.d/www.conf
在server字段中添加:
location /download {
root /www;
autoindex on; 启用索引显示
charset utf-8,gbk; 字符编码为中文
autoindex_exact_size on; 显示文件大小
autoindex_localtime on; 显示文件创建时间
}
autoindex_exact_size off; //默认为on,显示出文件的确切大小,单位bytes。 修改为off,显示出文件大概大小,单位是KB或者MB或者GB
autoindex_localtime on; //默认为off,显示的文件时间为GMT时间。修改为no,显示的文件时间为文件服务器时间
charset utf-8,gbk; // 默认中文目录乱码,解决乱码
保存退出

- 重新加载nginx配置文件,使更改生效
sudo nginx -s reload
- 客户端访问测试
http://www.yun71.com/download/

- nginx状态监控(status模块)
- 通过nginx -V来查看是否有with-http_stub_status_module该模块。
Syntax: stub_status; 启用状态化追踪
Default: — 默认关闭
Context: server, location 场景:
- 针对www网站,启用状态化追踪
vim /etc/nginx/conf.d/www.conf
在sever字段中添加:
location /status {
stub_status;
access_log off;
}
- 重新加载nginx配置文件,使更改生效
sudo nginx -s reload
- 客户端访问

客户端显示结果如下:
Active connections: 1 当前活跃的连接数
server accepts 19 当前的总tcp连接数
handled 19 成功的连接数
requests 486 总HTTP请求数
- 在测试机使用ab压力工具测试(测试服务器的负载压力)
- 安装测试工具
yum -y install httpd-tools
或者使用rpm
- 挂载光盘
mount /dev/cdrom /media/
cd /media/Packages/
- Rpm安装过测试工具
rpm -ivh httpd-tools-2.4.6-45.el7.centos.x86_64.rpm
- ab -c 1000 -n 10000 http://www.yun71.com/

-n:在测试会话中所执行的请求个数,默认仅执行一个请求
-c:一次产生的请求个数,默认是一次一个
- Nginx基于IP的访问控制(access模块)
- 仅允许内部网段或vpn访问status
vim /etc/nginx/conf.d/www.conf
修改:
location /status {
stub_status;
access_log off;
allow 192.168.8.0/24;
deny all;
}

- 重新加载nginx配置文件,使更改生效
sudo nginx -s reload
- 客户端访问
http://www.yun71.com/status

- 修改配置文件
vim /etc/nginx/conf.d/www.conf
将仅允许8.0网段改为9.0网段再次测试

- 重新加载nginx配置文件,使更改生效
sudo nginx -s reload
- 客户端访问
注意:出现403 Forbidden 表示你对这个资源没有权限
- Nginx基于用户的访问控制(auth模块)
- 设置访问/status,用户密码验证
安装接口调用工具
yum -y install httpd-tools
- 创建用户名为admin密码为123456并写入文件
htpasswd -b -c /etc/nginx/.auth_conf admin 123456
- 修改虚拟配置文件
vim /etc/nginx/conf.d/www.conf
修改为:
location /status {
stub_status;
access_log off;
auth_basic "input your passwd:";
auth_basic_user_file /etc/nginx/.auth_conf;
}

- 重新加载nginx配置文件,使更改生效
sudo nginx -s reload
- 客户端测试
http://www.yun71.com/status 需要输入用户名和密码



输入正确的用户名和密码会出现以下界面

- Nginx日志格式
- 查看日志
tail /var/log/nginx/access.log

- 修改配置文件
vim /etc/nginx/nginx.conf
增加一个变量请求包长度$request_length

- 再次查看日志
tail /var/log/nginx/access.log


- 测试匹配符的优先级

- 添加域名使之能被正常解析到
Vim /etc/hosts
添加:
192.168.8.5 test.benet.com

- 进入目录
cd /etc/nginx/conf.d/
- 编辑配置文件
vim test.conf
添加:
server {
listen 80;
server_name test.benet.com;
location / {
default_type text/html;
return 200 "location /";
}
location =/ {
default_type text/html;
return 200 "location =/";
}
location ~ / {
default_type text/html;
return 200 "location ~ /";
}
location ~* / {
default_type text/html;
return 200 "location ~* /";
}
}

- 客户端访问
http://test.benet.com/

3671

被折叠的 条评论
为什么被折叠?



