前幾日吧服務器給搭好了,nginx+tomcat6,剛剛開始開可以,隨着用戶數的增加,網站訪問越來越慢,開始以為是代碼問題,下載了JRockit來監控,並沒有發現內存泄漏什么的問題,然后開啟了nginx的監控,和tomcat的監控,發現問題了,tomcat的並發數是200,達到這個上線之后就不能訪問了,很有可能問題出現在這里,到網上搜索之后,很多人說tomcat6性能不如tomcat7,對並發處理也不如tomcat7,好吧,那就換成tomcat7吧,換成tomcat7之后達到這個上線還是一樣的,看了得修改並發數了,修改並發數之后性能稍好一點,但是也並不如意,網上再次搜索之后發現tomcat默認的http請求處理模式是bio(即阻塞型),每次請求都新開一個線程處理,難怪這個並發數一下就給滿了,好了,將默認bio處理方式改為nio(非阻塞型),修改之后果然快很多。下面看下具體操作:
一、關於nginx+tomcat性能監控,請看http://www.cnblogs.com/wanghaosoft/archive/2013/02/04/2892099.html
二、使用JRockit監控java內存,請看http://www.linuxidc.com/Linux/2011-04/34615.htm
三、nginx+tomcat優化
1)、nginx優化
這里只說nginx的簡單優化,即讓nginx處理html靜態文件,圖片,css,js等非動態文件,講jsp文件交給tomcat處理,這樣的話可以減輕tomcat的壓力,再說對於這些靜態文件來說,不是tomcat的強項,而是nginx的強項。
請在nginx.conf中添加如下配置
location ~ .*\.(gif|jpg|jpeg|png|bmp|ico)$ {
root /www/; #即圖片存在的根路徑
expires 30d;
}
location ~ .*\.(js|css)?$ {
root /www/;#即圖文件存在的根路徑
expires 10h;
}
還可以將worker_processes 2; #處理線程更改為cpu的倍數,即cpu*2。最后附上詳細餓nginx配置文件供參考
2)、tomcat優化,
1、修改tomcat的並發線程和默認處理方式為nio
這里以tomcat7為例,修改tomcat/conf/server.xml中Connector節點為
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="1000" minSpareThreads="25" maxSpareThreads="250"
enableLookups="false" redirectPort="8443" acceptCount="300" connectionTimeout="20000" disableUploadTimeout="true"/>
2、tomcat的幾種connector方式簡介
Tomcat的四種基於HTTP協議的Connector性能比較
<Connector port="8081" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000" redirectPort="8443"/>
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443"/>
<Connector executor="tomcatThreadPool"
port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector executor="tomcatThreadPool"
port="8081" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" />
我們姑且把上面四種Connector按照順序命名為 NIO, HTTP, POOL, NIOP
為了不讓其他因素影響測試結果,我們只對一個很簡單的jsp頁面進行測試,這個頁面僅僅是輸出一個Hello World。假設地址是 http://tomcat1/test.jsp
我們依次對四種Connector進行測試,測試的客戶端在另外一台機器上用ab命令來完成,測試命令為: ab -c 900 -n 2000 http://tomcat1/test.jsp ,最終的測試結果如下表所示(單位:平均每秒處理的請求數):
NIO HTTP POOL NIOP
281 65 208 365
666 66 110 398
692 65 66 263
256 63 94 459
440 67 145 363
由這五組數據不難看出,HTTP的性能是很穩定,但是也是最差的,而這種方式就是Tomcat的默認配置。NIO方式波動很大,但沒有低於280 的,NIOP是在NIO的基礎上加入線程池,可能是程序處理更復雜了,因此性能不見得比NIO強;而POOL方式則波動很大,測試期間和HTTP方式一樣,不時有停滯。
由於linux的內核默認限制了最大打開文件數目是1024,因此此次並發數控制在900。
盡管這一個結果在實際的網站中因為各方面因素導致,可能差別沒這么大,例如受限於數據庫的性能等等的問題。但對我們在部署網站應用時還是具有參考價值的
最后附上nginx的配置文件,這是我的配置文件,僅供參考,如果您有什么好的建議請留言。
關於nginx的各項參數解釋請看http://www.cnblogs.com/wanghaosoft/archive/2013/01/16/2863265.html
user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
#new 1 start---------------
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32K;
client_max_body_size 8m;
#new 1 end -----------
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#tomcat add start<<
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#tomcat add end>>
gzip on;
#news2 start --
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#tomcat add start<<
upstream tomcat_server {
server 127.0.0.1:8080;
#server 127.0.0.1:9080;
}
#tomcat add end>>
#Proxy_temp_path:/www/cache/images_temp;
#Proxy_cache_path:/www/cache/images_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;
server {
listen 80;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /www;
index index.html index.htm index.jsp default.jsp index.do default.do index.php default.php;
#nagios
#auth_basic "nagios admin";
#auth_basic_user_file /usr/local/nagios/etc/nagiosAdmin.net;
#cache set start
#proxy_cache cache_one;
#proxy_cache_valid 200 304 12h;
#proxy_cache_key $host$uri$is_args$args;
#proxy_set_header Host $host;
#proxy_set_header X-Forwarded-For $remote_addr;
#proxy_pass http://127.0.0.1:8080;
#log_format cache '***$time_local ' '***$upstream_cache_status ' '***Cache-Control: $upstream_http_cache_control ' '***Expires: $upstream_http_expires ' '***"$request" ($status) ' '***"$http_user_agent" ';
#access_log /usr/local/nginx-0.8.32/logs/cache.log cache;
#expires 1d;
}
#tomcat add start<<
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$http://$host/$1$2/ permanent;
}
location ~ \.(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_server;
}
#tomcat add end>>
error_page 404 /404.html;
location = /404.html {
root /www;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /www;
}
# 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 /www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;
include fastcgi_params;
#nagios
#auth_basic "nagios admin";
#auth_basic_user_file /usr/local/nagios/etc/nagiosAdmin.net;
}
# location ~* /dwr/?
# {
# proxy_pass http://localhost:8080;
# }
location ~ .*\.(gif|jpg|jpeg|png|bmp|ico)$ {
root /www/;
expires 30d;
}
location ~ .*\.(js|css)?$ {
root /www/;
expires 10h;
}
location /status {
stub_status on;
access_log off;
}
location /nagios {
alias /www/nagios;
auth_basic "nagios admin";
auth_basic_user_file /usr/local/nagios/etc/nagiosAdmin.net;
}
location /cgi-bin {
alias /usr/local/nagios/sbin;
}
location ~ .*\.cgi$ {
root /usr/local/nagios/sbin;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
include fastcgi_params;
auth_basic "nagios admin";
auth_basic_user_file /usr/local/nagios/etc/nagiosAdmin.net;
}
location ~ .*\.pl$ {
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_index index.pl;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$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;
#}
}
# Load config files from the /etc/nginx/conf.d directory
#news 2 -----------
include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;
}