七层负载均衡示例配置: worker_processes 2; events { worker_connections 1024; } # 7层http负载 http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; # app upstream app.com { ip_hash; server 192.168.152.100:8080; server 192.168.152.101:8080; } server { listen 80; server_name app; charset utf-8; location / { proxy_pass http://plugin.com; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # web upstream web.com { ip_hash; server 192.168.152.100:8090; server 192.168.152.101:8090; } server { listen 81; server_name web; charset utf-8; location / { proxy_pass http://web.com; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 四层负载均衡配置示例: 说明: 四层需要使用stream模块,因此编译安装需要开启此配置 stream 模块和 http 模块是一同等级;做四层代理时需要添加上这个模块 # 4 层 tcp 负载 stream { upstream myweb { hash $remote_addr consistent; server 192.168.152.100:8080; server 192.168.152.101:8080; } server { listen 82; proxy_connect_timeout 10s; proxy_timeout 30s; proxy_pass myweb; } } # proxy_timeout 30s; #默认值为10分钟,nginx接收后端服务器的响应超时时间 四层代理配置示例: ser nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } # 配置 4 层代理 stream { upstream mytest1 { server 192.168.0.100:80; server 192.168.0.100:81; } server { listen 192.168.0.108:80; proxy_connect_timeout 10s; proxy_timeout 30s; proxy_pass mytest1; } upstream mytest2 { server 192.168.0.100:3000; server 192.168.0.100:3001; } server { listen 8080; proxy_connect_timeout 10s; proxy_timeout 30s; proxy_pass mytest2; } } http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 0; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 192.168.0.100:80; server_name www.test1.com; root /usr/share/nginx/mytest1; access_log /var/log/www.test1.com.log main; error_log /var/log/www.test1.com.error.log; set_real_ip_from 192.168.0.108; location / { } } server { listen 192.168.0.100:81; server_name www.test1.com; root /usr/share/nginx/mytest2; access_log /var/log/www.test1.com.log main; error_log /var/log/www.test1.com.error.log; set_real_ip_from 192.168.0.108; location / { } } server { listen 3000; server_name www.test2.com; root /usr/share/nginx/mytest3; access_log /var/log/www.test2.com.log main; error_log /var/log/www.test2.com.error.log; set_real_ip_from 192.168.0.108; location / { } } server { listen 192.168.0.100:3001; server_name www.test2.com; root /usr/share/nginx/mytest4; access_log /var/log/www.test2.com.log main; error_log /var/log/www.test2.com.error.log; set_real_ip_from 192.168.0.108; location / { } } } 负载均衡算法介绍: upstream 支持4种负载均衡调度算法: A、轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器; 加权论询 B、ip_hash:每个请求按访问IP的hash结果分配,同一个IP客户端固定访问一个后端服务器。可以保证来自同一ip的请求被打到固定的机器上,可以解决session问题。 C、url_hash 按访问url的hash结果来分配请求,使每个url 定向到同一个后端服务器。后台服务器为缓存的时候效率。 D、fair:这是比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持 fair的,如果需要使用这种调度算法,必须下载Nginx的 upstream_fair模块。 示例: 1.轮询 nginx默认就是轮询其权重都默认为1,服务器处理请求的顺序:ABABABABAB.... upstream myweb { server 192.168.152.100:8080; server 192.168.152.101:8080; } 2.加权轮询 跟据配置的权重的大小而分发给不同服务器不同数量的请求。如果不设置,则默认为1。下面服务器的请求顺序为:ABBABBABBABBABB.... upstream myweb { server 192.168.152.100:8080 weight=1; server 192.168.152.101:8080 weight=2; } # 注意 值越大分配的请求越多 3.ip hash nginx 会让相同的客户端ip请求相同的服务器 upstream myweb { server 192.168.152.100:8080; server 192.168.152.101:8080; ip_hash; } 4.fair fair比 weight、ip_hash更加智能的负载均衡算法,fair算法可以根据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间 来分配请求,响应时间短的优先分配。Nginx本身不支持fair,如果需要这种调度算法,则必须安装upstream_fair模块。 5.url_hash 按访问的URL的哈希结果来分配请求,使每个URL定向到一台后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身不支持url_hash,如果需要这种调度算法,则必须安装Nginx的hash软件包。 负载均衡状态参数 down,表示当前的server暂时不参与负载均衡。 backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。 max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回 proxy_next_upstream 模块定义的错误。 fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails 可以和 fail_timeout一起使用。 upstream myweb { server 192.168.152.100:8080 weight=2 max_fails=2 fail_timeout=2; server 192.168.152.101:8080 weight=1 max_fails=2 fail_timeout=1; }
本文由一叶发布,不代表一叶立场,转载联系作者并注明出处:https://yiyeo.com/jishu/311.html