Nginx优化

Nginx以事件驱动(epoll的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡但是Nginx并不支持cgi方式运行,原因是可以减少因此带来的一些程序上的漏洞。所以必须使用FastCGI方式来执行PHP程序。

由于Nginx本身的一些优点,轻量,开源,易用,越来越多的公司使用nginx作为自己公司的web应用服务器,本文详细介绍nginx源码安装的同时并对nginx进行优化配置。

一、Nginx的优化

1、编译安装前优化

编译前的优化主要是用来修改程序名等等目的更改源码隐藏软件名称和版本号

安装zlib-develpcre-devel等依赖包

[root@CXW /]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel

下载nginx的源码包:http://nginx.org/download

解压源码包:

[root@CXW /]# tar zxf nginx-1.10.2.tar.gz 
[root@CXW /]# cd nginx-1.10.2/

隐藏软件名称和版本号

[root@CXW nginx-1.10.2]# vim src/core/nginx.h

//此行修改的是你想要的版本

#define NGINX_VERSION      "1.22.22"     //13

//此行修改的是你想修改的软件名称

#define NGINX_VER          "nginx/" NGINX_VERSION  //14

修改上面的信息,即可更改nginx显示版本。例如:

#define NGINX_VERSION      "7.0"

#define NGINX_VER          "IIS/" NGINX_VERSION

nginx优化压力测试_第1张图片

修改HTTP头信息中的connection字段,防止回显具体版本号

拓展:通用http通用头包含请求和响应消息都支持的头,通用头包含Cache-ControlConnectionDatePragmaTransfer-EncodingUpgradeVia。对通用头的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头,一般将会作为实体头处理。那么也就是说有部分设备,或者是软件,能获取到connection,部分不能,要隐藏就要彻底!

[root@CXW nginx-1.10.2]# vim src/http/ngx_http_header_filter_module.c

修改前:

static char ngx_http_server_string[] = "Server: nginx" CRLF;  //49

修改后:

static char ngx_http_server_string[] = "Server: CXW" CRLF;

截图01.png

修改HTTP头信息中的connection字段,防止回显具体版本号

拓展:通用http通用头包含请求和响应消息都支持的头,通用头包含Cache-ControlConnectionDatePragmaTransfer-EncodingUpgradeVia。对通用头的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头,一般将会作为实体头处理。那么也就是说有部分设备,或者是软件,能获取到connection,部分不能,要隐藏就要彻底!

[root@CXW nginx-1.10.2]# vim src/http/ngx_http_header_filter_module.c

修改前:

static char ngx_http_server_string[] = "Server: nginx" CRLF;  //49

修改后:

static char ngx_http_server_string[] = "Server: CXW" CRLF;

[root@CXW nginx-1.10.2]# vim src/http/ngx_http_special_response.c

修改前

static u_char ngx_http_error_tail[] =     //29

"


nginx
" CRLF

"" CRLF

"" CRLF

;

修改后
static u_char ngx_http_error_tail[] =
"
CXW
" CRLF "" CRLF "" CRLF ;

nginx优化压力测试_第2张图片

2、安装ngnix

[root@CXW nginx-1.10.2]# groupadd www   #添加www组
[root@CXW nginx-1.10.2]# useradd -g www www -s /sbin/nologin  #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统
root@CXW nginx-1.10.2]#./configure --prefix=/nginx1.10 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www
[root@CXW nginx-1.10.2]# make && make install

相关选项说明

--with-http_dav_module  #增加PUT,DELETE,MKCOL:创建集合,COPYMOVE方法

--with-http_stub_status_module  #获取Nginx的状态统计信息

--with-http_addition_module   #作为一个输出过滤器,支持不完全缓冲,分部分相应请求

--with-http_sub_module     #允许一些其他文本替换Nginx相应中的一些文本

--with-http_flv_module     #提供支持flv视频文件支持

--with-http_mp4_module  #提供支持mp4视频文件支持,提供伪流媒体服务端支持

--with-http_ssl_module         #启用ngx_http_ssl_module

[root@CXW nginx-1.10.2]# ln -s /usr/src/nginx1.10/sbin/nginx /usr/local/sbin/
[root@CXW nginx-1.10.2]# nginx -t
nginx: the configuration file /usr/src/nginx1.10/conf/nginx.conf syntax is ok
nginx: configuration file /usr/src/nginx1.10/conf/nginx.conf test is successful
[root@CXW nginx-1.10.2]# nginx
[root@CXW nginx-1.10.2]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7935/nginx: master
[root@CXW nginx-1.10.2]# nginx -h
nginx version: cxw/7.0

