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

.Net Core + Nginx實現(xiàn)項目負載均衡的全步驟

 更新時間:2020年07月19日 11:19:01   作者:江北的博客、  
這篇文章主要給大家介紹了關(guān)于.Net Core + Nginx實現(xiàn)項目負載均衡的相關(guān)資料,文中通過示例代碼以及圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

nginx大家如果沒用過那或多或少都應(yīng)該聽過,vue的部署、反向代理、負載均衡nginx都能幫你做到。

今天主要說一下nginx負載均衡我們的項目,如下圖所示,請求到達nginx,nginx再幫我們轉(zhuǎn)發(fā)。

首先使用Docker安裝nginx.

docker pull nginx:latest

運行容器,將本地的8080端口映射到容器內(nèi)部的 80 端口.

docker run --name nginx -p 8080:80 -d nginx

查看nginx容器,如果有錯請看日志.

瀏覽器中訪問一下

ok,到此我們的nginx就已安裝完成。

我們準備好3個以上的webapi的項目并發(fā)布。

進入nginx容器

Docker exec -it nginx bash

找到nginx.conf文件并作修改,nginx.conf分為http塊、events塊和server塊,此次主要在server塊中做更改.

此時在nginx容器里面使用vi或者vim沒有用,需要依次執(zhí)行如下兩條命令

apt-get update 
apt-get install vim

進入文件內(nèi),末尾處指向了另一個文件,沒錯這個文件里就是放server塊配置內(nèi)容

進入etc/nginx/conf.d/default.conf文件中并做修改

upstream ServiceInstance{   #nginx默認輪詢下面的服務(wù)實例
  server ***.**.***.***:9007; 
  server ***.**.***.***:9008; 
  server ***.**.***.***:9009;
} 
server { 
  listen    80; 
  server_name localhost; 
 
  #charset koi8-r; 
  #access_log /var/log/nginx/host.access.log main; 
 
  location / { 
    #root  /usr/share/nginx/html; 
    #index index.html index.htm;     #請求到達后會進行轉(zhuǎn)發(fā)
    proxy_pass http://ServiceInstance; 
  } 
 
  #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  /usr/share/nginx/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; 
  #} 
}

完成之后重啟一下容器,如果有錯誤請查看日志.

docker restart nginx

瀏覽器中調(diào)用一個接口查看

每一次都會輪詢不同的服務(wù)實例,負載均衡的預(yù)期就實現(xiàn)了!

我們也可以設(shè)置權(quán)重比例,weight值越大,請求到達此實例的次數(shù)就越多!

upstream ServiceInstance{ 
  #nginx默認輪詢下面的服務(wù)實例
  server ***.**.***.***:9007 weight=1; 
  server ***.**.***.***:9008 weight=2; 
  server ***.**.***.***:9009 weight=3;
}

各位同學(xué)也可慢慢研究,nginx很強大的!😎

總結(jié)

到此這篇關(guān)于.Net Core + Nginx實現(xiàn)項目負載均衡的文章就介紹到這了,更多相關(guān).Net Core+Nginx項目負載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論