Nginx和fastcgi分離的實現以及注意問題

科技小先鋒發表於2017-11-07

    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

修改以下兩個引數

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,然後提供給客戶端。


本文轉自it你好 51CTO部落格,原文連結:http://blog.51cto.com/itnihao/931490,如需轉載請自行聯絡原作者


相關文章