原创文章转载请注明来源:https://blog.csdn.net/weixin_41756573/article/details/88812773
一:Nginx的负载均衡策略
1.轮询策略
2.权重策略
3.最少连接策略
4.IP绑定策略(低并发,重业务 )
5.按相应时间(第三方)
6.按url分配策略(第三方)
二:Nginx的nginx.conf
#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;
# 自己的配置(关于upstream)-服务器池
upstream babytun {
server 192.168.145.128:8001;
server 192.168.145.128:8001;
server 192.168.145.128:8001;
server 192.168.145.128:8001;
}
server {
# 配置监听的端口
listen:80;
location / {
# 配置处理请求的服务器池
proxy_pass http:服务器池名称;
# 默认配置
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# 原来的server配置
# server {
# listen 80;
# server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
#root html;
#index index.html index.htm;
# }
#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;
}
# 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
三:对应负载均衡策略的配置
Nginx的负载均衡策略
1.default 轮询策略 (Nginx默认的负载均衡策略)
-使用方法:在nginx.conf中无需进行任何配置
2.least_conn 最少连接策略
-使用方法:在http的服务器池的第一行配置 least_conn;
http {
upstream 服务器池名称{
# 这样配置则表示该服务器池使用最少连接策略
least_conn;
#配置服务器
...
}
}
3.weighted 权重策略
-使用方法:在http服务器池中配置服务器时,在后面配置上 weighted=数字 即可
http{
#配置服务器
server 服务器真实IP:端口号 weighted=数字;
}
-权重相关的参数:
3-1.weighted=数字 按百分比分配请求,数字越大,权重越大
3-2.down 表示负载过重或者不参与负载
3-3.backup 其它服务器时或down时才会请求backup服务器
4.ip_hash IP绑定策略
-使用方法:在http的服务器池的第一行配置 ip_hash;
http{
# 这样配置则表示该服务器池使用IP绑定策略
ip_hash;
#配置服务器
...
}
-使用场景:适用于低并发,重业务(执行像导入导出等复杂的业务等)
5.fair 按相应时间(第三方)
6.url_hash 按url分配策略(第三方)