Linux 同台服务器上用ngnix配置两个tomcat的负载均衡

Linux 同台服务器上用ngnix配置两个tomcat的负载均衡
今天要在Linux服务器上做个负载均衡,于是从网上搜了许多资料,自己总结下来并成功实现
1.Linux上安装Java jdk
  a.执行安装命令:
#yum install jdk-8u66-linux-x64.rpm
  b.在/etc/profile中配置环境变量
export JAVA_HOME=/usr/java/jdk1.8.0_66
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
  c.使环境变量生效
#source /etc/profile
  d.测试是否安装成功
#java -version

2.Linux上部署2个tomcat,名字为tomcat1,tomcat2
   a.修改TOMCAT配置文件tomcat2/conf/server.xml
将以下内容
    <Server port="8005" shutdown="SHUTDOWN">...
    <Connector port="8080" protocol="HTTP/1.1" ...
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Host name="localhost" appBase="webapps"
    .../>
修改为
    <Server port="9005" shutdown="SHUTDOWN">...
    <Connector port="9090" protocol="HTTP/1.1" ...
    <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />
    <Host name="localhost" appBase="/usr/local/www/web"
    .../>
   b.启动tomcat1和tomcat2   
3.Linux上安装ngnix
   a.检查安装nginx的依赖性,nginx的模块需要第三方的支持,检查是否安装下列库:
   zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc
   b.安装ngnix
#tar zxvf nginx-1.0.5.tar.gz
#wget http://www.linuxidc.com/system/systemfiles/2011/08/ngx_cache_purge-1.3.tar.gz
#tar zxvf ngx_cache_purge-1.3.tar.gz
#cd nginx-1.0.5

#./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --add-module=../ngx_cache_purge-1.3
#make
#make install
4.配置ngnix
    修改/usr/local/nginx/conf/nginx.conf
   
    http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
   
    upstream backserver {
server   127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server   127.0.0.1:9080 weight=1 max_fails=2 fail_timeout=30s;
ip_hash;
    }

    server {
listen      80;
server_name  localhost;
index  index.html index.htm;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#对不同的HTTP状态码设置不同的缓存时间
proxy_pass http://backserver
expires      1d;
}

#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;
#}

    }
}

启动NGINX
/usr/local/nginx/sbin/nginx
关闭NGINX
/usr/local/nginx/sbin/nginx -s stop

猜你喜欢

转载自thjjava.iteye.com/blog/2251330