.Net Core+Nginx实现项目负载均衡

栏目: IT技术 · 发布时间: 4年前

内容简介:nginx大家如果没用过那或多或少都应该听过,vue的部署、反向代理、负载均衡nginx都能帮你做到。今天主要说一下nginx负载均衡我们的项目,如下图所示,请求到达nginx,nginx再帮我们转发。

nginx大家如果没用过那或多或少都应该听过,vue的部署、反向代理、负载均衡nginx都能帮你做到。

今天主要说一下nginx负载均衡我们的项目,如下图所示,请求到达nginx,nginx再帮我们转发。

.Net Core+Nginx实现项目负载均衡

首先使用 Docker 安装nginx.

docker pull nginx:latest

运行容器,将本地的8080端口映射到容器内部的 80 端口.

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

查看nginx容器,如果有错请看日志.

.Net Core+Nginx实现项目负载均衡

浏览器中访问一下

.Net Core+Nginx实现项目负载均衡

ok,到此我们的nginx就已安装完成。

我们准备好3个以上的webapi的项目并发布。

.Net Core+Nginx实现项目负载均衡

进入nginx容器

Docker exec -it nginx bash

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

.Net Core+Nginx实现项目负载均衡

此时在nginx容器里面使用vi或者vim没有用,需要依次执行如下两条命令

apt-get update  
apt-get install vim

进入文件内,末尾处指向了另一个文件,没错这个文件里就是放server块配置内容

.Net Core+Nginx实现项目负载均衡

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

.Net Core+Nginx实现项目负载均衡

.Net Core+Nginx实现项目负载均衡

upstream ServiceInstance{ 
    #nginx默认轮询下面的服务实例
    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; 
        #请求到达后会进行转发
        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

浏览器中调用一个接口查看

.Net Core+Nginx实现项目负载均衡

每一次都会轮询不同的服务实例,负载均衡的预期就实现了!

我们也可以设置权重比例,weight值越大,请求达到此实例的次数就越多!

upstream ServiceInstance{ 
    #nginx默认轮询下面的服务实例
    server ***.**.***.***:9007 weight=1; 
    server ***.**.***.***:9008 weight=2; 
    server ***.**.***.***:9009 weight=3;
} 

各位同学也可慢慢研究,nginx很强大的! :sunglasses:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learning PHP, MySQL, and JavaScript

Learning PHP, MySQL, and JavaScript

Robin Nixon / O'Reilly Media / 2009-7-21 / USD 39.99

Learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript - whether or not you know how to program. This simple, streamlined guide explains how the powerful combination of P......一起来看看 《Learning PHP, MySQL, and JavaScript》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码