Nginx+tomcat 實現負載均衡和動靜分離

轉載於:http://blog.csdn.net/Yoara/article/details/41803153


 怎麼下載和安裝nginx/tomcat就不說了,談談在剛開始配置時最容易讓人模糊的地方。


    1.配置動靜分離和負載均衡,注意,upstram、server都是在http{}下面的。

upstream t1.test.com {
server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s; 
}

    其中,紅色標註定義的服務器組的名字,後續通過改名字制定服務器組。

server {
listen       80;
#server 
server_name  t1.test.com;


location / {
proxy_connect_timeout   3;
proxy_send_timeout      30;
proxy_read_timeout      30;
proxy_pass http://t1.test.com;
}


#配置Nginx動靜分離   
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /data/www;
#expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力.
expires 3d;
}
}

    特別注意,藍色標註的是指域名,就是通過訪問http://t1.test.com   將使用該server定義。後面紅色標註,指的是使用upstream中定義的服務器組。這樣,訪問http://t1.test.com域名的所有請求都會被server   t1.test.com處理。


    2.理解了upstream的使用方法,負載均衡就不是問題了,weight表示權重,max_fails=2 fail_timeout=30s這兩個參數是指,30s內請求失敗2次就不會再訪問該server了。fail_timeout時間過後才繼續訪問,這是容錯的一種處理,服務器如果恢復了,還是能繼續使用。完整的配置如下


[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. upstream t1.test.com {  

  2.     server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s;   

  3. }  

  4. upstream t2.test.com {  

  5.     server 192.168.235.1:8081 weight=1 max_fails=2 fail_timeout=30s;     

  6. }  

  7. upstream all.test.com {  

  8.     server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s;  

  9.     server 192.168.235.1:8081 weight=1 max_fails=2 fail_timeout=30s;     

  10. }  

  11. server {  

  12.     listen       80;  

  13.     #server   

  14.     server_name  t1.test.com;  

  15.   

  16.     location / {  

  17.         proxy_connect_timeout   3;  

  18.         proxy_send_timeout      30;  

  19.         proxy_read_timeout      30;  

  20.         proxy_pass http://t1.test.com;  

  21.     }  

  22.   

  23.     #配置Nginx動靜分離     

  24.     location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {  

  25.         root /data/www;  

  26.         #expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力.  

  27.         expires 3d;  

  28.     }  

  29. }  

  30. server {  

  31.     listen       80;  

  32.     #server   

  33.     server_name  t2.test.com;  

  34.   

  35.     location / {  

  36.         proxy_connect_timeout   3;  

  37.         proxy_send_timeout      30;  

  38.         proxy_read_timeout      30;  

  39.         proxy_pass http://t2.test.com;  

  40.     }  

  41.   

  42.     #配置Nginx動靜分離     

  43.     location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {  

  44.         root /data/www;  

  45.         #expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力.  

  46.         expires 3d;  

  47.     }  

  48. }  

  49. server {  

  50.     listen       80;  

  51.     #server   

  52.     server_name  all.test.com;  

  53.   

  54.     location / {  

  55.         proxy_connect_timeout   3;  

  56.         proxy_send_timeout      30;  

  57.         proxy_read_timeout      30;  

  58.         proxy_pass http://all.test.com;  

  59.     }  

  60.   

  61.     #配置Nginx動靜分離     

  62.     location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {  

  63.         root /data/www;  

  64.         #expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力.  

  65.         expires 3d;  

  66.     }  


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