189 8069 5689

nginx+tomcat

<

10余年的新干网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整新干建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“新干网站设计”,“新干网站推广”以来,每个客户项目都认真落实执行。div>nginx+tomcat集群实现负载均衡

nginxtomcat集群server负载均衡file
1、安装nginx
所需的prce库
wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz

tarzxvfpcre-8.10.tar.gz
cdpcre-8.10/
./configure
make&&makeinstall
cd../

安装nginx
wget http://nginx.org/download/nginx-0.8.54.tar.gz
wget http://nginx.org/download/nginx-1.0.2.tar.gz

tarzxvfnginx-1.0.2.tar.gz
cdnginx-1.0.2/
./configure--user=www --group=www--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module
make&&makeinstall
cd../
创建Nginx日志目录
mkdir-p/home/nginx/logs
chmod+w/home/nginx/logs
chown-Rwww:www/home/nginx/logs
创建Nginx配置文件
①、在/usr/local/nginx/conf/目录中创建nginx.conf文件:
rm-f/usr/local/nginx/conf/nginx.conf
vi/usr/local/nginx/conf/nginx.conf
输入以下内容:
userwww www;
worker_processes8;
error_log/home/nginx/logs/nginx_error.logcrit;
pid/usr/local/nginx/nginx.pid;
worker_rlimit_nofile65535;

events
{
useepoll;
worker_connections65535;
}

http
{
includemime.types;
default_typeapplication/octet-stream;
#charsetgb2312;

server_names_hash_bucket_size128;
client_header_buffer_size32k;
large_client_header_buffers432k;
client_max_body_size8m;
sendfileon;
tcp_nopushon;

keepalive_timeout60;

tcp_nodelayon;

fastcgi_connect_timeout300;
fastcgi_send_timeout300;
fastcgi_read_timeout300;
fastcgi_buffer_size64k;
fastcgi_buffers464k;
fastcgi_busy_buffers_size128k;
fastcgi_temp_file_write_size128k;

gzipon;
gzip_min_length1k;
gzip_buffers416k;
gzip_http_version1.0;
gzip_comp_level2;
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
gzip_varyon;

#limit_zonecrawler$binary_remote_addr10m;

#测试
server
{
listen80;
server_name www.test.com;
indexindex.htmindex.htmlindex.jsp;
root/home/htdocs/web/ROOT/;

location~.*$
{
indexindex.jsp;
proxy_pass http://www.test.com:8080;
}
access_loglogs/sp.imichat.com.logcombined;
error_page404=/404.html;
}

}

②、在/usr/local/nginx/conf/目录中创建fcgi.conf文件:
vi/usr/local/nginx/conf/fcgi.conf
输入以下内容:
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWAREnginx;

fastcgi_paramQUERY_STRING$query_string;
fastcgi_paramREQUEST_METHOD$request_method;
fastcgi_paramCONTENT_TYPE$content_type;
fastcgi_paramCONTENT_LENGTH$content_length;

fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
fastcgi_paramSCRIPT_NAME$fastcgi_script_name;
fastcgi_paramREQUEST_URI$request_uri;
fastcgi_paramDOCUMENT_URI$document_uri;
fastcgi_paramDOCUMENT_ROOT$document_root;
fastcgi_paramSERVER_PROTOCOL$server_protocol;

fastcgi_paramREMOTE_ADDR$remote_addr;
fastcgi_paramREMOTE_PORT$remote_port;
fastcgi_paramSERVER_ADDR$server_addr;
fastcgi_paramSERVER_PORT$server_port;
fastcgi_paramSERVER_NAME$server_name;

#PHPonly,requiredifPHPwasbuiltwith--enable-force-cgi-redirect
fastcgi_paramREDIRECT_STATUS200;
启动Nginx
ulimit-SHn65535
/usr/local/nginx/sbin/nginx
配置开机自动启动Nginx
vi/etc/rc.local
在末尾增加以下内容:
ulimit-SHn65535
/usr/local/nginx/sbin/nginx
优化Linux内核参数
vi/etc/sysctl.conf
在末尾增加以下内容:
#Add
net.ipv4.tcp_max_syn_backlog=65536
net.core.netdev_max_backlog=32768
net.core.somaxconn=32768

