Nginx和fastcgi分离的实现以及注意问题

                Nginx和fastcgi分离的实现以及注意问题

  前言,写此文的目的是当时在配置nginx fastcgi分离的时候(即大家所说的动静分离),遇到文件无法解析的情况,现记录如此,希望有对遇到同样情况的朋友有帮助,同时,在此感谢网站运维管理群里的“南昌‖某C”等提供的帮助。

环境

Nginx  192.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.254nginx.conf配置参数如下

user  www www;

worker_processes  1;

error_log  logs/error.log  notice;

pid        logs/nginx.pid;

events 

{

    use epoll;

    worker_connections  1024;

}

http 

{

    include       mime.types;

    default_type  application/octet-stream;

    log_format  main  '$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;

    gzip  on;

    gzip_min_length       1k;

    gzip_buffers       4 16k;

    gzip_http_version    1.1;

    gzip_comp_level        2;

    gzip_types  text/plain application/x-javascript text/css applocation/xml;  

upstream backend {

server 192.168.16.21:9000;

}

server

  {

    listen       80;

    server_name  192.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_pass  backend;

   fastcgi_index  index.php;

   fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/cacti$fastcgi_script_name;

   include        fastcgi_params;

   }

    log_format  wwwcacti  '$remote_addr - $remote_user [$time_local] "$request" '

               '$status $body_bytes_sent "$http_referer" '

               '"$http_user_agent" $http_x_forwarded_for';

    access_log  logs/access.log  wwwcacti;

    source_charset      UTF-8;

  }

}

   upstream backend 可以设置多个fastcgi服务器这里除了此配置方法,还有另外一个等效的配置方法,不过只能指定一个fastcgi服务器

   #去掉上面的红色字部分。

   location ~ \.php$

   {

   fastcgi_pass  192.168.16.219000

   fastcgi_index  index.php;

   fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/cacti$fastcgi_script_name;

   include        fastcgi_params;

   }

  查看web页面目录,目录的位置,和nginx.conflocation配置有关,此处不解释了,可以查看相关资料

查看php-fpm运行情况

查看nginx运行情况

验证php-fpm端口是否能通

访问nginx页面

   可以看到,程序已经成功运行

   注意:如果fastcgi服务器上面没有放webphp页面,在访问php页面的时候,出现以下画面

  (此文的关键之处在此)之所以出现这个情况,原因是fastcgi负责php的解析,当nginx发现访问的文件是.php, 会负责把php文件的解析交给fastcgifastcgi通过正确的解析,返回给nginx,然后提供给客户端。

   关于nginx的fastcgi运行原理,可以参考此文http://book.51cto.com/art/201202/314840.htm

   此文暂总结如此。

                                         itnihao  2012年7月15日于成都

附文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值