应用的最前端是一台nginx服务器,所有静态的内容都由nginx来处理,而将所有php的请求都分摊到下游的若干台运行php fastcgi守护进程的服务器中,这样可以以一种廉价的方案来实现对系统负载的分摊,扩展系统的负载能力。

三台php fastcgi服务器的ip地址分别为:
172.16.236.110 , 172.16.236.111, 172.16.236.112

运行php fastcgi进程时,需要让php-cgi监听到服务器的局域网地址(分别如上所示),而不是之前一般都是监听的本地地址(127.0.0.1)。以172.16.236.110这台服务器为例:

/usr/local/php5/bin/php-cgi -b 172.16.236.110:9000

或许你用spawn-fcgi来启动php-fcgi,那么就是这样(供参考,其实也就是修改监听的地址和端口即可):

/usr/local/lighttpd/bin/spawn-fcgi -f /usr/local/php5/bin/php-cgi -a 172.16.236.110 -p 9000

又或许你是用php-fpm来管理php-fcgi,那么你需要修改php-fpm的配置:

vi /usr/local/php5/etc/php-fpm.conf

找到这个配置项(其中的地址可能需要根据你自己环境来调整)

<value name=”listen_address”>127.0.0.1:9000</value>

修改为:

<value name=”listen_address”>172.16.236.110:9000</value>

修改完毕后,重启你的php-fpm进程。

然后按照上面的步骤,依次修改其他php fastcgi服务器。

php方面的工作暂时就是这些,下面修改nginx。

vi /usr/local/nginx/conf/nginx.conf

在配置文件的http段内增加类似如下的配置:

upstream myfastcgi {
server 172.16.236.110 weight=1;
server 172.16.236.111 weight=1;
server 172.16.236.112 weight=1;
}

我这里三台php fastcgi服务器的权重是相同的,所以其中的weight值都是1,如果你的php fastcgi服务器需要分主次,那么可以通过调整其weight值来达到目的。比如以第一台服务器为主,其他两台为辅,则就是这样:

upstream myfastcgi {
server 172.16.236.110 weight=1;
server 172.16.236.111 weight=2;
server 172.16.236.112 weight=2;
}

然后找到原来nginx关于php fastcgi配置的部分,比如:

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

将其中的fastcgi_pass那一段改为:

fastcgi_pass myfastcgi;

 
upstream web.home.org {
server 192.168.1.10:80 weight=1;
server 192.168.1.11:80 weight=2;
server 192.168.1.12:80;
server 192.168.1.13:80;
}

upstream squid.home.org {
server 192.168.1.117:80;
server 192.168.1.118:80;
server 192.168.1.119:80;
}

server {
listen 80;
server_name www.home.org;
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;

location / {
root html;
index index.html index.htm;
if ($request_uri ~* “.*\.(js|css|gif|jpg|jpeg|png|bmp|swf|html)$”)
{
proxy_pass http://squid.home.org;
}

if ($request_uri ~* “^/view/(.*)$”)
{
proxy_pass http://squid.home.org;
}

proxy_pass http://web.home.org;
}
location /Status {
stub_status on;
access_log on;
}