目录
1. 先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
2.修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置
一、访问状态统计配置
1. 先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
2.修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置
在http配置中添加以下配置
......
http {
......
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
##添加 stub_status 配置##
location /status { #访问位置为/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
}
}
3.重启服务,访问测试
systemctl restart nginx
- 浏览器访问 http://192.168.193.50/status
- Active connections :表示当前的活动连接数;
- server accepts handled requests :表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、 已处理的请求数。
二、基于授权的访问控制
1.生成用户密码认证文件
创建nginx用户的同时给他设置密码
更改它的属主,并赋权
2.修改主配置文件相对应目录,添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加认证配置##
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
}
3.重启服务,访问测试
nginx -t
systemctl restart nginx
浏览器访问 http://192.168.193.50
三、基于客户端的访问控制
- 访问控制规则如下:
- deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
- allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
- 规则从上往下执行,如匹配则停止,不再往下匹配。
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加控制规则##
deny 192.168.80.200; #拒绝访问的客户端 IP
allow all; #允许其它IP客户端访问
}
}
systemctl restart nginx
一台主机IP为192.168.193.40 准备访问 192.168.193.50
403报错 表示禁止访问该页面
将配置文件删除后,继续访问后成功
四、基于域名的Nginx虚拟主机
1.为虚拟主机提供域名解析
echo "192.168.193.50 www.kgc.com www.benet.com" >> /etc/hosts
2.为虚拟主机准备网页文档
mkdir -p /var/www/html/benet
mkdir -p /var/www/html/kgc
echo "<h1>www.kgc.com</h1>" > /var/www/html/kgc/index.html
echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html
3.修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.kgc.com; #设置域名www.kgc.com
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /var/www/html/kgc; #设置www.kgc.com 的工作目录
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 80;
server_name www.benet.com; #设置域名www.benet.com
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
4.重启服务,访问测试
systemctl restart nginx
- 浏览器访问
- http://www.kgc.com
- http://www.benet.com
五、基于IP的Nginx虚拟主机
ifconfig ens33:0 192.168.193.51 netmask 255.255.255.0
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.193.50:80; #设置监听地址192.168.193.50
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /var/www/html/kgc;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.193.51:80; #设置监听地址192.168.193.51
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
重启服务
systemctl restart nginx
浏览器访问
http://192.168.193.50
http://192.168.193.51
六、基于端口的Nginx虚拟主机
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.193.50:8080; #设置监听 8080 端口
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /var/www/html/kgc;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.193.51:8888; #设置监听 8888 端口
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
重启服务
systemctl restart nginx
- 浏览器访问
- http://192.168.193.50:8080
- http://192.168.193.51:8888