【愚公系列】2022年03月 .NET架构班 029-分布式中间件 Nginx多场景使用问题分析
【摘要】 一、微服务场景下Nginx的使用Nginx默认使用轮询算法 1.最小活跃数算法问题:当客户端给Nginx发送查询商品的请求时,Nginx把请求转发给5001 和 5002 ,如果5002处理请求比较慢,会导致请求堆积在5002。如何解决请求堆积问题?解决方案:最小活跃数算法#user nobody;worker_processes 1;#error_log logs/error.lo...
一、微服务场景下Nginx的使用
Nginx默认使用轮询算法
1.最小活跃数算法
问题:当客户端给Nginx发送查询商品的请求时,Nginx把请求转发给5001 和 5002 ,如果5002处理请求比较慢,会导致请求堆积在5002。如何解决请求堆积问题?
解决方案:最小活跃数算法
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://YDT.EBusiness;
}
#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 html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
least_conn;
server localhost:5001;
server localhost:5002;
}
}
2.Hash一致性算法
问题:当客户端给Nginx发送查询商品的请求时,Nginx把请求转发给5001 和 5002 ,如果5001和5002分别都有缓存,会导致缓存命中下降,请求会回源到数据库中去查询数据,导致性能下降。如何提升性能?
解决方案:Hash一致性算法
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://YDT.EBusiness;
}
#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 html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
ip_hash;
server localhost:5001;
server localhost:5002;
}
}
3.失败重试
问题:当客户端给Nginx发送查询商品的请求时,Nginx把请求转发给5001 和 5002 ,如果转发到5001的时候,5001零时宕机了,会导致请求失败。如何保证请求成功
解决方案:失败重试
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://YDT.EBusiness;
}
#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 html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
ip_hash;
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
}
}
4.故障转移
问题:当客户端给Nginx发送查询商品的请求时,Nginx把请求转发给5001 和 5002 ,如果转发到5001的时候,5001永久宕机了,会导致请求失败。如何保证请求成功
解决方案:故障转移
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /data0/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#轮询服务器,weight为服务器权重,与访问频率成正比,max_fails最大超时次数,fail_timeout服务器代理监听超时时间
upstream backend_server {
server 192.168.203.43:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.203.44:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.203.45:80 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name www.yourdomain.com 192.168.203.42;
index index.html index.htm;
root /data0/htdocs/www;
location /
{
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
}
5.主机备份
问题:当客户端给Nginx发送查询商品的请求时,Nginx把请求转发给5001 和 5002 ,如果转发到5001和5002,两个实例同时宕机了,会导致系统不可用,如何保证系统高度可用?
解决方案:主机备份
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://YDT.EBusiness;
}
#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 html;
}
}
#动态负载均衡配置
upstream YDT.EBusiness{
ip_hash;
server localhost:5001 max_fails=2 fail_timeout=10s;
server localhost:5002 max_fails=2 fail_timeout=10s;
server localhost:5003 backup;
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)