问题描述
- 用Nginx实现均衡负载效率反而降低,为什么?
-
我在Win7系统下安装了两个tomcat,一个8080端口,一个8088端口。跑着同样的WEB项目。然后在Win7下用VMWare装了个CentOS6.5虚拟机,在该虚拟机上安装并配置了Nginx。然后在该虚拟机上用apache ab进行压力测试,结果用nginx反而比不用差一些。请高手帮助解惑!!!下面是我的测试方法和结果:
[root@localhost local]# ab -n 10000 -c 1000 -H "Authorization: Digest username="cpe", realm="SWACS", nonce="2ef7d7f74e136a548362a488fa1ca753",uri="/SWTMS/acs", qop="auth", nc=00000001, cnonce="1243442698", response="78374f1538516f6055faf0dcab6571a7",opaque="A0720F8F9F1C4EC1B108C201E4660C79"" -p ./inform.txt http://172.16.15.110:8080/SWTMS/acs(这是直接访问tomcat,访问nginx则是把url改成http://172.16.15.111/SWTMS/acs)
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 172.16.15.110 (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 requestsServer Software: Apache-Coyote/1.1
Server Hostname: 172.16.15.110
Server Port: 8080Document Path: /SWTMS/acs
Document Length: 549 bytesConcurrency Level: 1000
Time taken for tests: 10.269 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 7910000 bytes
Total POSTed: 32570000
HTML transferred: 5490000 bytes
Requests per second: 973.81 #/sec
Time per request: 1026.895 ms
Time per request: 1.027 ms下面是我的Nginx配置:
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
multi_accept on;
use epoll;
}http {
include mime.types;
default_type application/octet-stream;charset utf-8; #默认编码 server_names_hash_bucket_size 128; #服务器名字的hash表大小 client_header_buffer_size 32k; #上传文件大小限制 large_client_header_buffers 4 64k; #设定请求缓 client_max_body_size 8m; #设定请求缓 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 0; keepalive_timeout 65; tcp_nodelay on; #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。 fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; #gzip模块设置 gzip on; #开启gzip压缩输出 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0) gzip_comp_level 2; #压缩等级 gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。 gzip_vary on; upstream tomcats { server 172.16.15.110:8088 weight=1; server 172.16.15.110:8080 weight=1; #ip_hash; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://tomcats; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 50m; client_body_buffer_size 256k; proxy_connect_timeout 10; proxy_send_timeout 15; proxy_read_timeout 15; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}
}
解决方案
你机器是单核吧。这样无法发挥多核CPU的性能,所以单个比多个节省了调度。反而性能会好一些
解决方案二:
我给虚拟机分配的是一个CPU,不确定是不是这个原因。我在Win7(4核)下面也配过nginx,但效率也低。不过Windows系统好像的确不能用nginx提速
解决方案三:
在linux真机上测试,排除下io 网络等别的瓶颈
解决方案四:
参考下:http://www.nginx.cn/2212.html 或许对你有用