3nginx配置项优化

[root@CXW nginx-1.10.2]# ps -ef | grep nginx

root      7935     1  0 17:24 ?        00:00:00 nginx: master process nginx

www       7936  7935  0 17:24 ?        00:00:00 nginx: worker process

root      8125  4969  0 17:30 pts/1    00:00:00 grep --color=auto nginx

截图04.png

在这里我们还可以看到在查看的时候,work进程是nginx程序用户,但是master进程还是root其中,master是监控进程,也叫主进程,work是工作进程

(1):Nginx运行工作进程个数,一般我们设置CPU的核心或者核心数x2

如果不了解cpu的核数可以top命令之后按1也可以看出来,也可以查看/proc/cpuinfo文件#grep ^processor /proc/cpuinfo | wc -l

[root@CXW nginx-1.10.2]# vim /usr/src/nginx1.10/conf/nginx.conf

nginx优化压力测试_第3张图片

[root@CXW nginx-1.10.2]# /usr/src/nginx1.10/sbin/nginx -s reload 
[root@CXW nginx-1.10.2]#  ps -aux | grep nginx | grep -v grep
root      7935  0.0  0.1  46008  1924 ?        Ss   17:24   0:00 nginx: master process nginx
www       8355  0.0  0.1  48520  2072 ?        S    17:39   0:00 nginx: worker process

Nginx运行CPU亲和力

比如4核配置

worker_processes  4;

worker_cpu_affinity 0001 0010 0100 1000

比如8核配置

worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。

Nginx最多可以打开文件数

worker_rlimit_nofile 65535;

这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

注:

文件资源限制的配置可以在/etc/security/limits.conf设置,针对root/user等各个用户或者*代表所有用户来设置。

*     soft   nofile   65535

*     hard  nofile    65535

(3)开启高效传输模式

