实战 | php负载均衡配置-nginx反向代理设置笔记

需求

1.单台服务器满足不了当前业务量,用多台服务器来分流

思路

1.看图片架构,本文只讲述负载均衡部分,数据库和缓存部以后更新

实战 | php负载均衡配置-nginx反向代理设置笔记


2.这3台web服务器怎么分配流量就靠前端的代理服务器来分配,load Balance

配置步骤

1.安装好nginx (自找资料,网上很多,本文用的环境是阿里云的一键安装sh-1.5.5环境)

2.找到nginx配置文件,我的目录是

#/alidata/server/nginx-1.4.4/conf/vhosts

将默认的phpwind.conf拷贝一份,命名为proxy.conf,proxy.conf内配置内容:


server {
        listen       80;
        server_name  proxy.xxx.com;
index index.html index.htm index.php;
root /alidata/www/proxy;
    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_pass http://monitor_server;   #这里是转发到目标地址,如果做代理,只需要把这里写上ip,做均衡就类似一个数组
    }
location ~ .*\.(php|php5)?$
{
#fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#伪静态规则
include /alidata/server/nginx/conf/rewrite/phpwind.conf;
access_log  /alidata/log/nginx/access/proxy.log;
}
upstream monitor_server {
    server xxx.xx.xxx.01:80 weight=2; #服务器1真实地址 ,weight是权重,值越大,代理服务器就会优先发给它

    server xxx.xx.xxx.02:80 weight=1; 

}

3.在这两台服务器上部署好自己的应用,正常可以访问

    server xxx.xx.xxx.01

    server xxx.xx.xxx.02

效果

1.流量器访问proxy.xxx.com,会优先获取回server xxx.xx.xxx.01:80的页面,因为它的权重大.

2.代理转发服务器还可以做静态资源服务器.像一些css,js,image等静态资源,有些缓存静态页面也可以放在这里,这样可以加快速度