nginx + tomcat 叢集實現負載平衡
2012-09-21 14:15 1338人閱讀 評論(1) 收藏 舉報
所需的prce庫
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz cre-8.10.tar.gz
cd pcre-8.10/
./configure
安裝nginx
wget http://nginx.org/download/nginx- 0.8.54.tar.gz
wget
http://nginx.org/download/nginx-1.0.2.tar.gz cd nginx -1.0.2/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../ 建立Nginx日誌目錄
mkdir -p /home/nginx/logs
chwwwch; /home/nginx/ logs
建立Nginx設定檔
①、在/usr/local/nginx/conf/目錄中建立nginx.conf檔案:
/conf
vi /usr/local/nginx/conf/nginx.conf
輸入以下:
user home/nginx/logs/nginx_error.log crit ;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
eventsp worker_connections 65535;
}
http
{
包括mime.types;
default_type
server_ name_hash_bucket_size 128;
client_header_buffer_size 32k; 4 32k;
client_max_body_size 8m;
sendfile 開式live_timeout 60;
tcp_nodelay fastcgi_read_timeout 300 ;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k; fastcgi_temp_file_write_size 12 8k;
gzip 開啟;
16k;
gzip_http_version 1.0 ;
gzip_comp_level 2;
gzip_types text/plain
#limit_zone crawler$binary_re_ addr10m;
#測試
伺服器
{
聽覺 80;
root /home /htdocs/網路/ROOT/;
位置 ~ .*$
{
index index
}
存取_log 日誌/sp.imichat .com.logmm;
error_page 404 = /404.html;
}
}
二、在/usr/local/nginx/conf/目錄中建立fcgi.conf檔案:
vi /usr/local/fc
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
cgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONT對ENAME $ document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER $ Remote_addr;
fastcgi_param REMOTE_PORT $ ;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
#只適用於PHP,如果PHP 是使用--enable )
啟動Nginx
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx
設定啟動自動啟動N ginx
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx
最佳化Linux核心參數
vi /etc/sysctl . # Add
net 。 ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 327688838 是
net.core.rmem_default = 8388608
net。 core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_timestamps = 0net.ipv4.tcp_timestamps = 0
net。 ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ip v4.tcp_tw_reuse = 1
net.ip0. 00 0
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
# net.ipv4.tcp_keepalive_time = 120
:
/sbin/sysctl -p
在不停止Nginx服務的情況下更改Nginx設定
/usr/local/nginx/conf/nginx.conf設定檔後,請執行下列指令檢查設定檔是否正確:
/usr/local / nginx/sbin/nginx -t
如果螢幕顯示以下兩行信息,說明設定檔正確:
/ usr/local/nginx/conf/nginx.conf測試成功
重啟nginx:
/usr/local/ nginx/sbin/nginxx 建立腳本/usr/local/nginx/sbin/cut_nginx_log.sh
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_root_path="/home/nginx/logs/"day
logs_root_path="/home/nginx/logs/"daylogs_root_path="/home/nginx/logs/"day,$log; +"% Y")/$(date -d "yesterday" +"%m")/
mkdir -p ${logs_path}
#日誌檔案名稱 ==============}
#日誌檔案名稱 ================}
#日誌檔案名稱 ================}
#日誌檔案名稱 ================ ======================================
logs_name="weblogs"
logs_file =${logs_root_path}${logs_name}.log
cut_logs_file=${logs_path}${logs_name}_$(date -d "yesterday" +"%Y%m%d").log
{logs_file} ${cut_logs_file}
tar czf ${cut_logs_file}.tar.gz ${cut_logs_file}
rm -f ${cut_logs_}$ /local/nginx/ sbin/nginx -s reload
對這個腳本賦執行權
# chmod +x /usr/local/nginx/sbin/cut_nginx_log.sh
crontab -e
輸入以下內容:
00 00 * * * /bin/bash 00 00 * * * /bin/bash /usr/mec/L集. nginx負載平衡
編輯nginx .conf
vim /usr/local/nginx/conf/nginx.conf
server www.test.com:8888;
server www.test.com:8080;
所做的一切操作都只會在那一台伺服器不會跳到另外的伺服器,如果那台伺服器over掉了,則會自動退出,連接到另一台伺服器 }
www.test.com 是自己的伺服器,最好是用網域名稱
upstream後面的名字要與proxy_pass 這個名字一樣,建議upstream後面的名字,proxy_pass 後面的名字,server_name 後面的名字一致
範例:testtest
🎠 .com {
server www.test.com:8888; 8080;
}
#測試
server ;
server_name
www.test.com; index index.htm index.html index.jsp; location ~ .*$
{
index index.jsp;
proxy_pass
http://www.test.com; access_log logs/sp .imichat.com.log combined;
error_page 404 = /404.html;
}
註:如果負載平衡不成功,請檢查是否沒有經過nginx,80埠直接被轉送到了tomcat服務的連接埠上
3、設定tomcat叢集
tom
SpareThreads="100" maxSpareThreads="200"
enableLookups="false redirectPort="8844Count350accept "
c/>
修改web.xml
之前就可以了,這個是加入tomcat 的session 複製的,做tomcat 叢集必須需要這一步,否則使用者的session 就無法正常使用.
以上就介紹了nginx + tomcat 叢集實現負載平衡,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。