net.core.wmem_default=8388608
net.core.rmem_default=8388608
net.core.rmem_max=16777216
net.core.wmem_max=16777216

net.ipv4.tcp_timestamps=0
net.ipv4.tcp_synack_retries=2
net.ipv4.tcp_syn_retries=2

net.ipv4.tcp_tw_recycle=1
#net.ipv4.tcp_tw_len=1
net.ipv4.tcp_tw_reuse=1

net.ipv4.tcp_mem=94500000915000000927000000
net.ipv4.tcp_max_orphans=3276800

#net.ipv4.tcp_fin_timeout=30
#net.ipv4.tcp_keepalive_time=120
net.ipv4.ip_local_port_range=102465535

使配置立即生效:
/sbin/sysctl-p

在不停止Nginx服务的情况下变更Nginx配置
修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/nginx/sbin/nginx-t

如果屏幕显示以下两行信息,说明配置文件正确:
theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok
theconfigurationfile/usr/local/nginx/conf/nginx.confwastestedsuccessfully
重启nginx:
/usr/local/nginx/sbin/nginx-sreload
编写每天定时切割Nginx日志的脚本
创建脚本/usr/local/nginx/sbin/cut_nginx_log.sh
vi/usr/local/nginx/sbin/cut_nginx_log.sh
输入以下内容:
#!/bin/bash
#Thisscriptrunat00:00
#TheNginxlogspath
logs_root_path="/home/nginx/logs/"
logs_path=${logs_root_path}$(date-d"yesterday"+"%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
mv${logs_file}${cut_logs_file}
tarczf${cut_logs_file}.tar.gz${cut_logs_file}
rm-f${cut_logs_file}
#重启nginx
/usr/local/nginx/sbin/nginx-sreload

对这个脚本赋执行权限
#chmod+x/usr/local/nginx/sbin/cut_nginx_log.sh
设置crontab,每天凌晨00:00切割nginx访问日志
crontab-e
输入以下内容:
0000***/bin/bash/usr/local/nginx/sbin/cut_nginx_log.sh

2、配置nginx负载均衡
编辑nginx.conf
vim/usr/local/nginx/conf/nginx.conf
在server上面增加
upstream www.test.com{
server www.test.com:8888;
server www.test.com:8080;
#ip_hash;注:如果加入了ip_hash,这种只能适用于当某个用户连接上了一台服务器后,他登陆之后所做的一切操作都只会在那一台服务器不会跳转到另外的服务器,如果那台服务器over掉了,则会自动退出,连接到另一服务器
}
www.test.com是自己的服务器,最好是用域名
upstream后面的名字要与proxy_pass这个名字一样,建议upstream后面的名字,proxy_pass后面的名字,server_name后面的名字一致
示例:
upstream www.test.com{
server www.test.com:8888;
server www.test.com:8080;
}

#测试
server
{
listen80;
server_name www.test.com;
indexindex.htmindex.htmlindex.jsp;
root/home/htdocs/web/ROOT/;

location~.*$
{
indexindex.jsp;
proxy_pass http://www.test.com;注:做了负载均衡,这个后面就不能再加端口
}
access_loglogs/sp.imichat.com.logcombined;
error_page404=/404.html;
}

注:如果负载均衡不成功,请检查是否没有经过nginx,80端口直接被转发到了tomcat服务的端口上

3、配置tomcat集群
优化tomcat并发数,编辑server.xml

maxThreads="2048"minSpareThreads="100"maxSpareThreads="200"

enableLookups="false"redirectPort="8443"acceptCount="500"

connectionTimeout="20000"disableUploadTimeout="true"/>




修改web.xml
增加标签:
直接加在之前就可以了,这个是加入tomcat的session复制的,做tomcat集群必须需要这一步,否则用户的session就无法正常使用.

网站标题:nginx+tomcat
URL网址:http://cdxtjz.cn/article/cpigoe.html

其他资讯