http {
include mime.types;
default_type application/octet-stream;
……
sendfile on;
tcp_nopush on;
……

Include mime.types; //媒体类型,include 只是一个在当前文件中包含另一个文件内容的指令

default_type application/octet-stream;   //默认媒体类型足够

sendfile on//开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。

注意:如果图片显示不正常把这个改成off

tcp_nopush on必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量将响应头和正文的开始部分一起发送,而不一个接一个的发送。

(4)连接超时时间

主要目的是保护服务器资源,CPU,内存,控制连接数,因为建立连接也是需要消耗资源的

keepalive_timeout 60;
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
server_tokens off;
client_max_body_size 10m;

keepalived_timeout客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接

tcp_nodelay;也是防止网络阻塞,不过要包涵在keepalived参数才有效

client_header_buffer_size 4k;
客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过 1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
open_file_cache max=102400 inactive=20s;
这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件
数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
open_file_cache_valid 30s;
这个是指多长时间检查一次缓存的有效信息。
open_file_cache_min_uses 1;
open_file_cache指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文
件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

client_header_timeout设置请求头的超时时间。我们也可以把这个设置低些,如果超过这个时间没有发送任何数据,nginx将返回request time out的错误

client_body_timeout设置请求体的超时时间。我们也可以把这个设置低些,超过这个时间没有发送任何数据,和上面一样的错误提示

reset_timeout_connection 告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。

send_timeout响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超过这个时间,客户端没有任何活动,nginx关闭连接

server_tokens  并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。

client_max_body_size上传文件大小限制

(5)fastcgi调优

fastcgi_connect_timeout     600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /usr/local/nginx1.10/nginx_tmp;
fastcgi_intercept_errors on;
fastcgi_cache_path /usr/local/nginx1.10/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;

fastcgi_connect_timeout 600; #指定接到后端FastCGI的超时时间。

fastcgi_send_timeout 600; #FastCGI传送请求的超时时间。

fastcgi_read_timeout 600; #指定接收FastCGI应答的超时时间。

fastcgi_buffer_size 64k; #指定读取FastCGI应答第一部分需要用多大的缓冲区,默认的缓冲区大小为fastcgi_buffers指令中的每块大小,可以将这个值设置更小。

总结:

nginx的缓存功能有:proxy_cache / fastcgi_cache

proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态。
fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是php生成的动态的内容。
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带。
fastcgi_cache缓存减少了nginxphp的通信的次数,更减轻了php和数据库(mysql)的压力。

下面贴一个完整的内核优化设置:

fs.file-max = 999999
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 10240 87380 12582912
net.ipv4.tcp_wmem = 10240 87380 12582912
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 40960
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000

执行sysctl  -p使内核修改生效

(10)关于系统连接数的优化:

linux 默认值 open files1024

#ulimit -n

1024

说明server只允许同时打开1024个文件

使用ulimit -a 可以查看当前系统的所有限制值,使用ulimit -n 可以查看当前的最大打开文件数。

新装的linux 默认只有1024 ,当作负载较大的服务器时,很容易遇到error: too many open files。因此,需要将其改大

/etc/security/limits.conf最后增加:

*                soft    nofile          65535

*                hard    nofile          65535

*                soft    noproc          65535

*                hard    noproc          65535

二、部署LNMP

1、安装php

(1)解决依赖关系

[root@CXW /]#  yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel

安装libmcrypt

[root@CXW src]# tar zxf libmcrypt-2.5.7.tar.gz 
[root@CXW src]# cd libmcrypt-2.5.7/
[root@CXW libmcrypt-2.5.7]# ./configure --prefix=/usr/src/libmcrypt && make && make install

(2)编译安装php

[root@CXW src]# tar zxf php-5.6.27.tar.gz 
[root@CXW src]# cd php-5.6.27/

(3)提供php配置文件

[root@CXW php-5.6.27]# cp php.ini-production /etc/php.ini

(4)php-fpm提供脚本

[root@CXW php-5.6.27]#  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@CXW php-5.6.27]# chmod +x /etc/init.d/php-fpm 
[root@CXW php-5.6.27]# chkconfig --add php-fpm
[root@CXW php-5.6.27]# chkconfig php-fpm on

(5)提供php-fpm配置文件并编辑:

[root@CXW php-5.6.27]# cp /usr/src/php5.6/etc/php-fpm.conf.default /usr/src/php5.6/etc/php-fpm.conf

修改内容如下:

pid = run/php-fpm.pid
listen = 0.0.0.0:9000
pm.max_children =300
pm.start_servers =20
pm.min_spare_servers = 20
pm.max_spare_servers = 100

启动php-fpm服务:

[root@CXW php-5.6.27]# systemctl restart php-fpm

nginx.conf文件的server中添加下面内容支持php

location ~ .*\.(php|php5)?$ {
            root html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
            fastcgi_cache cache_fastcgi;
            fastcgi_cache_valid 200 302 1h;
            fastcgi_cache_valid 301 1d;
            fastcgi_cache_valid any 1m;
            fastcgi_cache_min_uses 1;
            fastcgi_cache_use_stale error timeout invalid_header http_500;
            fastcgi_cache_key http://$host$request_uri;
}

下面是nginx.conf的一个完整配置文件

user www www;

worker_processes  1;

worker_cpu_affinity 0001;

error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

pid        logs/nginx.pid;

 

 

events {

use epoll;

    worker_connections  65535;

    multi_accept on;

}

 

 

http {

include       mime.types;

    default_type  application/octet-stream;

 

    #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;

 

    sendfile        on;

    tcp_nopush     on;

    keepalive_timeout  65;

    tcp_nodelay on;

    client_header_buffer_size 4k;

    open_file_cache max=102400 inactive=20s;

    open_file_cache_valid 30s;

    open_file_cache_min_uses 1;

    client_header_timeout 15;

    client_body_timeout 15;

    reset_timedout_connection on;

    send_timeout 15;

    server_tokens off;

    client_max_body_size 10m;

 

    fastcgi_connect_timeout     600;

    fastcgi_send_timeout 600;

    fastcgi_read_timeout 600;

    fastcgi_buffer_size 64k;

    fastcgi_buffers     4 64k;

    fastcgi_busy_buffers_size 128k;

    fastcgi_temp_file_write_size 128k;

    fastcgi_temp_path /usr/local/nginx1.10/nginx_tmp;

    fastcgi_intercept_errors on;

    fastcgi_cache_path /usr/local/nginx1.10/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;

 

gzip on;

    gzip_min_length  2k;

    gzip_buffers     4 32k;

    gzip_http_version 1.1;

    gzip_comp_level 6;

    gzip_types  text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;

    gzip_vary on;

    gzip_proxied any;

server {

        listen       80;

        server_name  www.benet.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location ~* ^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {

            valid_referers none blocked  www.benet.com benet.com;

            if ($invalid_referer) {

                #return 302  http://www.benet.com/img/nolink.jpg;

                return 404;

                break;

             }

             access_log off;

        }

        location / {

             root   html;

             index  index.php index.html index.htm;

        }

        location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {

            expires 30d;

            #log_not_found off;

            access_log off;

        }

 

        location ~* \.(js|css)$ {

            expires 7d;

            log_not_found off;

            access_log off;

        }      

 

        location = /(favicon.ico|roboots.txt) {

            access_log off;

            log_not_found off;

        }

        location /status {

            stub_status on;

        }

        location ~ .*\.(php|php5)?$ {

            root html;

            fastcgi_pass 127.0.0.1:9000;

            fastcgi_index index.php;

            include fastcgi.conf;

            fastcgi_cache cache_fastcgi;

            fastcgi_cache_valid 200 302 1h;

            fastcgi_cache_valid 301 1d;

            fastcgi_cache_valid any 1m;

            fastcgi_cache_min_uses 1;

            fastcgi_cache_use_stale error timeout invalid_header http_500;

            fastcgi_cache_key http://$host$request_uri;

        }

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

             root   html;

        }

   }

}

