day 41 Nginx进阶

Nginx 进阶

不是太重要:

1.索引模块
2.限速模块
3.认证模块
4.状态模块
5.yum仓库

非常重要:

1.location
2.rewrite

第一章 索引模块

1.官方地址
http://nginx.org/en/docs/http/ngx_http_index_module.html
2.配置说明
Syntax:	autoindex on | off;
Default: autoindex off;
Context: http, server, location

http {
  server {
    location {
	  autoindex on;
	}
  }
}

3.配置流程

第1步: 创建配置文件并重启服务
cat > /etc/nginx/conf.d/download.conf << 'EOF'
server {			
    listen       80;
    server_name  www.download.com;
    location / {
        autoindex on;
        root   /code/download;
        index  index.html;
    }
}
EOF
nginx -t
systemctl restart nginx

第2步: 创建目录和文件

mkdir /code/download
cd /code/download
touch {1..3}.txt

第3步 电脑浏览器测试

10.0.0.7

4.支持中文显示

server {			
    listen       80;
    server_name  www.download.com;
    location / {
        charset utf-8;
        autoindex on;
        root   /code/download;
        index  index.html;
    }
}

5.以系统时间为准

server {			
    listen       80;
    server_name  www.download.com;
    location / {
        charset utf-8;
        autoindex on;
		autoindex_localtime on;
        root   /code/download;
        index  index.html;
    }
}

6.以人类可读的形式显示文件大小

server {			
    listen       80;
    server_name  www.download.com;
    location / {
        charset utf-8;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
        root   /code/download;
        index  index.html;
    }
}


第二章 监控模块

1.官方地址

http://nginx.org/en/docs/http/ngx_http_stub_status_module.html

2.配置流程

server {			
    listen       80;
    server_name  www.download.com;
    location / {
        charset utf-8;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
        root   /code/download;
        index  index.html;
    }
   
    location /status {
        stub_status;
    }
}

3.监控指令解释

Active connections # 当前活动的连接数
accepts # 当前的总连接数 TCP
handled # 成功的连接数 TCP
requests # 总的 http 请求数
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了 keepalive

第3章 Nginx 基于IP的访问控制

1.指令解释

deny	拒绝
allow   允许

2.注意

只要有一条规则匹配上,下面的规则都不会再匹配了

3.配置文件

server {			
    listen       80;
    server_name  www.download.com;
    location / {
        allow 192.168.4.9;
        deny all;

        charset utf-8;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
        root   /code/download;
        index  index.html;
    }
}

第四章 Nginx 请求限制

1.官方地址

http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

2.配置语法

定义一条规则

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

参数解释:

limit_req_zone 					#引用限速模块
$binary_remote_addr 			#判定条件,每个请求的IP
zone=one:10m 					#定义一个zone名称
rate=1r/s;						#限制速度,1秒1次

引用一条限速规则

limit_req zone=two burst=5 nodelay;

limit_req 					#引用限速规则语法
zone=one 					#引用哪一条规则
burst=5 					#令牌桶,允许排队的数量
nodelay;					#如果不希望在请求被限制时延迟过多的请求,则应使用参数nodelay

3.配置文件

[root@web-7 ~]# cat /etc/nginx/conf.d/linux7.conf 
#定义限速规则
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

server {			
    listen       80;
    server_name  www.linux7.com;
	limit_req zone=one burst=5 nodelay;
    location / {
        
        root   /code/linux7;
        index  index.html;
    }
}

4.测试

for i in {1..100};do curl -sI 127.0.0.1|head -1;sleep 1;done
for i in {1..100};do curl -sI 127.0.0.1|head -1;sleep 0.5;done
for i in {1..100};do curl -sI 127.0.0.1|head -1;sleep 0.2;done

第五章 Nginx 实现内网yum仓库

1.为什么需要私有YUM仓库
下载速度慢
需要有外网
有些Base源和epel源软件没有,需要单独创建下载源

2.需要的软件
createrepo
nginx

3.配置nginx索引模块

cat >/etc/nginx/conf.d/yum.conf <<EOF
server {
    listen       80;
    server_name  yum.mysun.com;
    location / {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;
        charset utf-8,gbk;
        root   /data/yum;
        index  index.html;
    }
}
EOF

4.准备软件仓库目录
mkdir /data/yum -p

5.下载需要的软件
yum install --downloadonly --downloaddir=/data/yum mariadb-server nginx -y

6.下载制作yum仓库的软件并生成yum元数据
yum install createrepo -y
createrepo /data/yum

7.客户端生成本地的repo文件

cat >/etc/yum.repos.d/local.repo <<EOF
[local]
name=local
enable=1
gpgcheck=0
baseurl=http://172.16.1.61
EOF

8.更新软件流程

服务端:

yum install --downloadonly --downloaddir=/data/yum redis -y 
createrepo --update /data/yum/

客户端:

yum makecache fast
yum install redis -y

总结:

以后yum仓库每增加一个软件都需要更新一下目录的索引信息
客户端每次安装之前最好先更新一下yum缓存

第二种方法:

1.配置yum保存软件包
vim /etc/yum.conf
keepcache=1

2.正常yum安装软件

3.将保存下来的RPM包发送到m-61上

4.m-61上更新rpm包索引

5.客户端更新yum缓存

6.客户端安装软件

posted @ 2021-11-26 21:40  zhaocheng690  阅读(32)  评论(0编辑  收藏  举报