nginx實現負載均衡、熱備、動靜分離

 

個人淺談,有不對之處請指出,不喜勿碰,謝謝

nginx是一個很好的反向代理服務器,同時能實現負載均衡,熱備,動靜分離;在連接高併發的情況下,Nginx是Apache服務器不錯的替代品。

1. 負載均衡

高併發情況下,目前最好的選擇是dubbo,但是對開發的要求也高,如果併發上不了十萬級別的,可以用使用nginx來實現,可以達到同樣的效果,其人力成本也低!

 

後臺服務器組成了一個服務器集羣(多臺服務器). 有中間服務器(nginx)接受到請求分發給不同的服務器後臺. 該nginx就是一個負載均衡服務器.

nginx1

1.1 負載均衡session問題

負載均衡第一大問題就是要解決session問題,不然會造成用戶要不斷要登錄系統,解決這個問題有兩個思路

①使用ip_hash,根據ip的hash值來實現負載均衡,把用戶按照ip來分配服務器,改用戶一段時間內所有請求都會到同一個服務器,所有session也都在同一個服務器,這也是一個解決方案,也是最簡單的方案。

②使用Redis來共享session,後續會專寫一個文章來說明這個問題。

在upstream myservice裏面把服務器的列出來,weight 可以設置服務器的權重(服務器配置好的,權重就可以給大一點);max_fails最大失敗次數,fail_timeout超時時間;ip_hash根據ip來實現負載均衡

   upstream myservice {   
     # server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=2;
      server 127.0.0.1:8080 backup;#熱備
      server 127.0.0.1:8081 ;
      ip_hash;#指定同一IP只能訪問同一Tomcat,解決session問題
    }

2. 熱備

當某一服務器掛了之後,可以馬上自動切換到備份服務器,這樣給用戶的體驗是很好的;也可以利用這一特性來實現不停服而發佈新版本程序。

upstream myservice 中服務器列表中在備用服務器後面標記 backup,即可實現熱備,但同時要在設置location中超時時間

server 127.0.0.1:8080 backup;#熱備

location / {
             proxy_pass   http://myservice;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #設置了以下配置,當主機掛掉後,備份機可以很快的響應
    proxy_connect_timeout 1;
    proxy_read_timeout 1;
    proxy_send_timeout 1;
         }

3. 動靜分離

可以把靜態的前端頁面的圖片、效果圖、特效等,前移到nginx,減輕Tomcat服務器的壓力;前提是要把這些圖片放到nginx目錄文件下面,並且增加如下配置

# 所有靜態請求都由nginx處理,存放目錄爲toot  
        location ~ \.(gif|jpg|jpeg|png|bmp|swf)$ {  
            root    toot;#存放目錄
   expires 3d;
        } 

 

以下爲nginx.conf配置文件的全部內容:

nginx 版本爲1.8  

 



#user  nobody;
worker_processes  1;#設置與cpu的核心數一致


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;




events {
    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;


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
   
    #gzip  on;
     upstream myservice {   
     # server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=2;
      server 127.0.0.1:8080 backup;
      server 127.0.0.1:8081 ;
      #ip_hash;#指定同一IP只能訪問同一Tomcat,解決session問題
    }
    server {
        listen       80;#監聽端口
        server_name  localhost;#域名


        #charset koi8-r;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}


        #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
        
# 所有靜態請求都由nginx處理,存放目錄爲toot  
        location ~ \.(gif|jpg|jpeg|png|bmp|swf)$ {  
            root    toot;
   expires 3d;
        } 
        location / {
             proxy_pass   http://myservice;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #設置了以下配置,當主機掛掉後,備份機可以很快的響應
    proxy_connect_timeout 1;
    proxy_read_timeout 1;
    proxy_send_timeout 1;
         }



#location / {  
        #   root   html;  
        #   index  index.html index.htm;  
        #}  
 


        # 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       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;


    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}

 

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