nginx的fastcgi配置

首先參考了一份配置註釋(來自「小剛的博客」):javascript

#運行用戶
user www-data;    
#啓動進程,一般設置成和cpu的數量相等
worker_processes  1;

#全局錯誤日誌及PID文件
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

#工做模式及鏈接數上限
events {
    use   epoll;             #epoll是多路複用IO(I/O Multiplexing)中的一種方式,可是僅用於linux2.6以上內核,能夠大大提升nginx的性能
    worker_connections  1024;#單個後臺worker process進程的最大併發連接數
    # multi_accept on; 
}

#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
     #設定mime類型,類型由mime.type文件定義
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    #設定日誌格式
    access_log    /var/log/nginx/access.log;

    #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
    #必須設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,下降系統的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #鏈接超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
    
    #開啓gzip壓縮
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #設定請求緩衝
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    #設定負載均衡的服務器列表
     upstream mysvr {
    #weigth參數表示權值,權值越高被分配到的概率越大
    #本機上的Squid開啓3128端口
    server 192.168.8.1:3128 weight=5;
    server 192.168.8.2:80  weight=1;
    server 192.168.8.3:80  weight=6;
    }


   server {
    #偵聽80端口
        listen       80;
        #定義使用www.xx.com訪問
        server_name  www.xx.com;

        #設定本虛擬主機的訪問日誌
        access_log  logs/www.xx.com.access.log  main;

    #默認請求
    location / {
          root   /root;      #定義服務器的默認網站根目錄位置
          index index.php index.html index.htm;   #定義首頁索引文件的名稱

          fastcgi_pass  www.xx.com;
         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
          include /etc/nginx/fastcgi_params;
        }

    # 定義錯誤提示頁面
    error_page   500 502 503 504 /50x.html;  
        location = /50x.html {
        root   /root;
    }

    #靜態文件,nginx本身處理
    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        root /var/www/virtual/htdocs;
        #過時30天,靜態文件不怎麼更新,過時能夠設大一點,若是頻繁更新,則能夠設置得小一點。
        expires 30d;
    }
  #PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置. location ~ \.php$ { #以php結尾的都由下面來設定。 root /root;          #根文件 fastcgi_pass 127.0.0.1:9000;  #轉向的端口,好比php-fpm監聽在本地9000端口 fastcgi_index index.php;    #默認index fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;    #請求的資源路徑 include fastcgi_params; #引入fastcgi_params文件的各類參數 }
    #設定查看Nginx狀態的地址
    location /NginxStatus {
        stub_status            on;
        access_log              on;
        auth_basic              "NginxStatus";
        auth_basic_user_file  conf/htpasswd;
    }
    #禁止訪問 .htxxx 文件
    location ~ /\.ht {
        deny all;
    }
     
     }
}

咱們關注與php的交互fastcgi這一塊,在上面的配置裏標紅加粗的部分,並在尾部分別進行了註解。fastcgi_params以下:php

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#腳本文件請求的路徑  
fastcgi_param QUERY_STRING $query_string; #請求的參數;如?app=123  
fastcgi_param  REQUEST_METHOD     $request_method; #請求的動做(GET,POST)  
fastcgi_param  CONTENT_TYPE       $content_type; #請求頭中的Content-Type字段  
fastcgi_param  CONTENT_LENGTH     $content_length; #請求頭中的Content-length字段。  
  
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name; #腳本名稱(如:index.php)   
fastcgi_param  REQUEST_URI        $request_uri; #請求的地址不帶參數  
fastcgi_param  DOCUMENT_URI       $document_uri; #與$uri相同。   
fastcgi_param  DOCUMENT_ROOT      $document_root; #網站的根目錄。在server{}配置中root指令中指定的值   
fastcgi_param  SERVER_PROTOCOL    $server_protocol; #請求使用的協議,一般是HTTP/1.0或HTTP/1.1。    
  
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;#cgi 版本  
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;#nginx 版本號,可修改、隱藏  
  
fastcgi_param  REMOTE_ADDR        $remote_addr; #客戶端IP  
fastcgi_param  REMOTE_PORT        $remote_port; #客戶端端口  
fastcgi_param  SERVER_ADDR        $server_addr; #服務器IP地址  
fastcgi_param  SERVER_PORT        $server_port; #服務器端口  
fastcgi_param  SERVER_NAME        $server_name; #服務器名,域名在server{}配置中指定的server_name  
  
#fastcgi_param  PATH_INFO           $path_info;#可自定義變量  
  
# PHP only, required if PHP was built with --enable-force-cgi-redirect  
#fastcgi_param  REDIRECT_STATUS    200;  
  
在php可打印出上面的服務環境變量  
如:echo $_SERVER['REMOTE_ADDR'] 

上面文件中間一列的大寫字母實際上是經過fastcgi向php傳遞的參數,然後面的以$打頭的變量則是nginx提供的全局變量,包括了其從http包中得到的各類信息。參考。css

相關文章
相關標籤/搜索