欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx實(shí)現(xiàn)負(fù)載均衡和動(dòng)靜分離

 更新時(shí)間:2018年03月29日 09:29:49   作者:xdxxdx  
這篇文章主要為大家詳細(xì)介紹了nginx實(shí)現(xiàn)負(fù)載均衡和動(dòng)靜分離,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

以下是我的項(xiàng)目用到的一份配置文件

#user nobody;
worker_processes 4; #進(jìn)程數(shù),一般cpu是幾核就寫(xiě)多少

#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;#單個(gè)進(jìn)程的最大連接數(shù)
}


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;

 #第一個(gè)集群
 upstream xdx.com{
 server 119.10.52.28:8081 weight=100;
 server 119.10.52.28:8082 weight=100;
 
 }

 #第二個(gè)集群,用于上傳圖片所用
 upstream xdxfile.com{
 server 119.10.52.28:8081;#關(guān)于文件上傳的請(qǐng)求均訪問(wèn)這個(gè)集群
 }

 #第三個(gè)集群
 upstream xdx8082.com{
 server 119.10.52.28:8082;#8082
 }

 #第四個(gè)集群
 upstream xdxali.com{
 server 139.196.235.228:8082;#阿里云
 }

 #第五個(gè)集群
 upstream xdxaliws.com{
 server 139.196.235.228:8886;#阿里云websocket
 }
#第一個(gè)代理服務(wù)器,監(jiān)聽(tīng)的是80端口,監(jiān)聽(tīng)的域名是www.wonyen.com或者wonyen.com
 server {
 listen 80;#監(jiān)聽(tīng)的端口
 server_name www.wonyen.com wonyen.com;#監(jiān)聽(tīng)的域名

 #charset koi8-r;

 #access_log logs/host.access.log main;


 #location指的是訪問(wèn)的路徑,下面這條配置表示當(dāng)訪問(wèn)網(wǎng)站的根目錄,即訪問(wèn)wonyen.com或者www.wonyen.com的時(shí)候,就去根目錄為html的下面去尋找index.html或者index.htm。在index.html這個(gè)頁(yè)面里面你可以做一些重定向的工作,跳轉(zhuǎn)到指定頁(yè)面

 #也可以自定義到某個(gè)集群
 # location / {
  # root html;
  # index index.html index.htm;
 #}
 #所有靜態(tài)請(qǐng)求都交由nginx處理,存放目錄為webapps下的root,過(guò)期時(shí)間為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結(jié)尾的請(qǐng)求的處理集群為http://xdxfile.com
 location ~ ^/\w+Att{
  proxy_pass http://xdxfile.com;
 }

 #配置以Fill結(jié)尾的請(qǐng)求的處理集群為http://xdxfile.com
 location ~ ^/\w+Fill{
  proxy_pass http://xdxfile.com;
 }

 #精準(zhǔn)配置,如果請(qǐng)求名為/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;
 }
 #其他所有請(qǐng)求都訪問(wèn) http://xdx.com的集群 
  location ~ .*$ {
  index index;
  proxy_pass http://xdx.com;
  }
 #404頁(yè)面訪問(wèn)/Error404.jsp這個(gè)location
 error_page 404  /Error404.jsp;

 #500等頁(yè)面也訪問(wèn) /Error404.jsp這個(gè)location
 error_page 500 502 503 504 /Error404.jsp;

 #配置請(qǐng)求/Error404.jsp就訪問(wèn)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
 #另外一個(gè)代理服務(wù)器,監(jiān)聽(tīng)8886接口,監(jiān)聽(tīng)的域名為www.wonyen.com或者wonyen.com
 server {
 listen 8886;
 server_name www.wonyen.com wonyen.com;
#配置若請(qǐng)求為wonyen.com:8086(根目錄),就讓他去訪問(wèn)http://xdxaliws.com這個(gè)集群,這邊配置的是websocket的服務(wù)端
 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;
 # }
 #}

}

以上就是我的一個(gè)配置?;旧闲枰⒁獾亩荚谂渲梦募凶⒔?。我把幾個(gè)重要的地方單獨(dú)拿出來(lái)講一下。

