1. 程式人生 > >nginx實現負載均衡和動靜分離

nginx實現負載均衡和動靜分離

這篇文章主要為大家詳細介紹了nginx實現負載均衡和動靜分離,具有一定的參考價值,感興趣的小夥伴們可以參考一下

nginx配置(windows配置),供大家參考,具體內容如下

以下是我的專案用到的一份配置檔案

#user nobody;
worker_processes 4; #程序數,一般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;
 keepalive_timeout 65;
 proxy_connect_timeout 15s; 
 proxy_send_timeout 15s; 
 proxy_read_timeout 15s;
 fastcgi_buffers 8 128k;

 gzip on;
 client_max_body_size 30m;
 gzip_min_length 1k;
 gzip_buffers 16 64k;
 gzip_http_version 1.1;
 gzip_comp_level 6;
 gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp;
 gzip_vary on;

 #第一個叢集
 upstream xdx.com{
 server 119.10.52.28:8081 weight=100;
 server 119.10.52.28:8082 weight=100;
 
 }

 #第二個叢集,用於上傳圖片所用
 upstream xdxfile.com{
 server 119.10.52.28:8081;#關於檔案上傳的請求均訪問這個叢集
 }

 #第三個叢集
 upstream xdx8082.com{
 server 119.10.52.28:8082;#8082
 }

 #第四個叢集
 upstream xdxali.com{
 server 139.196.235.228:8082;#阿里雲
 }

 #第五個叢集
 upstream xdxaliws.com{
 server 139.196.235.228:8886;#阿里雲websocket
 }
#第一個代理伺服器,監聽的是80埠,監聽的域名是www.wonyen.com或者wonyen.com
 server {
 listen 80;#監聽的埠
 server_name www.wonyen.com wonyen.com;#監聽的域名

 #charset koi8-r;

 #access_log logs/host.access.log main;


 #location指的是訪問的路徑,下面這條配置表示當訪問網站的根目錄,即訪問wonyen.com或者www.wonyen.com的時候,就去根目錄為html的下面去尋找index.html或者index.htm。在index.html這個頁面裡面你可以做一些重定向的工作,跳轉到指定頁面

 #也可以自定義到某個叢集
 # location / {
  # root html;
  # index index.html index.htm;
 #}
 #所有靜態請求都交由nginx處理,存放目錄為webapps下的root,過期時間為30天
  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ { 
  root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT; 
  expires 30d; 
 }

 #配置以Att結尾的請求的處理叢集為http://xdxfile.com
 location ~ ^/\w+Att{
  proxy_pass http://xdxfile.com;
 }

 #配置以Fill結尾的請求的處理叢集為http://xdxfile.com
 location ~ ^/\w+Fill{
  proxy_pass http://xdxfile.com;
 }

 #精準配置,如果請求名為/crowdFundSave,則
 location = /crowdFundSave{
  proxy_pass http://xdxfile.com;
 }

 #精確配置,同上
 location = /crowdFundRewardSave{
  proxy_pass http://xdxfile.com;
 }

 #精確配置,同上
 location = /garbageCategorySave{
  proxy_pass http://xdxfile.com;
 }

 #精確配置,同上
 location = /mailTestAjax{
  proxy_pass http://xdx8082.com;
 }

 #精確配置,同上
 location = /mailSendAjax{
  proxy_pass http://xdx8082.com;
 }

 #精確配置,同上
 location = /mailOldAjax{
  proxy_pass http://xdx8082.com;
 }

 #精確配置,同上
 #location = /wechatAuthority{
  #proxy_pass http://xdxali.com;
 #}
 location ~ ^/ueditor1_4_3{
  proxy_pass http://xdxfile.com;
 }
 #其他所有請求都訪問 http://xdx.com的叢集 
  location ~ .*$ {
  index index;
  proxy_pass http://xdx.com;
  }
 #404頁面訪問/Error404.jsp這個location
 error_page 404  /Error404.jsp;

 #500等頁面也訪問 /Error404.jsp這個location
 error_page 500 502 503 504 /Error404.jsp;

 #配置請求/Error404.jsp就訪問http://xdxfile.com叢集
 location = /Error404.jsp {
  proxy_pass http://xdxfile.com;
 }

 # 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
 #另外一個代理伺服器,監聽8886介面,監聽的域名為www.wonyen.com或者wonyen.com
 server {
 listen 8886;
 server_name www.wonyen.com wonyen.com;
#配置若請求為wonyen.com:8086(根目錄),就讓他去訪問http://xdxaliws.com這個叢集,這邊配置的是websocket的服務端
 location / {
  proxy_pass http://xdxaliws.com;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
 }
 }


 # 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;
 # }
 #}

}

以上就是我的一個配置。基本上需要注意的都在配置檔案中註解。我把幾個重要的地方單獨拿出來講一下。

1.叢集的配置,我在上面的配置裡有定義多個叢集,叢集按字面的意思理解就是由多臺伺服器構成的一個集合,典型的例子如 

upstream xdx.com{
 server 119.10.52.28:8081 weight=100;
 server 119.10.52.28:8082 weight=100;
 
 }

這樣的一個配置,這個叢集包含了兩個分支,我們可以在兩臺伺服器上搭建相同的專案(上述的例子是在同樣的伺服器,不同的埠部署相同的專案,因為筆者的伺服器有限),當有請求是需要這個叢集來處理的時候,nginx會隨機分配,當然也可以配置權重來設定兩個server的訪問概率。這就是負載均衡的原理。我們在多臺伺服器上部署相同的專案,利用nginx對請求進行轉發,這樣可以降低只有一臺伺服器所造成的的負載過大,而且當其中一臺伺服器掛掉以後,nginx會分配另外一臺伺服器來工作,這樣就不會造成服務停止了。

