架构篇---4---Nginx管理模块

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次
			}
		}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值