这篇文章想让自己熟悉下nginx的upstream配置
先看一下默认的nginx配置
#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 / {
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;
# }
#}
}
通常在http指令下进行配置
upstream server主要配置
upstream tomcat_passport-web.b2c {
server 127.0.0.1:9002 weight=10 max_fails=2 fail_timeout=30s;
}
ip地址端口号
权重 根据服务器处理能力分配
proxy_pass 配置请求
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
proxy_next_upstream
当访问时会把请求反向代理到upstream server
算法 轮询 哈希
ip_hash加入到upstream
hash &url
hash $consistent_key consistent
通过lua设置一致性哈希
健康检查 需要安装check模块补丁
check interval=30000 rise=1 fall=3 timout=2000 type=tcp
HTTP额外两个
check_http_send ""
check_http_expect_alive ""
其他配置
动态更新 动态解析
域名备份
不可用配置down
长链接keepalive 100 proxy_set header connection ""
反向代理配置
proxycache
location配置
是否压缩
动态upstream实现
Consul服务注册发现故障检测KV存储多数据中心Raft算法
etcd3
Nginx四层负载均衡 一站式解决方案
静态负载均衡
动态负载均衡
还有Haproxy四层负载均衡
因为是彩笔,所以是给自己看看,后面慢慢更新