Linode Center OS 6.5(x64) 用Nginx 反向代理多個Tomcat 負載均衡。

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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章