转 : http://www.jbxue.com/article/7923.html
介绍一个nginx、mysql、php-fpm环境下配置负载均衡的例子,有需要的朋友,可以参考下。
系统环境如下:
前端Nginx:192.168.93.137
后端web1:192.168.0.11
后端web2:192.168.0.12
1、前端nginx配置:
http {
……
client_max_body_size 300m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
upstream www.jbxue.com {
#server 192.168.93.137:80;
server 192.168.0.11:80;
server 192.168.0.12:80;
}
upstream www.jbxue007.com {
#server 192.168.93.137:80;
server 192.168.0.11:80;
server 192.168.0.12:80;
}
server
{
listen 80;
server_name www.jbxue.com;
location / {
proxy_pass http://www.jbxue.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format jbxue '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /home/logs/www.jbxue.log jbxue;
}
server
{
listen 80;
server_name www.jbxue007.com;
location / {
proxy_pass http://www.jbxue007.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format jbxue007 '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /home/logs/www.jbxue.log jbxue007;
}
}
通过upstream 名字 {}定义后端web的负载机器,然后在虚拟主机中通过 proxy_pass http://名字; 来使用upstream,再自定义一下日志格式,以获取用户的IP。
2、后端web配置:
server
{
listen 80;
server_name www.jbxue.com;
index index.html index.php;
root /home/www/www.jbxue.com;
access_log /home/logs/access_www.jbxue.com.log;
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
error_page 500 502 503 504 404 403 http://www.jbxue.com;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 6h;
}
location ~ .*\.(log|txt)$
{
deny all;
}
location ~ .*\.(php)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
}
server
{
listen 80;
server_name www.jbxue007.com;
index index.html index.php;
root /home/www/www.jbxue007.com;
access_log /home/logs/access_www.jbxue007.com.log;
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
error_page 500 502 503 504 404 403 http://www.jbxue007.com;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 6h;
}
location ~ .*\.(log|txt)$
{
deny all;
}
location ~ .*\.(php)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
}
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
server 192.168.0.11 weight=10;
server 192.168.0.12 weight=10;
}
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
ip_hash;
server 192.168.0.11:80;
server 192.168.0.12:80;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
server 192.168.0.11:80;
server 192.168.0.12:80;
hash $request_uri;
hash_method crc32;
}
用得比较多得应该是第3、5这两种。
后端的web文件同步可以选择nfs、rsync等。