網址:https://blog.csdn.net/weixin_40779637/article/details/113932374?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-8.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-8.control
目錄
一、下載Nginx
二、配置Nginx
1、修改配置文件:nginx-1.18.0\conf\nginx.conf
2、大概配置如下,詳細配置請參考官網鏈接
三、啟動Nginx
四、負載均衡測試
1、啟動多個 .net core 項目
2、訪問Nginx配置監聽地址
Linux系統Nginx代理.Net Core項目
一、下載Nginx
官網下載請自行搜索
下載地址:https://download.csdn.net/download/weixin_40779637/15419408
二、配置Nginx
本站下載的配置好了
1、修改配置文件:nginx-1.18.0\conf\nginx.conf
2、大概配置如下,詳細配置請參考官網鏈接
worker_processes 1;
events {
worker_connections 1024;
}
http {
#負載均衡方法
upstream 負載均衡名稱 {
# no load balancing method is specified for round robin
# less_conn:最少連接,ip_hash:IP哈希
server URL1 weight=5;
server URL2 weight=3;
server URL3 weight=2;
# weight 權重
}
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#gzip on;
server {
#監聽本機8001
listen 8001;
#服務器地址,localhost代表本機
server_name localhost;
location / {
# 負載均衡名稱 配置中server的URL為全地址時直接名稱,否則HTTP//負載均衡名稱或HTTPS://負載均衡名稱
proxy_pass 負載均衡名稱;
#主動健康檢查 --只有plus支持,收費功能
health_check;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#可以有多個 http://nginx.org/en/docs/beginners_guide.html
server {
}
}
參考:https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#method
參考:https://nginx.org/en/docs/http/ngx_http_upstream_module.html
參考:多個server http://nginx.org/en/docs/beginners_guide.html
三、啟動Nginx
雙擊可執行文件:nginx.exe或命令:nginx啟動
參考:http://nginx.org/en/docs/beginners_guide.html
四、負載均衡測試
1、啟動多個 .net core 項目
a、新建項目並編譯
/// <summary>
/// 控制器代碼
/// </summary>
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet("[action]")]
public string GetIp()
{
return "IP:"+ Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + " 端口:" + Request.HttpContext.Connection.LocalPort;
}
}
b、編譯文件夾命令啟動
一定要多個命令窗口、或多個IIS站點、或多個docker容器
# 啟動命令
dotnet DockerRelease.dll --urls=http://*:9002 --ip="127.0.0.1" --port=9001
dotnet DockerRelease.dll --urls=http://*:9002 --ip="127.0.0.1" --port=9003
dotnet DockerRelease.dll --urls=http://*:9002 --ip="127.0.0.1" --port=9003
2、訪問Nginx配置監聽地址
請求Nginx監聽的9000端口,負載到9001、2、3三個系統實例。
http://localhost:9000/WeatherForecast/getip