[root@CXW /]# /usr/src/nginx1.10/sbin/nginx -s reload
[root@CXW /]# ab -c 500 -n 50000 http://www.benet.com/index.html

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking www.benet.com (be patient)

Completed 5000 requests

Completed 10000 requests

Completed 15000 requests

Completed 20000 requests

Completed 25000 requests

Completed 30000 requests

Completed 35000 requests

Completed 40000 requests

Completed 45000 requests

Completed 50000 requests

Finished 50000 requests

 

 

Server Software:        IIS

Server Hostname:        www.benet.com

Server Port:            80

 

Document Path:          /index.html

Document Length:        612 bytes

 

Concurrency Level:      500

Time taken for tests:   5.734 seconds

Complete requests:      50000

Failed requests:        0

Write errors:           0

Total transferred:      41800000 bytes

HTML transferred:       30600000 bytes

Requests per second:    8719.82 [#/sec] (mean)

Time per request:       57.341 [ms] (mean)

Time per request:       0.115 [ms] (mean, across all concurrent requests)

Transfer rate:          7118.92 [Kbytes/sec] received

 

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        1   25   4.2     25      38

Processing:     7   32   5.5     31      47

Waiting:        4   24   6.8     21      39

Total:         40   57   3.9     57      71

float

Percentage of the requests served within a certain time (ms)

  50%     57

  66%     59

  75%     59

  80%     60

  90%     61

  95%     62

  98%     63

  99%     64

 100%     71 (longest request)

第二次压力测试,比较两次的差异

[root@www ~]# ab -c 1000 -n 100000 http://www.benet.com/index.html

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking www.benet.com (be patient)

Completed 10000 requests

Completed 20000 requests

Completed 30000 requests

Completed 40000 requests

Completed 50000 requests

Completed 60000 requests

Completed 70000 requests

Completed 80000 requests

Completed 90000 requests

Completed 100000 requests

Finished 100000 requests

 

 

Server Software:        IIS

Server Hostname:        www.benet.com

Server Port:            80

 

Document Path:          /index.html

Document Length:        612 bytes

 

Concurrency Level:      1000

Time taken for tests:   12.010 seconds

Complete requests:      100000

Failed requests:        0

Write errors:           0

Total transferred:      83600000 bytes

HTML transferred:       61200000 bytes

Requests per second:    8326.49 [#/sec] (mean)

Time per request:       120.099 [ms] (mean)

Time per request:       0.120 [ms] (mean, across all concurrent requests)

Transfer rate:          6797.80 [Kbytes/sec] received

 

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        1   53   8.9     53      82

Processing:    17   67  11.4     66      98

Waiting:        0   49  14.3     43      84

Total:         70  119   6.5    120     140

 

Percentage of the requests served within a certain time (ms)

  50%    120

  66%    122

  75%    123

  80%    124

  90%    126

  95%    128

  98%    129

  99%    130

 100%    140 (longest request)

(5)xcache加速php

安装xcache

[root@CXW src]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
[root@CXW src]# cd xcache-3.2.0/
[root@CXW xcache-3.2.0]# /usr/src/php5.6/bin/phpize

nginx优化压力测试_第4张图片

[root@CXW xcache-3.2.0]# ./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/src/php5.6/bin/php-config 
[root@CXW xcache-3.2.0]# make && make install
Installing shared extensions:

安装完成之后,出现下面的界面,记住以下路径,后面会用到

/usr/src/php5.6/lib/php/extensions/no-debug-non-zts-20131226/
[root@CXW xcache-3.2.0]# touch /tmp/xcache
[root@CXW xcache-3.2.0]# chmod 777 /tmp/xcache

3拷贝xcache后台管理程序到网站根目录

[root@CXW xcache-3.2.0]# cp -r htdocs/ /usr/src/nginx1.10/html/xcache

4配置php支持xcache

vi / etc/php.ini #编辑配置文件,在最后一行添加以下内容

[xcache-common]

extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so

[xcache.admin]

xcache.admin.enable_auth = Off

[xcache]

xcache.shm_scheme ="mmap"

xcache.size=60M

xcache.count =1

xcache.slots =8K

xcache.ttl=0

xcache.gc_interval =0

xcache.var_size=64M

xcache.var_count =1

xcache.var_slots =8K

xcache.var_ttl=0

xcache.var_maxttl=0

xcache.var_gc_interval =300

xcache.test =Off

xcache.readonly_protection = Off

xcache.mmap_path ="/tmp/xcache"

xcache.coredump_directory =""

xcache.cacher =On

xcache.stat=On

xcache.optimizer =Off

[xcache.coverager]

xcache.coverager =On

xcache.coveragedump_directory =""

测试

[root@CXW xcache-3.2.0]# systemctl restart php-fpm

浏览器打开网站根目录下面的xcache

http://http://www.benet.com/xcache可以看到如下页面:

nginx优化压力测试_第5张图片

测试对php动态页面的压力测试

[root@CXW xcache-3.2.0]# ab -c 1000 -n 100000 http://www.benet.com/test.php

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking www.benet.com (be patient)

Completed 10000 requests

Completed 20000 requests

Completed 30000 requests

Completed 40000 requests

Completed 50000 requests

Completed 60000 requests

Completed 70000 requests

Completed 80000 requests

Completed 90000 requests

Completed 100000 requests

Finished 100000 requests

 

 

Server Software:        IIS

Server Hostname:        www.benet.com

Server Port:            80

 

Document Path:          /test.php

Document Length:        85102 bytes

 

Concurrency Level:      1000

Time taken for tests:   13.686 seconds

Complete requests:      100000

Failed requests:        0

Write errors:           0

Total transferred:      8527900000 bytes

HTML transferred:       8510200000 bytes

Requests per second:    7306.71 [#/sec] (mean)

Time per request:       136.861 [ms] (mean)

Time per request:       0.137 [ms] (mean, across all concurrent requests)

Transfer rate:          608504.46 [Kbytes/sec] received

 

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        0   17   5.5     17      81

Processing:    21  119  10.8    121     140

Waiting:        1   17   6.7     16      68

Total:         50  136   8.1    137     151

 

Percentage of the requests served within a certain time (ms)

  50%    137

  66%    139

  75%    140

  80%    141

  90%    143

  95%    144

  98%    146

  99%    148

 100%    151 (longest request)

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking www.benet.com (be patient)

Completed 10000 requests

Completed 20000 requests

Completed 30000 requests

Completed 40000 requests

Completed 50000 requests

Completed 60000 requests

Completed 70000 requests

Completed 80000 requests

Completed 90000 requests

Completed 100000 requests

Finished 100000 requests

 

 

Server Software:        IIS

Server Hostname:        www.benet.com

Server Port:            80

 

Document Path:          /test.php

Document Length:        85102 bytes

 

Concurrency Level:      1000

Time taken for tests:   13.686 seconds

Complete requests:      100000

Failed requests:        0

Write errors:           0

Total transferred:      8527900000 bytes

HTML transferred:       8510200000 bytes

Requests per second:    7306.71 [#/sec] (mean)

Time per request:       136.861 [ms] (mean)

Time per request:       0.137 [ms] (mean, across all concurrent requests)

Transfer rate:          608504.46 [Kbytes/sec] received

 

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        0   17   5.5     17      81

Processing:    21  119  10.8    121     140

Waiting:        1   17   6.7     16      68

Total:         50  136   8.1    137     151

 

Percentage of the requests served within a certain time (ms)

  50%    137

  66%    139

  75%    140

  80%    141

  90%    143

  95%    144

  98%    146

  99%    148

 100%    151 (longest request)