一、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的消耗