大家好、 agentzh好:
我是一个nginx新手,最近有幸接触到agentzh开发的几个给力的nginx模块,想把它们在线上用一用,但是苦于对配置不熟,所以结果并没有网上你们那些测试结果那么诱人,而且遇到很多问题,在此向大家请教。
先贴上我的相关配置及说明,其间的问题还望大家多多指教,将不胜感谢。
worker_processes 4; #因为我的机器是4核
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
keepalive_timeout 65;
upstream memcache {
server 192.168.11.64:11211;
#server 192.168.11.64:11212;
#server 192.168.11.64:11213;
keepalive 512 single;
}
server {
listen 80;
server_name xxxx.com;
#all modules config -> index.php 因为使用了其他php框架所以将相关请求指向入口文件
if (!-e $request_filename) {
rewrite ^/(forum|index|topic)(.*) /index.php?$2 last;
}
root /data/htdocs/www;
index index.html index.htm index.php;
location /memc {
# internal; #问题1:为什么我打开这个配置,memc就不生效了?
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key memc$arg_tid;
set $memc_exptime 3000;
memc_pass memcache;
error_page 404 = @missing;
}
location @missing { #如果缓存中没有,跳转到missing部分请求php,将http://xxx.com/topic$1请求的结果cache
rewrite ^/memc(.*) http://xxx.com/topic$1 last;
#问题2:为什么我通过http://xxx.com/memc?tid=xxxx访问的时候,按上面的rewrite应该转向
http://xxx.com/topic?tid=xxxx请求才对,但是事实却将请求结果生成了下载文件,一请求就下载了
一个memc文件。。。而使用下面的rewrite规则却能正常通过url(http://xxx.com/memc/index.html?tid=xxxx跳转到了http://xxx.com/topic?tid=xxxxx)访问
#rewrite ^/memc/index\.html(.*) http://xxx.com/topic$1 last;
}
#location ~ \.php$ {
location ~ .*\.(php|php5)?$ {
charset utf-8;
default_type text/html;
if ($request_uri ~* "^/topic\?tid\=(.*)$"){
#问题3:我将满足条件的请求结果缓存了,但是我在这里对$key变量不是很了解,一个访问过来先去请求我上面的http://xxx.com/memc?tid=xxxx,如果缓存中没有取到则根据上面的rewrite规则自动跳转到
http://xxx.com/topic?tid=xxxx,也就是一个满足缓存条件的请求,srcache将cgi程序请求返回的结果cache了一份到memcached里面,cache的key是memc里面的$memc_key(memc$arg_tid)?还是srcache里面
set的$key($uri$args)啊?
根据测试的结果,我认为应该是上面的$memc_key(还是我理解错了?如果不错那srcache里面set的$uri$args是做什么的啊?)
#srcache-nginx-module
set $key $uri$args;
#set $key $arg_tid;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
}
#问题4:为什么缓存的结果里面一直带着请求的头信息
【HTTP/1.1 200 OK Content-Type: text/html X-Powered-By: PHP/5.3.10】
这些信息当我直接用http://xxx.com/memc?tid=xxxx访问的时候一直会出现在页面的最前端,但是使用http://xxx.com/topic?tid=xxxx访问时却没有(此时请求也是直接读的缓存【因为我在php里面echo了发觉php没有执行,这样的话,一次请求是被cache了两次吗?我用memcached -vvv启动跟踪测试的结果是没有cache两次,但是怎样能让使用http://xxx.com/memc?tid=xxxx访问的结果里面不带那些头信息啊?cache里面存的内容就是带头信息的,用http://xxx.com/topic?tid=xxxx访问头信息就没有输出了,这样的话是nginx做了处理吗?这个处理对性能会有影响吗?】)
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
配置贴完我的问题也差不多问完了,也许我的问题很白痴,但是请大家一笑过后表忘了给我指导下啊,尤其重要的是,我ab测试的结果并不理想,而且还有大量的Failed requests,但是我查不到是为什么:
我的服务器配置如下:
Tasks: 383 total, 1 running, 382 sleeping, 0 stopped, 0 zombie
Cpu0 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 0.3%us, 0.3%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 4054904k total, 1078024k used, 2976880k free, 64696k buffers
Swap: 6160376k total, 531192k used, 5629184k free, 316072k cached
http://xxx/memc/index.html?tid=04fd4c1f406718691c129919
[root@xxx-test02 ~]# ab -c 1000 -n 1000 http://xxx/memc/index.html?tid=04fd4c1f406718691c129919
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxx.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: ngx_openresty
Server Hostname: xxx.com
Server Port: 80
Document Path: /memc/index.html?tid=04fd4c1f406718691c129919
Document Length: 186 bytes
Concurrency Level: 1000
Time taken for tests: 0.290 seconds
Complete requests: 1000
Failed requests: 132
(Connect: 0, Receive: 0, Length: 132, Exceptions: 0)
Write errors: 0
Non-2xx responses: 868
Total transferred: 2057608 bytes
HTML transferred: 1893816 bytes
Requests per second: 3443.80 [#/sec] (mean)
Time per request: 290.377 [ms] (mean)
Time per request: 0.290 [ms] (mean, across all concurrent requests)
Transfer rate: 6919.91 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 23 37 8.2 37 51
Processing: 23 65 48.9 52 220
Waiting: 20 61 39.5 52 190
Total: 70 102 43.6 89 250
Percentage of the requests served within a certain time (ms)
50% 89
66% 102
75% 111
80% 115
90% 149
95% 240
98% 245
99% 246
100% 250 (longest request)
[root@xxx-test02 ~]# ab -c 10000 -n 10000 http:/xxx/memc/index.html?tid=04fd4c1f406718691c129919
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxx.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: ngx_openresty
Server Hostname: xxx.com
Server Port: 80
Document Path: /memc/index.html?tid=04fd4c1f406718691c129919
Document Length: 0 bytes
Concurrency Level: 10000
Time taken for tests: 1.061 seconds
Complete requests: 10000
Failed requests: 10000
(Connect: 0, Receive: 0, Length: 1946, Exceptions: 8054)
Write errors: 0
Non-2xx responses: 1841
Total transferred: 2038274 bytes
HTML transferred: 1720446 bytes
Requests per second: 9428.44 [#/sec] (mean)
Time per request: 1060.621 [ms] (mean)
Time per request: 0.106 [ms] (mean, across all concurrent requests)
Transfer rate: 1876.73 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 227 395 94.9 398 548
Processing: 187 252 96.9 209 510
Waiting: 0 48 99.0 0 323
Total: 446 647 170.2 597 1057
Percentage of the requests served within a certain time (ms)
50% 597
66% 642
75% 670
80% 731
90% 942
95% 988
98% 997
99% 1035
100% 1057 (longest request)
http://xxx/topic?tid=04fd4c1f406718691c129919
[root@xxx-test02 ~]# ab -c 1000 -n 1000 http://xxx/topic?tid=04fd4c1f406718691c129919
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxx.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: ngx_openresty
Server Hostname: xxx.com
Server Port: 80
Document Path: /topic?tid=04fd4c1f406718691c129919
Document Length: 0 bytes
Concurrency Level: 1000
Time taken for tests: 0.333 seconds
Complete requests: 1000
Failed requests: 1000
(Connect: 0, Receive: 0, Length: 956, Exceptions: 44)
Write errors: 0
Non-2xx responses: 797
Total transferred: 2278094 bytes
HTML transferred: 2124564 bytes
Requests per second: 3000.74 [#/sec] (mean)
Time per request: 333.251 [ms] (mean)
Time per request: 0.333 [ms] (mean, across all concurrent requests)
Transfer rate: 6675.75 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 23 37 8.4 37 52
Processing: 2 97 63.8 90 280
Waiting: 0 97 64.0 90 278
Total: 24 134 60.9 127 330
Percentage of the requests served within a certain time (ms)
50% 127
66% 153
75% 168
80% 176
90% 234
95% 266
98% 272
99% 291
100% 330 (longest request)
[root@xxx-test02 ~]# ab -c 10000 -n 10000 http://xxx/topic?tid=04fd4c1f406718691c129919
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxx.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: ngx_openresty
Server Hostname: xxx.com
Server Port: 80
Document Path: /topic?tid=04fd4c1f406718691c129919
Document Length: 0 bytes
Concurrency Level: 10000
Time taken for tests: 1.704 seconds
Complete requests: 10000
Failed requests: 10000
(Connect: 0, Receive: 0, Length: 2050, Exceptions: 7950)
Write errors: 0
Non-2xx responses: 1617
Total transferred: 5409100 bytes
HTML transferred: 5079112 bytes
Requests per second: 5868.07 [#/sec] (mean)
Time per request: 1704.139 [ms] (mean)
Time per request: 0.170 [ms] (mean, across all concurrent requests)
Transfer rate: 3099.70 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 256 423 95.4 424 598
Processing: 248 626 238.5 624 1077
Waiting: 0 139 301.3 0 1044
Total: 754 1050 250.4 999 1673
Percentage of the requests served within a certain time (ms)
50% 999
66% 1011
75% 1015
80% 1292
90% 1565
95% 1573
98% 1647
99% 1659
100% 1673 (longest request)