1.集群的配置,我在上面的配置里有定義多個(gè)集群,集群按字面的意思理解就是由多臺(tái)服務(wù)器構(gòu)成的一個(gè)集合,典型的例子如 

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

這樣的一個(gè)配置,這個(gè)集群包含了兩個(gè)分支,我們可以在兩臺(tái)服務(wù)器上搭建相同的項(xiàng)目(上述的例子是在同樣的服務(wù)器,不同的端口部署相同的項(xiàng)目,因?yàn)楣P者的服務(wù)器有限),當(dāng)有請(qǐng)求是需要這個(gè)集群來(lái)處理的時(shí)候,nginx會(huì)隨機(jī)分配,當(dāng)然也可以配置權(quán)重來(lái)設(shè)置兩個(gè)server的訪問(wèn)概率。這就是負(fù)載均衡的原理。我們?cè)诙嗯_(tái)服務(wù)器上部署相同的項(xiàng)目,利用nginx對(duì)請(qǐng)求進(jìn)行轉(zhuǎn)發(fā),這樣可以降低只有一臺(tái)服務(wù)器所造成的的負(fù)載過(guò)大,而且當(dāng)其中一臺(tái)服務(wù)器掛掉以后,nginx會(huì)分配另外一臺(tái)服務(wù)器來(lái)工作,這樣就不會(huì)造成服務(wù)停止了。

2.server配置項(xiàng)代表的是一個(gè)代理服務(wù)器,上面的文件中我們配置了兩個(gè)文件,分別監(jiān)聽(tīng)wonyen.com(www.wonyen.com)這兩個(gè)域名的80和8886端口,所有訪問(wèn)wonyen.com:80(即wonyen.com)這個(gè)域名下的請(qǐng)求,都按照第一個(gè)server所定義的規(guī)則去轉(zhuǎn)發(fā),而所有訪問(wèn)wonyen.com:8886下的請(qǐng)求,則會(huì)按照第二個(gè)server所定義的規(guī)則去轉(zhuǎn)發(fā)。

3.我們甚至可以通過(guò)配置來(lái)處理多個(gè)域名,看以下的例子。下面的例子我配置了兩個(gè)域名的規(guī)則,一個(gè)是iis服務(wù)器,一個(gè)是tomcat服務(wù)器,主要目的是為了解決80端口只能被一個(gè)程序使用的問(wèn)題。如果iis用了80,tomcat就用不了,反之亦然。所以我給iis和tomcat都分配除了80以外的端口,而把80端口留給niginx。由nginx來(lái)分配請(qǐng)求給不同的網(wǎng)站。

復(fù)制代碼

#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;
 }
  #其他請(qǐng)求 
  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;
 }
  #所有靜態(tài)請(qǐng)求都交由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; 
 }
  #其他請(qǐng)求 
  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.還有一個(gè)就是動(dòng)靜分離,說(shuō)得通俗一點(diǎn)就是,把請(qǐng)求數(shù)據(jù)(動(dòng))與請(qǐng)求圖片(靜)分開(kāi),在tomcat里,當(dāng)我們沒(méi)有做動(dòng)靜分離的時(shí)候,tomcat把對(duì)圖片的請(qǐng)求也會(huì)當(dāng)成一個(gè)動(dòng)態(tài)的請(qǐng)求,而處理動(dòng)態(tài)請(qǐng)求是比較費(fèi)性能的(至于為什么,我也不太清楚)。所以我們可以使用nginx配置來(lái)實(shí)現(xiàn)動(dòng)靜分離。

我的做法是把其中一個(gè)tomcat項(xiàng)目放在nginx的根目錄下,這樣,我們就可以通過(guò)以下方式來(lái)配置,實(shí)現(xiàn)當(dāng)我們?cè)L問(wèn)圖片,js,css等靜態(tài)資源的時(shí)候,都到一個(gè)指定的目錄去訪問(wèn)。這樣做的好處除了節(jié)省性能,還有一個(gè)就是我們不需要在所有的負(fù)載均衡服務(wù)器中都同步保留這些靜態(tài)資源,只需要在一個(gè)地方保留就好了。配置如下