2.server配置項代表的是一個代理伺服器,上面的檔案中我們配置了兩個檔案,分別監聽wonyen.com(www.wonyen.com)這兩個域名的80和8886埠,所有訪問wonyen.com:80(即wonyen.com)這個域名下的請求,都按照第一個server所定義的規則去轉發,而所有訪問wonyen.com:8886下的請求,則會按照第二個server所定義的規則去轉發。

3.我們甚至可以通過配置來處理多個域名,看以下的例子。下面的例子我配置了兩個域名的規則,一個是iis伺服器,一個是tomcat伺服器,主要目的是為了解決80埠只能被一個程式使用的問題。如果iis用了80,tomcat就用不了,反之亦然。所以我給iis和tomcat都分配除了80以外的埠,而把80埠留給niginx。由nginx來分配請求給不同的網站。

複製程式碼

#user nobody;
worker_processes 1;

#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;
 keepalive_timeout 65;

 gzip on;
 client_max_body_size 30m;
 gzip_min_length 1k;
 gzip_buffers 16 64k;
 gzip_http_version 1.1;
 gzip_comp_level 6;
 gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp;
 gzip_vary on;
 upstream achina.com{
 server 120.76.129.218:81;
 
 }
 upstream qgrani.com{
 server 120.76.129.218:8080;
 
 }

 server {
 listen 80;
 server_name www.achinastone.com achinastone.com;

 #charset koi8-r;

 #access_log logs/host.access.log main;

 location / {
  root html;
  index index.html index.htm;
 }
  #其他請求 
  location ~ .*$ {
  index index;
  proxy_pass http://achina.com;
  }

 #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 80;
 server_name www.qgranite.com qgranite.com;

 location / {
  root html;
  index index.html index.htm;
 }
  #所有靜態請求都交由nginx處理,存放目錄為webapp
  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v)$ { 
  root apache-tomcat-8.0.9\webapps\ROOT; 
  expires 30d; 
 }
  #其他請求 
  location ~ .*$ {
  index index;
  proxy_pass http://qgrani.com;
  }
 }


 # 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;
 # }
 #}

}

4.還有一個就是動靜分離,說得通俗一點就是,把請求資料(動)與請求圖片(靜)分開,在tomcat裡,當我們沒有做動靜分離的時候,tomcat把對圖片的請求也會當成一個動態的請求,而處理動態請求是比較費效能的(至於為什麼,我也不太清楚)。所以我們可以使用nginx配置來實現動靜分離。

我的做法是把其中一個tomcat專案放在nginx的根目錄下,這樣,我們就可以通過以下方式來配置,實現當我們訪問圖片,js,css等靜態資源的時候,都到一個指定的目錄去訪問。這樣做的好處除了節省效能,還有一個就是我們不需要在所有的負載均衡伺服器中都同步保留這些靜態資源,只需要在一個地方保留就好了。配置如下

#所有靜態請求都交由nginx處理,存放目錄為webapps下的root,過期時間為30天
  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ { 
  root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT; 
  expires 30d; 
 }

5.既然讀取靜態資源是從這個目錄讀取的,那麼我們必須考慮如何儲存靜態資源,特別是當我們做了負載均衡以後,在我們的 專案中上傳圖片的請求有可能在任意一個叢集的分支中被呼叫,比如我們的叢集中有A,B兩臺伺服器,他們都有可能做上傳圖片這件事情,如果A呼叫了上傳圖片這個請求,則圖片則被上傳到了A這臺伺服器上,反之就是B上面。這樣勢必導致A,B兩臺伺服器上的靜態圖片是不同步的,當我們要訪問這些圖片的時候,(假設此時我們還沒做動靜分離)就有可能出現訪問不到的情況。由於上一步我們做了動靜分離,現在的問題就演變為,如何把這些A,B伺服器上傳的圖片,都同步到我們做動靜分離的那個資料夾下。人工或者程式去同步都很麻煩,我的做法是指定一臺伺服器(也就是nginx安裝的那臺伺服器)的tomcat專案(也就是部署在nginx根目錄下的那個tomcat專案),讓它專門來負責上傳圖片的工作,這樣所有的圖片都由這個tomcat專案來上傳,也就保證了靜態庫中的圖片就是完整的圖片。為此我配置了一個叢集,如下。

#第二個叢集,用於上傳圖片所用
 upstream xdxfile.com{
 server 119.10.52.28:8081;#關於檔案上傳的請求均訪問這個叢集
 }

然後在location中我這樣配置:

 #配置以Att結尾的請求的處理叢集為http://xdxfile.com
 location ~ ^/\w+Att{
  proxy_pass http://xdxfile.com;
 }

 #配置以Fill結尾的請求的處理叢集為http://xdxfile.com
 location ~ ^/\w+Fill{
  proxy_pass http://xdxfile.com;
 }

因為我把所有涉及到附件上傳的請求都加上了Att或者Fill的字尾,當nginx捕獲這些字尾名的請求的時候,就會把他們都交給 http://xdxfile.com這個叢集,也就是119.10.52.28:8081這個專案。

6.做了負載均衡以後,有一個不得不面臨的問題就是記憶體資料的同步,我們在程式中有時候會把一些資料存放在記憶體中,典型的一類資料就是session。如何讓session資料在叢集的各個分支中共享session呢,這邊要用到一個新的東西,叫做redis。