WEB服务与NGINX(9)-NGINX作为下载服务器的相关配置


目录


1. NGINX的目录索引功能

  • autoindex on | off;

    Default: autoindex off;

    Context: http, server, location

    启用或禁用目录列表功能,on代表启用。

    当访问一个目录时(以/结尾),配置的index的默认页面不存在,会将请求传递给ngx_http_autoindex_module模块处理,该模块会处理以“/”结尾的请求,并以目录的形式展示给客户端,以供客户端下载。

  • autoindex_exact_size on | off;

    Default: autoindex_exact_size on;

    Context: http, server, location

    指定是否在目录列表中输出确切的文件大小,on以字节为单位显示,off显示易读的大小。

  • autoindex_localtime on | off;

    Default: autoindex_localtime off;

    Context: http, server, location

    指定目录列表中显示的时间是本地的时间还是GMT(格林威治)时间,on表示本地时区,off表示格林威治时区。

示例:搭建企业内部yum仓库

#1.服务器端配置
[root@nginx01 web1]# cat /etc/nginx/conf.d/virtualhost.conf
server {
	listen 80;
	server_name www.nginx01.com;
	charset utf-8,gbk;               <==设定字符集,防止中文字符出现乱码。

	location / {
		root /data/nginx/html/web1;
		index index.html;
	}
        
	location /repo {                  <==搭建repo仓库,不能配置index默认返回文件
		autoindex on;              
		autoindex_exact_size off;     
		autoindex_localtime on;
		alias /misc/cd;
	}
}

#2.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service

客户端测试:

客户端使用windows,首先要在hosts文件中增加域名解析,hosts文件的路径为C:\Windows\System32\drivers\etc,文件增加192.168.20.20 www.nginx01.com行,然后在浏览器进行测试访问http://www.nginx01.com/repo/进行测试。

WEB服务与NGINX(9)-NGINX作为下载服务器的相关配置

WEB服务与NGINX(9)-NGINX作为下载服务器的相关配置

2. NGINX的限速功能

nginx有三种限速场景:

  1. 下载限速:限制用户的下载速度,使用Nginx ngx_http_core_module模块实现
  2. 请求限制:限制用户单位时间内所产生的http连接数,使用Nginx ngx_http_limit_req_module实现
  3. 连接限制:限制同一时间的连接数,以及并发连接数的限制,使用Nginx ngx_http_limit_conn_module实现

2.1 限制下载速度

  • limit_rate rate;

    限制响应给客户端的传输速率,单位是bytes/second,默认值0表示无限制。

    支持环境:http, server, location, if in location

    Nginx ngx_http_core_module模块实现

    场景示例:搭建的镜像站点限制用户的下载速度,一开始对用户不限速,当下载达到3M时,限制用户的下载速度为10Kbps。

    #1.服务端配置文件
    [root@nginx01 web1]# cat /etc/nginx/conf.d/virtualhost.conf 
    server {
    	listen 80;
    	server_name www.nginx01.com;
    	charset utf-8,gbk;
    
    	location / {
    		root /data/nginx/html/web1;
    		index index.html;
    	}
            
    	location /repo {
    		autoindex on;
    		autoindex_exact_size off;
    		autoindex_localtime on;
    		alias /misc/cd;
    		limit_rate_after 3m;      <==下载文件3m之前不限速
    		limit_rate 10k;
    	}
    }
    
    #2.重启nginx服务
    [root@nginx01 web1]# systemctl reload nginx.service
    
    #3,客户端下载测试,开始的3M不限速,3M过后开始限速10Kbps
    [root@xuzhichao ~]# wget http://www.nginx01.com/repo/Packages/valgrind-3.15.0-11.el7.x86_64.rpm
    --2021-06-16 23:09:53--  http://www.nginx01.com/repo/Packages/valgrind-3.15.0-11.el7.x86_64.rpm
    Resolving www.nginx01.com (www.nginx01.com)... 192.168.20.20
    Connecting to www.nginx01.com (www.nginx01.com)|192.168.20.20|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 12145800 (12M) [application/x-redhat-package-manager]
    Saving to: ‘valgrind-3.15.0-11.el7.x86_64.rpm’
    37% [===================>                                 ] 4,612,096   10.0KB/s  eta 3m 54s
    

