Nginx和fastcgi分离的实现以及注意问题
环境Nginx192.168.16.254:80
Fastcgi 192.168.16.21:900
Web页面路径放的位置
静态页面放 192.168.16.254:/usr/local/nginx/html/cacti
动态页面放 192.168.16.21:/usr/local/nginx/html/cacti
提示:当然,为了简单,可以两台服务器的文件放一样的
程序版本
Nginx 1.01
Php 5.3.12
系统版本 centos6.2
在192.168.16.21上面改fastcgi配置文件php-fpm.conf
修改以下两个参数
listen = 192.168.16.21:9000
listen.allowed_clients = 192.168.16.254
注意,php-fpm默认是允许any来访问的,除用allowed外,可以通过防火墙来实现
192.168.16.254的nginx.conf配置参数如下
userwww www;
worker_processes1;
error_loglogs/error.lognotice;
pid logs/nginx.pid;
events
{
use epoll;
worker_connections1024;
}
http
{
include mime.types;
default_typeapplication/octet-stream;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
gzipon;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_typestext/plain application/x-javascript text/css applocation/xml;
upstream backend {
server 192.168.16.21:9000;
}
server
{
listen 80;
server_name192.168.16.254;
index index.html index.htm index.php;
#root/usr/local/nginx/html/cacti;
location /
{
index index.html index.htm index.php;
root/usr/local/nginx/html/cacti;
}
location ~ \.php$
{
fastcgi_passbackend;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/usr/local/nginx/html/cacti$fastcgi_script_name;
include fastcgi_params;
}
log_formatwwwcacti'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_loglogs/access.logwwwcacti;
source_charset UTF-8;
}
}
upstream backend 可以设置多个fastcgi服务器这里除了此配置方法,还有另外一个等效的配置方法,不过只能指定一个fastcgi服务器
#去掉上面的红色字部分。
location ~ \.php$
{
fastcgi_pass192.168.16.21:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/usr/local/nginx/html/cacti$fastcgi_script_name;
include fastcgi_params;
}
查看web页面目录,目录的位置,和nginx.conf的location配置有关,此处不解释了,可以查看相关资料
查看php-fpm运行情况
查看nginx运行情况
验证php-fpm端口是否能通
访问nginx页面
可以看到,程序已经成功运行
注意:如果fastcgi服务器上面没有放web的php页面,在访问php页面的时候,出现以下画面
(此文的关键之处在此)之所以出现这个情况,原因是fastcgi负责php的解析,当nginx发现访问的文件是.php, 会负责把php文件的解析交给fastcgi,fastcgi通过正确的解析,返回给nginx,然后提供给客户端。
关于nginx的fastcgi运行原理,可以参考此文http://book.51cto.com/art/201202/314840.htm
此文暂总结如此。
itnihao2012年7月15日于成都
附文档
页:
[1]