nginx大家如果没用过那或多或少都应该听过,vue的部署、反向代理、负载均衡nginx都能帮你做到。
今天主要说一下nginx负载均衡我们的项目,如下图所示,请求到达nginx,nginx再帮我们转发。
首先使用docker安装nginx.
|
1
|
docker pull nginx:latest
|
运行容器,将本地的8080端口映射到容器内部的 80 端口.
|
1
|
docker run --name nginx -p 8080:80 -d nginx
|
查看nginx容器,如果有错请看日志.
浏览器中访问一下
ok,到此我们的nginx就已安装完成。
我们准备好3个以上的webapi的项目并发布。
进入nginx容器
|
1
|
docker exec -it nginx bash
|
找到nginx.conf文件并作修改,nginx.conf分为http块、events块和server块,此次主要在server块中做更改.
此时在nginx容器里面使用vi或者vim没有用,需要依次执行如下两条命令
|
1
2
|
apt-get update
apt-get install vim
|
进入文件内,末尾处指向了另一个文件,没错这个文件里就是放server块配置内容
进入etc/nginx/conf.d/default.conf文件中并做修改
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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;
#}
}
|
完成之后重启一下容器,如果有错误请查看日志.
|
1
|
docker restart nginx
|
浏览器中调用一个接口查看
每一次都会轮询不同的服务实例,负载均衡的预期就实现了!
我们也可以设置权重比例,weight值越大,请求到达此实例的次数就越多!
|
1
2
3
4
5
6
|
upstream serviceinstance{
#nginx默认轮询下面的服务实例
server ***.**.***.***:9007 weight=1;
server ***.**.***.***:9008 weight=2;
server ***.**.***.***:9009 weight=3;
}
|
各位同学也可慢慢研究,nginx很强大的!
总结
到此这篇关于.net core + nginx实现项目负载均衡的文章就介绍到这了,更多相关.net core+nginx项目负载均衡内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://www.cnblogs.com/zhangnever/p/13288939.html
相关文章
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 93
-
2025-05-27 29
-
2025-05-29 58
-
2025-05-25 37
-
PHP中new static()与new self()的比较
2025-05-29 18










