Nginx管理模块
配置nginx官方yum源
http://nginx.org/en/linux_packages.html#RHEL-CentOS \\Nginx官方配置地址
vim /etc/yum.repos.d/nginx.repo
添加:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出
yum -y install nginx #yum安装Nginx
nginx -V #查看nginx默认模块
nginx新版本的配置文件
全局配置文件:/etc/nginx/nginx.conf
虚拟主机配置:/etc/nginx/conf.d/*.conf
例子1:使用域名搭建一台虚拟主机
mkdir /www #创建www网页目录
复制网页代码到/www目录下
vim /etc/nginx/conf.d/www.conf
添加:
server {
listen 80;
server_name www.yundong.com;
location / {
root /www;
index index.html index.htm;
}
}
保存退出
systemctl restart nginx #重启Nginx
客户端修改/etc/hosts、访问测试(hosts为本地解析)
vim /etc/hosts
添加:
192.168.1.102 www.yundong.com
保存退出
测试:http://www.yundong.com
例子2在www网站下,创建download下载目录,索引显示
nginx目录索引(autoindex自动索引模块)
nginx默认不起用目录索引,更不允许列出网目录提供下载。
Syntax: autoindex on | off; 索引功能的开或关
Default: autoindex off; 默认关闭
Context: http, server, location 场景:全局、某个虚拟主机、某个虚拟主机的目录
mkdir /www/download #创建download下载目录
复制文件到/www/download目录下
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; #显示文件创建时间
}
保存退出
systemctl reload nginx #重启Nginx
客户端测试访问:http://www.yundong.com/download
例子:针对www网站,启用状态化追踪
nginx状态监控(status模块)
Syntax: stub_status; 启用状态化追踪
Default: — 默认关闭
Context: server, location 场景:
vim /etc/nginx/conf.d/www.conf
在server字段中添加:
location /status {
stub_status; #启用状态化追踪
access_log off; #关闭status的日志记录
}
保存退出
systemctl reload nginx
客户端访问:http://www.yundong.com/status
客户端显示结果如下:
Active connections: 1 当前活跃的连接数
server accepts 19 当前的总tcp连接数
handled 19 成功的连接数
requests 486 总HTTP请求数
例子:仅允许内部网段或vpn访问status
nginx基于ip的访问控制(access模块)
vim /etc/nginx/conf.d/www.conf
修改为:
location /status {
stub_status;
access_log off;
allow 192.168.1.0/24; 仅允许1.0网段访问
deny all; 拒绝其他所有
}
例子:设置访问/status,用户密码验证
nginx基于用户的访问控制(auth模块)
yum -y install httpd-tools htpasswd -b -c /etc/nginx/.auth_conf webadmin 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的访问限制***
limit_conn_module 连接频率限制
vim /etc/nginx/conf.d/www.conf
http {
limit_conn_zone $binary_remote_addr zone=addr:10m; #创建zone区域名为addr,大小10m,保存客户端的二进制ip
server {
location /download/ {
limit_conn addr 1; #一个ip同一时间点只允许建立一个连接
}
}
}
例子:limit_req_module 请求频率限制
vim /etc/nginx/conf.d/www.conf
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #创建zone区域名为one,大小10m,保存客户端的二进制ip,限制请求速率每秒1次
server {
location /download {
limit_req zone=one burst=5; #调用请求速率区域,另外设置额外突发5次
}
}
}