1.优化worker进程个数
查看cpu总核数
查看CPU总颗数:
2.访问日志配置实战
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| sed -n '21,23 s/#//gp' ../conf/nginx.conf.default log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
vim nginx.con error_log logs/error.log; ... http { ... log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; ... }
|
Nginx访问日志轮询切割
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #!/bin/sh Dateformat=`date +%Y%m%d` Basedir="/application/nginx" Nginxlogdir="$Basedir/logs" Logname="access" [ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1 [ -f ${Logname}.log ]|| exit1 /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log $Basedir/sbin/nginx -s reload
cat >> /var/spool/cron/root <<EOF #cut nginx access log by cwzhou 00 00 * * * /bin/sh /server/script/cut_nginx_log.sh > /dev/null 2>&1 EOF
|
Nginx并发连接数量
Reference ngx_http_limit_conn_module
1.限制单ip并发连接数
1 2 3 4 5 6 7 8 9 10 11 12 13
| vim nginx.conf ... http{ ... limit_conn_zone $binary_remote_addr zone=addr:10m; server{ ... location / { ... limit_conn addr 1; } } }
|
1 2
| ab -c 1 -n 10 http://10.0.0.3/
|
应用场景之一是服务器下载:
1 2 3
| location /download/ { limit_conn add 1; }
|
2.限制虚拟主机总连接数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| vim nginx.conf ... http{ ... limit_conn_zone $binary_remote_addr zone=addr:10m; limit_conn_zone $server_name zone=perserver:10m;
server{ ... location / { ... limit_conn perserver 2; } } }
|