nginx限流与配置管理

一、nginx限流

1.控制单IP并发连接数

//$binary_remote_addr 表示通过remote_addr这个标识来做限制

//zone=addr:10m 表示生成一个大小为10M,名字为addr的内存区域

limit_conn addr 1; #限制并发数

[root@server1 conf]# vim nginx.conf
#gzip  on;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen       80;
server_name  localhost;
 
location /download/ {
limit_conn addr 1;      ##并发数为1
}

 [root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

ab -c1 -n10 http://172.25.52.1/download/vim.jpg  ab命令 并发1次,请求10次

出现9个错误

设置队列长度

[root@server1 conf]# vim nginx.conf
 
  #gzip  on;
        limit_conn_zone $binary_remote_addr zone=addr:10m;
        limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
 
    server {
        listen       80;
        server_name  localhost;
 
        location / {
                root html;
                index index.php index.html index.htm;
 
        }
        location /download/ {
         limit_conn addr 1;
         limit_req zone=one burst=5 nodelay;  
[root@server1 conf]# nginx -s reload

 

设置带宽

[root@server1 conf]# vim nginx.conf
   #gzip  on;
        limit_conn_zone $binary_remote_addr zone=addr:10m;
        limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
 
    server {
        listen       80;
        server_name  localhost;
 
        location / { 
                root html;      
                index index.php index.html index.htm;
 
        }
        location /download/ {
         limit_conn addr 1; 
         limit_req zone=one burst=5 nodelay;
         limit_rate 50k;    ###限制带宽
         }


 
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

 

二. ​nginx配置管理

1.自动索引:下载方便

 [root@server1 conf]# vim nginx.conf
 
   #gzip  on;
        limit_conn_zone $binary_remote_addr zone=addr:10m;
        limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
 
    server {
        listen       80;
        server_name  localhost;
 
        location / {
                root html;
                index index.php index.html index.htm;
 
        }
        location /download/ {
         autoindex on;     ####自动索引
         limit_conn addr 1;
         limit_req zone=one burst=5 nodelay;
         limit_rate 50k;
         }
 
        #error_page  404              /404.html;

 2. Nginx expire缓存配置: 缓存可以降低网站带宽,加速用户访问

设置过期时间为365天

[root@server1 conf]# vim nginx.conf
   

         location /download/ {
         autoindex on;
         limit_conn addr 1;
         limit_req zone=one burst=5 nodelay;
         limit_rate 50k;
         }
 
        location ~ .*\.(gif|jpg|png)$ {      ##Nginx expire缓存配置: 缓存可以降低网站带宽,加速用户访问
            expires 365d;
             root html;
        }
[root@server1 conf]# nginx -s reload

 

 

3.日志轮询

编写脚本,添加执行权限​

[root@server1 logs]# /opt/nginx_log.sh   ##执行脚本,生成往期日志 

 

 

[root@server1 ~]# cd goaccess-1.4/

[root@server1 goaccess-1.4]# ./configure --enable-utf8 --enable-geoip=legacy

 

 

 数据可视化 

4.禁用不必要的日志记录,以节省磁盘IO的消耗 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值