一台服务器同时跑多个tomcat,并做nginx负载均衡 

 

系统上跑多个tomcat的时候,用apache-tomcat-6.0.18.tar.gz 压缩包文件,
1.使用压缩版的tomcat不能使用安装版的。  
2.第一个tomcat的配置不变。
[root@localhost ~]# cp -rp tomcat/*  tomcat2  #复制之前解压的tomcat文件,或重新解压一份,改个名字
[root@localhost ~]# vim tomcat2/conf/server.xml (改3个端口即可)
22 <Server port="8005" shutdown="SHUTDOWN">  #将8005改为其他,如:9005
67 <Connector port="8080" protocol="HTTP/1.1" #将8080改为其他,如:9090
88 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> #将8009改为其他,如:9009
[root@localhost ~]# mkdir -p /home/www/
[root@localhost www]# cd /home/www/
[root@localhost www]# mkdir ROOT/
[root@localhost www]# cd ROOT/
[root@localhost ROOT]# echo " My web 2" > index.jsp 
[root@localhost ~]# vim tomcat2/conf/server.xml 
126       <Host name="localhost"  appBase="/home/www"
注:保存退出,重新启动linux系统之后,先启动tomcat2,在启动tomcat,否则报端口错误被占用。
到此,服务器上跑多个tomcat的配置就完成了。下面是nginx负载均衡
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf   #做nginx负载,添加如下红色部分
#user  nobody;
worker_processes  4;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
worker_rlimit_nofile 65535; 
pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    #include /usr/local/nginx/conf/proxy.conf;
    server_names_hash_bucket_size 128; 
    client_header_buffer_size 32k; 
    large_client_header_buffers 4 32k; 
    #client_max_body_size 8m;

    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;
    tcp_nodelay on;

    #gzip  on;

    upstream  www.benet.com  {
               server   192.168.0.160:8080;   weight=1  #权重,不写默认是轮询模式
               server   192.168.0.160:9090;   weight=3
       }


    server {
        listen       80;
        server_name  www.benet.com;
        index index.html index.htm index.jsp;
        root  /home/www/ROOT;
    location / {
        proxy_pass        
http://www.benet.com;

        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

                }

        location ~ .*.jsp$ {
        index index.jsp; 
        proxy_pass http://localhost:8080;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d; 
        }
        location ~ .*\.(js|css)?$ {
        expires      1h; 
        }