负载策略
- 权重轮询:wrr 【weight round-robin】
- ip_hash:
nginx会根据客户端ip的hash结果选择一个真实服务器,而且每次都会固定访问这台服务器,可以解决session保存问题。 - least_conn:当前活跃连接数越小,权重越大,优先选择。
- 第三方负载策略,另查找资料
- …
配置块
- weight
- max_fails
- fail_timeout
- down
- backup
- …
nginx.conf
重点关注 upstream 块里边的内容 upstream server_group { }
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
#user nginx;
worker_processes auto;
error_log /usr/local/soft/nginx/logs/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;
}
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 /usr/local/soft/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /usr/local/soft/nginx/conf/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 /usr/local/soft/nginx/conf/conf.d/*.conf;
# 定义一个服务器组
upstream server_group {
# 默认的轮询算法
wrr;
server 49.234.55.50:8080 weight=2 max_fails=3 fail_timeout=15;
server 49.234.55.50:8081 down;
server 49.234.55.50:8082 backup;
}
server {
listen 80;
server_name 159.75.79.151;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /usr/local/soft/nginx/conf/default.d/*.conf;
location /load_balancing_test {
proxy_http_version 1.1;
proxy_pass http://server_group/;
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;
client_max_body_size 1000M;
}
}
}