1. nginx实现负载均衡策略
2.1 轮询 - 轮流处理请求
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream rr_test{
server localhost:8080;
server localhost:8081;
}
server {
listen 8083;
server_name localhost;
location / {
proxy_pass http://rr_test;
proxy_set_header Host $host:$server_port;
}
}
2.2 权重 — you can you up
通过配置权重,指定轮询几率,权重和访问比率成正比,用于应用服务器性能不均的情况。
upstream weight_test{
server localhost:8080 weight=1;
server localhost:8081 weight=9;
}
server {
listen 8084;
server_name localhost;
location / {
proxy_pass http://weight_test;
proxy_set_header Host $host:$server_port;
}
}
2.3 ip_哈希算法
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个应用服务器,可以解决session共享的问题。
server {
listen 8085;
server_name localhost;
location / {
proxy_pass http://ip_hash;
proxy_set_header Host $host:$server_port;
}
}
upstream ip_hash{
ip_hash;
server localhost:8080;
server localhost:8081;
}
2. nginx/conf代码示例
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
# 负载均衡,轮询模式
upstream rr_test{
server localhost:8080;
server localhost:8081;
}
# 负载均衡,权重模式
upstream weight_test{
server localhost:8080 weight=1;
server localhost:8081 weight=9;
}
# 负载均衡,ip哈希模式
upstream ip_hash{
ip_hash;
server localhost:8080;
server localhost:8081;
}
# 服务端,普通测试
server {
listen 8081;
server_name localhost;
location /love {
root /mnt/windows_share/nginx;
index index.html;
}
}
# 服务端, 代理测试
server {
listen 8082;
server_name localhost;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port;
}
}
# 服务端, 轮询负载均衡测试
server {
listen 8083;
server_name localhost;
location / {
proxy_pass http://rr_test;
proxy_set_header Host $host:$server_port;
}
}
# 服务端, 权重负载均衡测试
server {
listen 8084;
server_name localhost;
location / {
proxy_pass http://weight_test;
proxy_set_header Host $host:$server_port;
}
}
# 服务端, ip哈希负载均衡测试
server {
listen 8085;
server_name localhost;
location / {
proxy_pass http://ip_hash;
proxy_set_header Host $host:$server_port;
}
}
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
参考:
https://blog.csdn.net/qq_36125138/article/details/84144932
https://www.cnblogs.com/qlqwjy/p/8536779.html