Linode Center OS 6.5(x64) 用Nginx 反向代理多个Tomcat 负载均衡。

#安装Java JDK

yum install java-1.7.0-openjdk.x86_64

#安装Tomcat

cd /usr/local

mkdir tomcat

wget http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.37/bin/apache-tomcat-6.0.37.tar.gz

tar -zxf apache-tomcat-6.0.37.tar.gz

mv apache-tomcat-6.0.37 tomcat6-8080

cp -R tomcat6-8080 tomcat6-8081

vim tomcat6-8081/conf/server.xml

#修改以下位置  

<Server port="8005" shutdown="SHUTDOWN"> 

<Server port="8006" shutdown="SHUTDOWN"> 

<Connector port="8080" protocol="HTTP/1.1" 

               connectionTimeout="20000" 

               redirectPort="8443" />

<Connector port="8081" protocol="HTTP/1.1" 

               connectionTimeout="20000" 

               redirectPort="8444" />

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />

#安装Nginx

#1.安装所需环境

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-server krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

yum install pcre-devel zlib-devel

#2.安装Nginx

wget http://nginx.org/download/nginx-1.0.14.tar.gz

tar xzf nginx-1.0.14.tar.gz

cd nginx-1.0.14

./configure --prefix=/usr/local/nginx

make && make install

#配置Nginx实现负载均衡

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

vim /usr/local/nginx/conf/nginx.conf 

    删除 nginx.conf 中全部内容 并加入以下内容

#运行用户#user  nobody;#开启进程数 <=CPU数worker_processes  2;#错误日志保存位置#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#进程号保存文件#pid        logs/nginx.pid;#等待事件events {    #Linux下打开提高性能    #use epoll;    #每个进程最大连接数(最大连接=连接数x进程数)    worker_connections  1024;}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;         #设定请求缓冲    client_header_buffer_size 1k;    large_client_header_buffers 4 4k;    #打开发送文件    sendfile        on;    #tcp_nopush    on;    #keepalive_timeout  0;    keepalive_timeout  65;         #客户端上传文件大小控制    client_max_body_size 8m;         #打开gzip压缩    #gzip  on;         #设定负载均衡的服务器列表    #upstream mysvr {    #    #weigth参数表示权值,权值越高被分配到的几率越大    #    #本机上的Squid开启3128端口    #    #server 192.168.8.1:3128 weight=5;    #    #server 192.168.8.2:80 weight=1;    #    #server 192.168.8.3:80 weight=6;    #}    #第一个虚拟主机        upstream mytomcats {        server 127.0.0.1:8080;        server 127.0.0.1:8081;      }      server {        #监听IP端口        listen      80;        #主机名        server_name  localhost;        #root  	root /usr/local/tomcat/tomcat6-8080/webapps/ROOT;        #设置字符集        #charset koi8-r;        #本虚拟server的访问日志 相当于局部变量        #access_log  logs/host.access.log  main;        #日志文件输出格式        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '        #                  '$status $body_bytes_sent "$http_referer" '        #                  '"$http_user_agent" "$http_x_forwarded_for"';                 #location / {         #   root  html;          #  index  index.html index.htm;        #}        	location ~ (\.html)|(\.jsp)$ {     		 proxy_pass http://mytomcats;  	   	 proxy_redirect off;     		 proxy_set_header Host $host;     		 proxy_set_header X-Real-IP $remote_addr;     		 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;     		 client_max_body_size 10m;     		 client_body_buffer_size 128k;     		 proxy_connect_timeout 90;     		 proxy_send_timeout 90;     		 proxy_read_timeout 90;     		 proxy_buffer_size 4k;     		 proxy_buffers 4 32k;     		 proxy_busy_buffers_size 64k;     		 proxy_temp_file_write_size 64k;  	}          #静态文件缓存时间设置        #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${                #    expires 30d;        #}                 #静态文件缓存时间设置        #location ~ .*\.(js|css)?${                #    expires 1h;        #}                 #对本server"/"启用负载均衡        #location / {        #    proxy_pass http://mysvr;        #    proxy_redirect off;        #    proxy_set_header Host $host;        #    proxy_set_header X-Real-IP $remote_addr;        #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        #    client_max_body_size 10m;        #    client_body_buffer_size 128k;        #    proxy_connect_timeout 90;        #    proxy_send_timeout 90;        #    proxy_read_timeout 90;        #    proxy_buffer_size 4k;        #    proxy_buffers 4 32k;        #    proxy_busy_buffers_size 64k;        #    proxy_temp_file_write_size 64k;        #}                 #设定查看Nginx状态的地址        #location /NginxStatus {        #    stub_status on;        #    access_log on;        #    auth_basic “NginxStatus”;        #    auth_basic_user_file conf/htpasswd;        #}          #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;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }     # another virtual host using mix of IP-, name-, and port-based configuration    #    server {        #多监听      #        listen      8666;        #主机名#        server_name localhost;        #WEB文件路径#        root       /tmp;        #默认首页#        index        HomePage.html;                #location / {        #    #这里相当于局部变量        #    root  E:/Portal;        #    index  HomePage.html;        #}#    }     # HTTPS server HTTPS SSL加密服务器    #    #server {    #    listen      443;    #    server_name  localhost;    #    ssl                  on;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_timeout  5m;    #    ssl_protocols  SSLv2 SSLv3 TLSv1;    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root  html;    #        index  index.html index.htm;    #    }    #}}

#启动Tomcat

/usr/local/tomcat/tomcat6-8080/bin/startup.sh

/usr/local/tomcat/tomcat6-8081/bin/startup.sh

#启动Nginx 

/usr/local/tomcat/nginx/sbin/nginx

OK DONE.