时间:2023-07-14 23:48:02 | 来源:网站运营
时间:2023-07-14 23:48:02 来源:网站运营
Nginx负载均衡配置、限流配置、Https配置详解:upstream myApiTest { server localhost:9001 weight=10; server localhost:9002 weight=5; server localhost:9003 max_fails=3 fail_timeout=30s; server localhost:9004 backup; server localhost:9005 down; }
worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream myApiTest { server localhost:9001; server localhost:9002; server localhost:9003; } server { listen 8080; server_name xxx; #随意配置一个即可,优先走代理地址 location / { proxy_pass http://myApiTest; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
补充其他参数说明:upstream myApiTest { server localhost:9001 backup; server localhost:9002 down; server localhost:9003;}
(2).轮询+权重worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream myApiTest { server localhost:9001 weight=10; server localhost:9002 weight=5; server localhost:9003; } server { listen 8080; server_name xxx; #随意配置一个即可,优先走代理地址 location / { proxy_pass http://myApiTest; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
(3). ip_hashworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream myApiTest { ip_hash; #开启ip_hash策略 server localhost:9001; server localhost:9002; server localhost:9003; } server { listen 8080; server_name xxx; #随意配置一个即可,优先走代理地址 location / { proxy_pass http://myApiTest; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
(4). url_hash[Route("api/[controller]/[action]")] [ApiController] public class HomeController : ControllerBase { [HttpPost] public string GetNowTime() { string nowTime = DateTime.Now.ToString(); Console.WriteLine($"当前时间为:{nowTime}"); return $"当前时间为:{nowTime}"; } }
(2). nginx服务worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 7000; #监听端口 server_name xxx; #随意配置一个地址即可,优先走代理 location / { proxy_pass http://localhost:7061; #代理地址 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
通过post请求访问:http://localhost:7000/api/Home/GetNowTime ,返回当前时间,表示配置成功。limit_conn_zone $server_name zone=myLimit0:10m;limit_conn_zone $binary_remote_addr zone=myLimit1:10m;
(1). $server_name:表示虚拟主机(server) 同时能处理并发连接的总数。 (数量在启用时配置)limit_conn myLimit1 2; #启用限流
(1). myLimit1 :表示用上述声明的哪个配置进行限制,myLimit1与上述声明的名称相对应。worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 限流配置声明 limit_conn_zone $server_name zone=myLimit0:10m; server { listen 7000; #监听端口 server_name xxx; #随意配置一个地址即可,优先走代理 location / { limit_conn myLimit0 5; #启用限流 proxy_pass http://localhost:7061; #代理地址 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
测试结果:worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 限流配置声明 limit_conn_zone $binary_remote_addr zone=myLimit1:10m; server { listen 7000; #监听端口 server_name xxx; #随意配置一个地址即可,优先走代理 location / { limit_conn myLimit1 8; #启用限流 proxy_pass http://localhost:7061; #代理地址 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
测试结果:忽略limit_req_zone $binary_remote_addr zone=myLimit2:10m rate=5r/s;
(1). $binary_remote_addr : 表示限制同一客户端ip地址,即限制速率是以ip为分类的,限制每个ip的速度。limit_req zone=myLimit2 burst=5 nodelay;
(1). zone=myLimit2:表示用上述声明的哪个配置进行限制,myLimit2与上述声明的名称相对应。worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 限流配置声明 limit_req_zone $binary_remote_addr zone=myLimit2:10m rate=2r/s; server { listen 7000; #监听端口 server_name xxx; #随意配置一个地址即可,优先走代理 location / { limit_req zone=myLimit2; #启用限流 proxy_pass http://localhost:7061; #代理地址 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
测试结果:worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 限流配置声明 limit_req_zone $binary_remote_addr zone=myLimit2:10m rate=2r/s; server { listen 7000; #监听端口 server_name xxx; #随意配置一个地址即可,优先走代理 location / { limit_req zone=myLimit2 burst=5; #启用限流 proxy_pass http://localhost:7061; #代理地址 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
测试结果:worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 限流配置声明 limit_req_zone $binary_remote_addr zone=myLimit2:10m rate=1r/s; server { listen 7000; #监听端口 server_name xxx; #随意配置一个地址即可,优先走代理 location / { limit_req zone=myLimit2 burst=5 nodelay; #启用限流 proxy_pass http://localhost:7061; #代理地址 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
测试结果:worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #http主机 server { listen 7000; #监听端口 server_name test1; #随意配置一个地址即可,优先走代理 location / { proxy_pass http://localhost:7061; #代理地址 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #https主机 server { listen 8000 ssl; server_name test2; #证书目录 ssl_certificate D:/cert/server-cert.pem; ssl_certificate_key D:/cert/server-key.pem; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; location / { proxy_pass http://localhost:7061; } }}
(2). 将http跳转到httpsworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #http主机 server { listen 7000; #监听端口 server_name test1; #随意配置一个地址即可,优先走代理 location / { proxy_pass http://localhost:7061; #代理地址 #跳转到https (test2是https主机的server_name) return 301 https://test2$request_uri; #或者 #return 301 https://$host:8000$request_uri; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #https主机 server { listen 8000 ssl; server_name test2; #证书目录 ssl_certificate D:/cert/server-cert.pem; ssl_certificate_key D:/cert/server-key.pem; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; location / { proxy_pass http://localhost:7061; } }}
原文链接:第二节:Nginx负载均衡配置、限流配置、Https配置详解 - Yaopengfei - 博客园关键词:配置,负载,均衡