天天看點

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

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

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

1. 負載均衡

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

背景伺服器組成了一個伺服器叢集(多台伺服器). 有中間伺服器(nginx)接受到請求分發給不同的伺服器背景. 該nginx就是一個負載均衡伺服器.

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

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;

    #    }

    #}

}

繼續閱讀