Maison > Article > développement back-end > Nginx系列(十三 nginx负载均衡)
一.负载均衡配置
<code><span># 负载均衡</span><span>upstream</span> webserver { <span>server</span><span>192.168.27.134:8080</span>; <span>server</span><span>192.168.27.135:80</span>; <span>server</span><span>192.168.27.136:80</span>; } <span># 反向代理</span><span>server</span> { <span>listen</span><span>80</span>; <span>server_name</span> www.test01.com; <span>location</span> / { <span>proxy_pass</span><span>http://webserver</span>; <span>proxy_redirect</span><span>off</span>; <span>proxy_set_header</span> Host <span>$host</span>; <span>proxy_set_header</span> X-Real-IP <span>$remote_addr</span>; <span>proxy_set_header</span> X-Forwarded-For <span>$proxy_add_x_forwarded_for</span>; } } <span># web服务器</span><span>server</span> { <span>listen</span><span>8080</span>; <span>server_name</span> www.test01.com; <span>access_log</span> logs/host.access.log main; <span>location</span> / { <span>root</span> /web/www/thinkphp; <span>index</span> index.php index.html index.htm; <span>if</span> (!-e <span>$request_filename</span>) { <span>rewrite</span><span> ^/(.*)$</span> /index.php/<span>$1</span><span>last</span>; } } <span>location</span><span>~ [^/]\.php(/|$)</span> { <span>root</span> /web/www/thinkphp; <span>fastcgi_index</span> index.php; <span>fastcgi_pass</span><span>127.0.0.1:9000</span>; <span>include</span> fastcgi_params; <span>fastcgi_split_path_info</span><span> ^(.+?\.php)(/.*)$</span>; <span>fastcgi_param</span> SCRIPT_FILENAME <span>$document_root</span><span>$fastcgi_script_name</span>; <span>fastcgi_param</span> SCRIPT_NAME <span>$fastcgi_script_name</span>; <span>fastcgi_param</span> PATH_INFO <span>$fastcgi_path_info</span>; <span>fastcgi_param</span> PATH_TRANSLATED <span>$document_root</span><span>$fastcgi_path_info</span>; } }</code>
二. 负载均衡算法
1.轮询 (默认)
<code>upstream webserver { <span>server</span><span>192.168</span><span>.27</span><span>.134</span>:<span>8080</span>; <span>server</span><span>192.168</span><span>.27</span><span>.135</span>:<span>80</span>; <span>server</span><span>192.168</span><span>.27</span><span>.136</span>:<span>80</span>; }</code>
2.weight
<code>upstream webserver { <span>server</span><span>192.168</span><span>.27</span><span>.134</span>:<span>8080</span> weight=<span>1</span>; <span>server</span><span>192.168</span><span>.27</span><span>.135</span>:<span>80</span> weight=<span>2</span>; <span>server</span><span>192.168</span><span>.27</span><span>.136</span>:<span>80</span> weight=<span>2</span>; }</code>
3.ip_hash ( 解决session共享问题)
<code>upstream webserver { ip_hash; <span>server</span><span>192.168</span><span>.27</span><span>.134</span>:<span>8080</span>; <span>server</span><span>192.168</span><span>.27</span><span>.135</span>:<span>80</span>; <span>server</span><span>192.168</span><span>.27</span><span>.136</span>:<span>80</span>; }</code>
4.fair 按后端服务器的响应时间来分配请求,响应时间短的优先分配。(第三方)
<code>upstream webserver { <span>server</span><span>192.168</span><span>.27</span><span>.134</span>:<span>8080</span>; <span>server</span><span>192.168</span><span>.27</span><span>.135</span>:<span>80</span>; <span>server</span><span>192.168</span><span>.27</span><span>.136</span>:<span>80</span>; fair; }</code>
参考:http://wiki.nginx.org/HttpUpstreamFairModule
5.url_hash 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。(第三方)
<code><span>upstream</span> webserver { <span>server</span><span>192.168.27.134:8080</span>; <span>server</span><span>192.168.27.135:80</span>; <span>server</span><span>192.168.27.136:80</span>; <span>hash</span><span>$request_uri</span>; }</code>
参考:http://wiki.nginx.org/HttpUpstreamRequestHashModule
6.一致性hash (第三方)
参考:http://wiki.nginx.org/HttpUpstreamConsistentHash
三.相关配置
Weight:指定轮询权值,Weight值越大,分配到的访问机率截越高,主要用于后端每个服务器性能不均的情况下。
down:表示当前的server暂时不参与负载均衡,通常和ip_hash一起使用。
backup:预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
max_fails:在一定时间内(这个时间在fail_timeout参数中设置)检查这个服务器是否可用时产生的最多失败请求数,默认为1。将其设置为0可以关闭检查,这些错误在proxy_next_upstream和fastcgi_next_upstream(404错误不会使max_fails增加)中定义。
fail_timeout:在这个时间内产生了max_fails所设置大小的失败尝试连接请求后这个服务器可能不可用,同样它指定了服务器不可用的时间(在下一次尝试连接请求发起之前),默认为10秒,fail_timeout与前端响应时间没有直接关系,不过可以使用proxy_connect_timeout和proxy_read_timeout来控制。
四.参考资料
http://baidutech.blog.51cto.com/4114344/1033718
http://lobert.iteye.com/blog/1929623
http://blog.csdn.net/poechant/article/details/7256184
以上就介绍了Nginx系列(十三 nginx负载均衡),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。