高并发大流量站点nginx,动不动就千万级PV及百万级的并发,那么nginx+PHP环境下我们如何做好优化?
分享高并发大流量站点nginx的PHP优化:
禁用或者优化access_log
大流量访问时,较大的访问会导致访问日志对磁盘的读写非常大。如果不需要日志的话,可以禁用掉
access_log off;
log_not_found off;
// 或者打开缓冲
access_log /var/log/nginx/access.log main buffer=32k;
1
2
3
4
5
access_logoff;
log_not_foundoff;
// 或者打开缓冲
access_log/var/log/nginx/access.logmainbuffer=32k;
打开Gzip
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
1
2
3
4
5
6
7
8
9
gzipon;
gzip_min_length1k;
gzip_buffers416k;
gzip_http_version1.0;
gzip_comp_level2;
gzip_typestext/plaintext/cssapplication/jsonapplication/x-javascripttext/xmlapplication/xmlapplication/xml+rsstext/javascript;
gzip_varyon;
gzip_proxiedexpiredno-cacheno-storeprivateauth;
gzip_disable"MSIE [1-6]\.";
TCP sockets 与Unix sockets
Unix sockets比TCP sockets提供更好一些的性能(因为I/O数据读写少,上下文切换少)。
upstream backend
{
server unix:/var/run/fastcgi.sock;
# server 127.0.0.1:8080;
}
1
2
3
4
5
upstreambackend
{
serverunix:/var/run/fastcgi.sock;
# server 127.0.0.1:8080;
}
优化输出缓存
fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_connect_timeout 3s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
1
2
3
4
5
6
7
fastcgi_buffers25616k;
fastcgi_buffer_size128k;
fastcgi_connect_timeout3s;
fastcgi_send_timeout120s;
fastcgi_read_timeout120s;
fastcgi_busy_buffers_size256k;
fastcgi_temp_file_write_size256k;
优化worker processes
nginx是多进程的而不是多线程的,对于进程相关的配置我们需要如下优化。
首先看一下服务器的处理器数。
cat /proc/cpuinfo | grep processor
1
cat/proc/cpuinfo|grepprocessor
然后设置worker_processes,进程数为得到的处理器数。 worker_connections 是每一个进程可以打开的最大连接数,可以调大一些。下面给一个参考。
# We have 16 cores
worker_processes 16;
# connections per worker
events
{
worker_connections 4096;
multi_accept on;
}
1
2
3
4
5
6
7
8
9
# We have 16 cores
worker_processes16;
# connections per worker
events
{
worker_connections4096;
multi_accepton;
}
原创文章,作者:PHP面试题,如若转载,请注明出处:http://www.phpmianshiti.com/youhua/117/