2.2 限制单位时间内产生的http请求数

  • limit_req_zone key zone=name:size rate=rate;

    Default: —

    Context: http

  • limit_conn zone number [burst=number] [nodelay];

    Default: —

    Context: http, server, location

    场景示例:基于来源IP对请求速率限制,限制每个客户端ip每秒只能处理一个请求,可以突发超过2个请求放入缓存区,后续的请求返回503。

    #1.nginx配置文件如下:
    [root@nginx01 web1]# cat /etc/nginx/conf.d/virtualhost.conf
    limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;   <==只能定义在http语句块中
    
    server {
    	listen 80;
    	server_name www.nginx01.com;
    
        limit_req zone=req_one burst=2 nodelay;
    	charset utf-8,gbk;
    
    	location / {
    		root /data/nginx/html/web1;
    		index index.html;
    	}
    
    	error_page 500 502 503 504 404 /errorpage.html;
    	location = /errorpage.html {
    		root /data/nginx/html/web1/errorpage/;
    	}       
    }
    
    #注意:
    #limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
    #第一个参数:,$binary_remote_addr表示通过这个标识来做限制,限制同一客户端ip地址,$binary_remote_addr变量适用于ipv4地址。
    #第二个参数:zone=reqone:10m表示生成一个大小为10M,名为req_one的内存区域,用来存储访问的频次信息。
    #第三个参数:rate=1r/s表示允许相同标识的客户端的访问频次,这里限制的是每秒1次
    
    #limit_req zone=req_one burst=2 nodelay;
    #第一个参数:zone=req_one设置使用哪个配置区域来做限制,与上面Limit_req_zone 里的name对应
    #第二个参数:burst=2,设置一个大小为2的缓冲区,当有大量请求过来时,超过了访问频次限制的请求可以先放到这个缓冲区内。
    #第三个参数:nodelay,超过访问频次并且缓冲区也满了的时候,则会返回503,如果没有设置,则所有请求会等待排队
    
    #2.重启nginx服务
    [root@nginx01 web1]# systemctl reload nginx.service
    
    #3.客户端进行测试,发现超过3个请求后后续的请求都返回503,503被重定向到自定义的错误页中。
    [root@xuzhichao ~]# for i in {1..10} ;do curl http://www.nginx01.com/;done 
    www.nginx01.com
    www.nginx01.com
    www.nginx01.com
    our system is being maintained
    our system is being maintained
    our system is being maintained
    our system is being maintained
    our system is being maintained
    our system is being maintained
    our system is being maintained
    

2.3 限制客户端同一时刻的并发连接数

  • limit_conn_zone key zone=name:size;

    Default: —

    Context: http

  • limit_conn zone number;

    Default: —

    Context: http, server, location

    语法和limit_req_zone key zone=name:size相似。

    场景示例:设置共享内存区域和单个IP最大的允许的并发连接数为2,超过限制时,返回503错误。

    [root@nginx01 web1]# cat /etc/nginx/conf.d/virtualhost.conf
    limit_conn_zone $binary_remote_addr zone=conn_one:10m;
    
    server {
    	listen 80;
    	server_name www.nginx01.com;
    
    	limit_conn conn_one 2;
    	charset utf-8,gbk;
    
    	location / {
    		root /data/nginx/html/web1;
    		index index.html;
    	}
    
    	error_page 500 502 503 504 404 /errorpage.html;
    	location = /errorpage.html {
    		root /data/nginx/html/web1/errorpage/;
    	}
    }
    
上一篇:处理nginx 文件服务器 ,log文件中文乱码


下一篇:Nginx设置访问服务器某个目录