#所有靜態(tài)請(qǐng)求都交由nginx處理,存放目錄為webapps下的root,過(guò)期時(shí)間為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.既然讀取靜態(tài)資源是從這個(gè)目錄讀取的,那么我們必須考慮如何存儲(chǔ)靜態(tài)資源,特別是當(dāng)我們做了負(fù)載均衡以后,在我們的 項(xiàng)目中上傳圖片的請(qǐng)求有可能在任意一個(gè)集群的分支中被調(diào)用,比如我們的集群中有A,B兩臺(tái)服務(wù)器,他們都有可能做上傳圖片這件事情,如果A調(diào)用了上傳圖片這個(gè)請(qǐng)求,則圖片則被上傳到了A這臺(tái)服務(wù)器上,反之就是B上面。這樣勢(shì)必導(dǎo)致A,B兩臺(tái)服務(wù)器上的靜態(tài)圖片是不同步的,當(dāng)我們要訪問(wèn)這些圖片的時(shí)候,(假設(shè)此時(shí)我們還沒(méi)做動(dòng)靜分離)就有可能出現(xiàn)訪問(wèn)不到的情況。由于上一步我們做了動(dòng)靜分離,現(xiàn)在的問(wèn)題就演變?yōu)?,如何把這些A,B服務(wù)器上傳的圖片,都同步到我們做動(dòng)靜分離的那個(gè)文件夾下。人工或者程序去同步都很麻煩,我的做法是指定一臺(tái)服務(wù)器(也就是nginx安裝的那臺(tái)服務(wù)器)的tomcat項(xiàng)目(也就是部署在nginx根目錄下的那個(gè)tomcat項(xiàng)目),讓它專門來(lái)負(fù)責(zé)上傳圖片的工作,這樣所有的圖片都由這個(gè)tomcat項(xiàng)目來(lái)上傳,也就保證了靜態(tài)庫(kù)中的圖片就是完整的圖片。為此我配置了一個(gè)集群,如下。

#第二個(gè)集群,用于上傳圖片所用
 upstream xdxfile.com{
 server 119.10.52.28:8081;#關(guān)于文件上傳的請(qǐng)求均訪問(wèn)這個(gè)集群
 }

然后在location中我這樣配置:

 #配置以Att結(jié)尾的請(qǐng)求的處理集群為http://xdxfile.com
 location ~ ^/\w+Att{
  proxy_pass http://xdxfile.com;
 }

 #配置以Fill結(jié)尾的請(qǐng)求的處理集群為http://xdxfile.com
 location ~ ^/\w+Fill{
  proxy_pass http://xdxfile.com;
 }

因?yàn)槲野阉猩婕暗礁郊蟼鞯恼?qǐng)求都加上了Att或者Fill的后綴,當(dāng)nginx捕獲這些后綴名的請(qǐng)求的時(shí)候,就會(huì)把他們都交給 http://xdxfile.com這個(gè)集群,也就是119.10.52.28:8081這個(gè)項(xiàng)目。

