nginx的upstream目前支持的5种方式的分配
1、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver { server 192.168.0.14; server 192.168.0.15; }2、指定权重 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver { server 192.168.0.14 weight=8; server 192.168.0.15 weight=10; }3、IP绑定 ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver { ip_hash; server 192.168.0.14:88; server 192.168.0.15:80; }4、fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver { server server1; server server2; fair; }5、url_hash(第三方) 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backserver { server squid1:3128; server squid2:3128; hash $request_uri; hash_method crc32; }在upstream模块配置完成后,要让指定的访问反向代理到服务器列表:
#其他页面反向代理到tomcat容器 location ~ .*$ { index index.jsp index.html; proxy_pass http://xxx.com; } #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 cwt.com { server 127.0.0.1:7080; server 127.0.0.1:8080; ip_hash; } server { listen 9080; server_name xxx.xxx.xxx.xxx 10.109.230.70; port_in_redirect off; if ($host !~* "xxx.xxx.xxx.xxx|10.109.230.70" ){ return 403; } #charset koi8-r; #access_log logs/host.access.log main; location / { #欢迎页面,按照从左到右的顺序查找页面 index index.jsp index.html index.htm; client_max_body_size 50m; client_body_buffer_size 256k; proxy_connect_timeout 1; proxy_send_timeout 30; proxy_read_timeout 60; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; proxy_max_temp_file_size 128m; #域名aaa.test.com的请求全部转发到tomcat_server1即tomcat1服务上 proxy_pass http://xxx.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_cookie_path /foundation/ /; } #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #{ # proxy_pass http://cwt.com; #} #location ~ .*\.(js|css)?$ #{ # proxy_pass http://cwt.com; #} #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; # } #} }