• server

    香港独立服务器

    免费IPMI/ 免费WEB环境配置/ 网络优质/ 价格实惠

    联系客服咨询
  • 阿里云香港ECS特惠销售

    全场八折优惠促销,冲1000元得1200元

    查看套餐 产品介绍

  • server

    香港PHP独立IP虚拟主机


    查看套餐

设置apache和nginx的tomcat负载平衡

当前位置: 首页  >> 常见问题  >> 服务器常见问题  >> 查看详情

概述

本篇文章主要先容apache和nginx的相关设置,tomcat的相关安装设置我在前面有写过一篇,具体先容通过两种设置要领设置nginx。

apache设置

源码安装

./configure--prefix=/usr/local/apache(安装目次)
make
makeinstall

对付2.4以上版本的apache在举办源码安装的时候有的呆板会提示缺少部门插件譬喻:apr、apr-util、pcre,需要先将这些插件安装好然后再安装apache

YUM安装

yuminstallhttpd

设置tomcate负载平衡

进入安装目次cnf.d文件夹下面,建设一个.conf后缀的文件

touchapa.conf
vimapa.cnf
Listen8051
<VirtualHost*:8051>
ServerAdmin[email protected]
ServerNamelocalhost
ErrorLog'/etc/httpd/logs/app_error.log'
CustomLog'/etc/httpd/logs/app_access.log'common
ProxyPass/balancer://cluster/stickysession=JSESSIONID|jsessionidnofailover=Onlbmethod=byrequeststimeout=5maxattempts=3
ProxyPassReverse/balancer://cluster/
ProxyRequestsOff
ProxyPreserveHostOn
<proxybalancer://cluster>
#BalancerMemberajp://localhost:8009route=tomcat_a
BalancerMemberhttp://localhost:8080/Front
#BalancerMemberajp://localhost:8010route=tomcat_b
BalancerMemberhttp://localhost:8081/Front
</proxy>
</VirtualHost>

设置文件一开始设置了apache的端口8051,然后在最下面设置了毗连tomcat的项目端口,我这里的设置的apache和tomcat都在一台处事器上面别离利用了差异的端口,在tomcat的webapps路径下面建设了一个Front项目,项目下面存放了一个test.jsp的测试页面

cd/usr/local/tomcat1/webapps
mkdirFront
cdFront
touchtest.jsp
vimtest.jsp
<fontcolor=red>testa</font><b>

同样在tomcat2中也利用同样的要领建设测试页面,可是将testa改成testb

接下来确保8051端口被启用,也可以封锁防火墙,tomcat1、tomcat2都已启动,启动要领参考前面我写的关于tomcat的文章,确保httpd也以启动,假如已经将httpd插手了启动处事,

启动http处事
servicehttpdstart
查察处事启动状态
servicehttpdstatus

接下来在欣赏器中输入:http://localhost:8051/test.jsp

假如刷新毗连页面功效是在testa和testb之间切换,说明设置乐成。

nginx设置

源码安装

建设nginx用户组
groupaddnginx
建设nginx用户
useradd-gnginx-s/sbin/nologinnginx
安装相关插件
yuminstall–ymakezlib-developenssl-develpcre-devel
解压nginx安装包
tarzxvfnginx-1.8.0.tar.gz
进入安装包
cdnginx-1.8.0
安装
./configure--prefix=/usr/local/nginx--with-http_ssl_module--with-http_gzip_static_module--with-http_stub_status_module
make&&makeinstall

单个文件设置要领

建设测试页面

cd/usr/local/tomcat1/webapps
mkdirMFront
cdMFront
touchindex.jsp
vimindex.jsp
<fontcolor=red>MFronttesta</font><b>

tomcat2也同样操纵,将MFronttesta改成MFronttestb

设置文件

cd/usr/local/nginx/conf
vimnginx.conf
usernginxnginx;
worker_processes8;
pid/usr/local/nginx/nginx.pid;
worker_rlimit_nofile102400;
events
{
useepoll;
worker_connections102400;
}
http
{
includemime.types;
default_typeapplication/octet-stream;
fastcgi_intercept_errorson;
charsetutf-8;
server_names_hash_bucket_size512;
client_header_buffer_size1024k;
large_client_header_buffers4128k;
client_max_body_size300m;
sendfileon;
tcp_nopushon;
keepalive_timeout600;
tcp_nodelayon;
client_body_buffer_size512k;
proxy_connect_timeout5;
proxy_read_timeout600;
proxy_send_timeout50;
proxy_buffer_size16k;
proxy_buffers464k;
proxy_busy_buffers_size128k;
proxy_temp_file_write_size128k;
gzipon;
gzip_min_length1k;
gzip_buffers416k;
gzip_http_version1.1;
gzip_comp_level2;
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
gzip_varyon;
###2012-12-19changenginxlogs
log_formatmain'$http_x_forwarded_for-$remote_user[$time_local]'$request''
'$status$body_bytes_sent'$http_referer''
''$http_user_agent'$request_time$remote_addr';
upstreamFront
{
server127.0.0.1:8080weight=1max_fails=2fail_timeout=30s;
server127.0.0.1:8081weight=1max_fails=2fail_timeout=30s;
}
upstreamMFront{
server127.0.0.1:8080weight=1max_fails=2fail_timeout=30s;
server127.0.0.1:8081weight=1max_fails=2fail_timeout=30s;
}
####chinaapp.sinaapp.com
server{
listen80;
server_namelocalhost;
location/Front
{
proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_passhttp://Front;
expires3d;
}
location/MFront
{
proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_passhttp://MFront;
expires3d;
}
}
}