Nginx服务器以轻量化,速度快著称,但是Nginx的默认设置并没有针对具体的硬件进行调优。在这篇文章中,我们要把Nginx的性能发挥到极限,更高的提高服务器的性能。
一.优化Nginx并发量
1 2 3 | [root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/ Benchmarking 192.168.4.5 (be patient) socket: Too many open files (24) //提示打开文件数量过多 |
修改Nginx配置文件,增加并发量
1 2 3 4 5 6 7 8 9 | [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf .. .. worker_processes 2; //与CPU核心数量一致 events { worker_connections 65535; //每个worker最大并发连接数 use epoll; } .. .. [root@proxy ~]# nginx -s reload |
二.优化Linux内核参数(最大文件数量)
1 2 3 4 5 6 7 8 9 | [root@proxy ~]# ulimit -a //查看所有属性值 [root@proxy ~]# ulimit -Hn 100000 //设置硬限制(临时规则) [root@proxy ~]# ulimit -Sn 100000 //设置软限制(临时规则) [root@proxy ~]# vim /etc/security/limits.conf .. .. * soft nofile 100000 * hard nofile 100000 #该配置文件分4列,分别如下: #用户或组 硬限制或软限制 需要限制的项目 限制的值 |
优化后测试服务器并发量
1 | [root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/ |
三.优化Nginx数据包头缓存
1 2 3 4 5 6 7 8 9 10 11 | [root@proxy ~]# cat lnmp_soft/buffer.sh #!/bin/bash URL=http://192.168.4.5/index.html? for i in {1..5000} do URL=${URL}v$i=$i done curl $URL //经过5000次循环后,生成一个长的URL地址栏 [root@proxy ~]# ./buffer.sh .. .. <center><h1>414 Request-URI Too Large</h1></center> //提示头部信息过大 |
修改Nginx配置文件,增加数据包头部缓存大小
1 2 3 4 5 6 7 8 | [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf .. .. http { client_header_buffer_size 1k; //默认请求包头信息的缓存 large_client_header_buffers 4 4k; //大请求包头部信息的缓存个数与容量 .. .. } [root@proxy ~]# nginx -s reload |
四.对页面进行压缩处理
1 2 3 4 5 6 7 8 9 | [root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf http { .. .. gzip on; //开启压缩 gzip_min_length 1000; //小文件不压缩 gzip_comp_level 4; //压缩比率 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; //对特定文件压缩,类型参考mime.types .. .. |
五.服务器内存缓存
1 2 3 4 5 6 7 8 9 | http { open_file_cache max=2000 inactive=20s; open_file_cache_valid 60s; open_file_cache_min_uses 5; open_file_cache_errors off; //设置服务器最大缓存2000个文件句柄,关闭20秒内无请求的文件句柄 //文件句柄的有效时间是60秒,60秒后过期 //只有访问次数超过5次会被缓存 } |
六.浏览器本地缓存静态数据
1 2 3 4 5 6 7 8 9 10 11 12 | [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { expires 30d; //定义客户端缓存时间为30天 } } [root@proxy ~]# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html [root@proxy ~]# nginx -s reload |
七、Nginx的I/O配置优化
sendfile
当一个程序需要传输文件时,Linux内核首先将文件数据缓冲,然后将文件数据传送给程序缓冲,最后程序将文件数据传输到目的地。Sendfile方法是一种数据传输的更高效的方法,数据在内核中的文件描述符之间传输,而不需要将数据传输给程序缓冲。这种方法的结果是改善了对操作系统资源的利用。
我们可以用sendfile directive来启用sendfile方法,在http,server,location三个模块都可以定义。
http {
sendfile on ;
}
默认情况下,sendfile 的值是on。
Direct I/O
通常,Linux内核会尝试优化和缓存读写请求。数据缓存在内核里,将来任何的读取相同数据的请求会变得更快,因为不需要从缓慢的硬盘中读取数据。
Direct I/O是文件系统的一个功能,它允许程序绕过内核缓存,直接在硬盘上读写数据,这可以充分利用CPU频数,改善缓存的有效性。Directo I/O适用于访问率少的数据。这些数据不需要缓存在任何位置。我们可以用directio directive来启用这一功能,在http, server和location当中定义。
location ideo/ {
directio 4m;
}
在上面的设置中,任何大于4M的文件都将以Direct I/O的形式直接从硬盘读取。默认directio没有启用。
如果某个请求是通过directo I/O,那么这个请求不能使用sendfile功能。
Direct I/O取决于硬盘的块大小。Nginx可以使用directio_alignment directive来设置块大小,在http, server, location中定义
location ideo/ {
directio 4m;
directio_alignment 512;
}
512字节适用于大多数情况。如果Linux文件系统是XFS,那么块大小应该为4KB。
异步I/O
异步I/O允许一个进程在不阻塞和等待的情况下进行I/O操作。
aio在http, server和location中定义。Linux2.6.22+和FreeBSD4.3支持aio。
location /data {
aio on;
}
默认情况下,aio是关闭的。在Linux发行版上,要先启用directio后才能启用aio。在FreeBSD上,必须先关闭sendfile才能让aio生效。
如果Nginx没有–with-file-aio这个模块,那么启用aio会导致unknown directive aio错误。
aio可以指定线程数,这个多线程功能只在Linux发行版上才可用,并且只能在epoll, kqueue 或 eventport方法下可用。
为了使用多线程,在编译Nginx时要添加–with-threads选项。然后在/etc/nignx/nignx.conf文件中使用一个全局设置。
thread_pool io_pool threads=16;
http {
....
location /data {
sendfile on;
aio threads=io_pool;
}
}
综合I/O设置
这三个directive可以综合起来实现不同的目标。在下面的配置中,如果文件的大小小于directio指定的大小,那么使用sendfile功能。以异步I/O的方式读取directio服务的文件。
location /archived-data/ {
send file on;
aio on;
directio 4m;
}
sendfile
当一个程序需要传输文件时,Linux内核首先将文件数据缓冲,然后将文件数据传送给程序缓冲,最后程序将文件数据传输到目的地。Sendfile方法是一种数据传输的更高效的方法,数据在内核中的文件描述符之间传输,而不需要将数据传输给程序缓冲。这种方法的结果是改善了对操作系统资源的利用。
我们可以用sendfile directive来启用sendfile方法,在http,server,location三个模块都可以定义。
http {
sendfile on ;
}
默认情况下,sendfile 的值是on。
Direct I/O
通常,Linux内核会尝试优化和缓存读写请求。数据缓存在内核里,将来任何的读取相同数据的请求会变得更快,因为不需要从缓慢的硬盘中读取数据。
Direct I/O是文件系统的一个功能,它允许程序绕过内核缓存,直接在硬盘上读写数据,这可以充分利用CPU频数,改善缓存的有效性。Directo I/O适用于访问率少的数据。这些数据不需要缓存在任何位置。我们可以用directio directive来启用这一功能,在http, server和location当中定义。
location ideo/ {
directio 4m;
}
在上面的设置中,任何大于4M的文件都将以Direct I/O的形式直接从硬盘读取。默认directio没有启用。
如果某个请求是通过directo I/O,那么这个请求不能使用sendfile功能。
Direct I/O取决于硬盘的块大小。Nginx可以使用directio_alignment directive来设置块大小,在http, server, location中定义
location ideo/ {
directio 4m;
directio_alignment 512;
}
512字节适用于大多数情况。如果Linux文件系统是XFS,那么块大小应该为4KB。
异步I/O
异步I/O允许一个进程在不阻塞和等待的情况下进行I/O操作。
aio在http, server和location中定义。Linux2.6.22+和FreeBSD4.3支持aio。
location /data {
aio on;
}
默认情况下,aio是关闭的。在Linux发行版上,要先启用directio后才能启用aio。在FreeBSD上,必须先关闭sendfile才能让aio生效。
如果Nginx没有–with-file-aio这个模块,那么启用aio会导致unknown directive aio错误。
aio可以指定线程数,这个多线程功能只在Linux发行版上才可用,并且只能在epoll, kqueue 或 eventport方法下可用。
为了使用多线程,在编译Nginx时要添加–with-threads选项。然后在/etc/nignx/nignx.conf文件中使用一个全局设置。
thread_pool io_pool threads=16;
http {
....
location /data {
sendfile on;
aio threads=io_pool;
}
}
综合I/O设置
这三个directive可以综合起来实现不同的目标。在下面的配置中,如果文件的大小小于directio指定的大小,那么使用sendfile功能。以异步I/O的方式读取directio服务的文件。
location /archived-data/ {
send file on;
aio on;
directio 4m;
}
以上七点就是关于香港服务器Linux系统性能优化的分享