nginx模塊之ngx_http_fastcgi_module

nginx支持LNMP

安裝php-fpm

yum install php-fpm -y

使用/etc/php-fpm.d/www.conf配置文件默認配置便可php

打開php配置: html

vim /etc/nginx/conf.d/defaults
location ~ \.php$ {
        root    /usr/share/nginx/html;  
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
        include fastcgi_params;
    }
vim /etc/nginx/server.conf
location / {
        root   html;
        index index.php index.html index.htm;  //支持index.php頁面
}

修改fastcgi_parmsmysql

vim /etc/nginx/fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
上面內容中添加以下一行:
  fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;  

 php支持mysql

# yum install php-mysql -y

安裝mysqlnginx

# yum install mysql-server -y

測試頁sql

<?php
        $conn = mysql_connect('127.0.0.1','root','');
        if ($conn)
                echo succ;
        else
                echo failure;
        mysql_close();
        phpinfo();
?>

爲動態內容fastcgi協議的集羣設置緩存

http上下文中添加:
upstream upservers {
        server 10.0.0.102 max_fails=5 fail_timeout=1s weight=1;
        server 10.0.0.103 max_fails=5 fail_timeout=1s weight=1;
    }
    proxy_cache_path /cache/nginx/ levels=1:1 keys_zone=mycache:32m;
    fastcgi_cache_path /cache/fastcgi/ levels=1:1 keys_zone=fcgicache:32m inactive=3m max_size=1g;
server.conf配置文件location中定義:
location ~ \.php$ {
 46             fastcgi_cache fcgicache;
 47             fastcgi_cache_valid 200 10m;
 48             fastcgi_cache_valid 302 3m;
 49             fastcgi_cache_valid any 1m;
 50 
 51             root           html;
 52             fastcgi_pass   127.0.0.1:9000;
 53             fastcgi_index  index.php;
 54             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 55             include        fastcgi_params;
 56         }

其餘參數介紹:

fastcgi_limit_rate: 限定從上游服務器收內容的速率

Syntax:    fastcgi_limit_rate rate;
Default:    
fastcgi_limit_rate 0;
Context:    http, server, location
This directive appeared in version 1.7.7.

fastcgi_param: 向後端傳遞參數

Syntax:    fastcgi_param parameter value [if_not_empty];
Default:    —
Context:    http, server, location

fastcgi_store: 將內容存到磁盤上,不建議開啓,若是要開啓說明性能是降低的

Syntax:    fastcgi_store on | off | string;
Default:    
fastcgi_store off;
Context:    http, server, location
location /images/ {
    root                 /data/www;
    error_page           404 = /fetch$uri;
}

location /fetch/ {
    internal;

    fastcgi_pass         backend:9000;
    ...

    fastcgi_store        on;
    fastcgi_store_access user:rw group:rw all:r;
    fastcgi_temp_path    /data/temp;  //不建議啓用,應該加大內存空間buffer的大小

    alias                /data/www/;
}

關閉緩存

fastcgi_cache off;
相關文章
相關標籤/搜索