6.做了負(fù)載均衡以后,有一個(gè)不得不面臨的問(wèn)題就是內(nèi)存數(shù)據(jù)的同步,我們?cè)诔绦蛑杏袝r(shí)候會(huì)把一些數(shù)據(jù)存放在內(nèi)存中,典型的一類數(shù)據(jù)就是session。如何讓session數(shù)據(jù)在集群的各個(gè)分支中共享session呢,這邊要用到一個(gè)新的東西,叫做redis。我會(huì)在下一篇文章中詳細(xì)地介紹。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Nginx熱部署的實(shí)現(xiàn)

    Nginx熱部署的實(shí)現(xiàn)

    本文主要介紹了Nginx熱部署的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 詳解CentOS配置Nginx官方的Yum源

    詳解CentOS配置Nginx官方的Yum源

    這篇文章主要介紹了詳解CentOS配置Nginx官方的Yum源,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Nginx上配置Basic Authorization登錄認(rèn)服務(wù)證的教程

    Nginx上配置Basic Authorization登錄認(rèn)服務(wù)證的教程

    現(xiàn)在我們所使用的包括社交網(wǎng)絡(luò)API等開(kāi)放平臺(tái)授權(quán)獲得用戶的用戶名和密碼一般有兩種認(rèn)證方式,一種是Basic Auth,一種是OAuth,這里我們就來(lái)看一下Nginx上配置Basic Authorization登錄認(rèn)服務(wù)證的教程
    2016-06-06
  • linux設(shè)置Nginx自動(dòng)重啟的實(shí)現(xiàn)

    linux設(shè)置Nginx自動(dòng)重啟的實(shí)現(xiàn)

    在Linux系統(tǒng)中,設(shè)置Nginx服務(wù)開(kāi)機(jī)自動(dòng)啟動(dòng)及意外停止后自動(dòng)重啟是保持服務(wù)穩(wěn)定運(yùn)行的關(guān)鍵步驟,本文詳細(xì)介紹了如何使用systemctl命令和配置systemd服務(wù)文件來(lái)實(shí)現(xiàn)這一功能,感興趣的可以了解一下
    2024-09-09
  • 使用referer指令配置Nginx服務(wù)器來(lái)防止圖片盜鏈

    使用referer指令配置Nginx服務(wù)器來(lái)防止圖片盜鏈

    這篇文章主要介紹了使用referer指令配置Nginx服務(wù)器來(lái)防止圖片盜鏈的方法,文中也簡(jiǎn)單介紹了referer指令的一些語(yǔ)法和常用參數(shù),需要的朋友可以參考下
    2015-12-12
  • Nginx服務(wù)500:Internal Server Error原因之一

    Nginx服務(wù)500:Internal Server Error原因之一

    這篇文章主要介紹了Nginx服務(wù)500:Internal Server Error原因之一,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Nginx HTTPS實(shí)現(xiàn)原理及配置實(shí)踐

    Nginx HTTPS實(shí)現(xiàn)原理及配置實(shí)踐

    本文主要介紹了Nginx HTTPS實(shí)現(xiàn)原理及配置實(shí)踐,詳細(xì)的介紹了HTTPS原理,實(shí)現(xiàn)及其HTTPS單臺(tái)配置實(shí)踐,集群配置、優(yōu)化等,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Nginx常用配置以及代理轉(zhuǎn)發(fā)操作詳解

    Nginx常用配置以及代理轉(zhuǎn)發(fā)操作詳解

    這篇文章主要給大家介紹了關(guān)于Nginx常用配置以及代理轉(zhuǎn)發(fā)的相關(guān)資料,nginx一般被用來(lái)做反向代理,將請(qǐng)求轉(zhuǎn)發(fā)到應(yīng)用服務(wù)器上,比如tomcat的應(yīng)用,需要的朋友可以參考下
    2023-09-09
  • 關(guān)于使用Keepalived實(shí)現(xiàn)Nginx的自動(dòng)重啟及雙主熱備高可用問(wèn)題

    關(guān)于使用Keepalived實(shí)現(xiàn)Nginx的自動(dòng)重啟及雙主熱備高可用問(wèn)題

    這篇文章主要介紹了使用Keepalived實(shí)現(xiàn)Nginx的自動(dòng)重啟及雙主熱備高可用,本文通過(guò)幾個(gè)問(wèn)題解析幫助大家學(xué)習(xí)Keepalived實(shí)現(xiàn)Nginx的自動(dòng)重啟的相關(guān)知識(shí),需要的朋友可以參考下
    2021-09-09
  • Nginx性能優(yōu)化之Gzip壓縮設(shè)置詳解(最大程度提高頁(yè)面打開(kāi)速度)

    Nginx性能優(yōu)化之Gzip壓縮設(shè)置詳解(最大程度提高頁(yè)面打開(kāi)速度)

    這篇文章主要介紹了Nginx性能優(yōu)化之Gzip壓縮設(shè)置詳解(最大程度提高頁(yè)面打開(kāi)速度),需要的朋友可以參考下
    2022-01-01

最新評(píng)論