Nginx+tomcat實現負載均衡


第一步:安裝和配置nginx,可參考:http://www.cnblogs.com/klslb/p/8962379.html

第二步:准備三個tomcat。

我這里准備了三個tomcat並重新命名方便區分。

修改每個tomcat啟動端口。並牢記!

為了區分是哪個tomcat,進入webapps\ROOT,修改index.jsp.的標題。

 

第三步:打開nginx安裝目錄,找到conf目錄下的nginx.conf,,把下面配置文件全部替換。

  1 #Nginx所用用戶和組,window下不指定 
  2 #user  nobody;
  3 
  4 #工作的子進程數量(通常等於CPU數量或者2倍於CPU)  
  5 worker_processes  4;
  6 
  7 #錯誤日志存放路徑 
  8 #error_log  logs/error.log;
  9 #error_log  logs/error.log  notice;
 10 error_log  logs/error.log  info;
 11 
 12 #指定pid存放文件  
 13 #pid        logs/nginx.pid;
 14 
 15 
 16 events {
 17     #允許最大連接數
 18     worker_connections  1024;
 19 
 20 }
 21 
 22 
 23 http {
 24     include       mime.types;
 25     default_type  application/octet-stream;
 26 
 27     access_log  logs/access.log;
 28     client_header_timeout  3m;  
 29     client_body_timeout    3m;  
 30     send_timeout           3m;  
 31 
 32     client_header_buffer_size    1k;  
 33     large_client_header_buffers  4 4k;  
 34 
 35     sendfile        on;  
 36     tcp_nopush      on;  
 37     tcp_nodelay     on;
 38 
 39     # 配置負載均衡
 40     upstream localhost{
 41         #Nginx是如何實現負載均衡的,Nginx的upstream目前支持以下幾種方式的分配
 42            #1、輪詢(默認)
 43         #每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
 44         #2、weight
 45         #指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。
 46         #2、ip_hash
 47         #每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
 48         #3、fair(第三方)
 49         #按后端服務器的響應時間來分配請求,響應時間短的優先分配。
 50         #4、url_hash(第三方)
 51         #按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。  
 52  ip_hash;

53       #本機IP+三個tomcat端口號 54  server 192.168.12.209:8081 weight=1; 55 server 192.168.12.209:8082 weight=2; 56 server 192.168.12.209:8083 weight=3; 57 } 58 59 server { 60 listen 80; 61 server_name localhost; 62 63 #charset koi8-r; 64 65 #access_log logs/host.access.log main; 66 67 location / { 68 root html; 69 index index.html index.htm; 70 proxy_connect_timeout 3; #nginx跟后端服務器連接超時時間(代理連接超時) 71 proxy_send_timeout 30; #后端服務器數據回傳時間(代理發送超時) 72 proxy_read_timeout 30; #連接成功后,后端服務器響應時間(代理接收超時) 73 proxy_pass http://localhost; 74 } 75 76 #error_page 404 /404.html; 77 78 # redirect server error pages to the static page /50x.html 79 # 80 error_page 500 502 503 504 /50x.html; 81 location = /50x.html { 82 root html; 83 } 84 85 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 86 # 87 #location ~ \.php$ { 88 # proxy_pass http://127.0.0.1; 89 #} 90 91 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 92 # 93 #location ~ \.php$ { 94 # root html; 95 # fastcgi_pass 127.0.0.1:9000; 96 # fastcgi_index index.php; 97 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 98 # include fastcgi_params; 99 #} 100 101 # deny access to .htaccess files, if Apache's document root 102 # concurs with nginx's one 103 # 104 #location ~ /\.ht { 105 # deny all; 106 #} 107 } 108 109 110 # another virtual host using mix of IP-, name-, and port-based configuration 111 # 112 #server { 113 # listen 8000; 114 # listen somename:8080; 115 # server_name somename alias another.alias; 116 117 # location / { 118 # root html; 119 # index index.html index.htm; 120 # } 121 #} 122 123 124 # HTTPS server 125 # 126 #server { 127 # listen 443 ssl; 128 # server_name localhost; 129 130 # ssl_certificate cert.pem; 131 # ssl_certificate_key cert.key; 132 133 # ssl_session_cache shared:SSL:1m; 134 # ssl_session_timeout 5m; 135 136 # ssl_ciphers HIGH:!aNULL:!MD5; 137 # ssl_prefer_server_ciphers on; 138 139 # location / { 140 # root html; 141 # index index.html index.htm; 142 # } 143 #} 144 145 }

 

第四步:分別啟動三個tomcat。重啟nginx。瀏覽器訪問localhost。(啟動命令:nginx -s reload)

可以看到隨機訪問三個tomcat,可以配置weight指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。優先級訪問

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM