Posts Tagged ‘ php (fastcgi)

nginx配置 – 含php (fastcgi), perl, proxy, rrd, nagios

Nginx配置文件nginx.conf

 
worker_processes 5;
 
error_log logs/error.log;
error_log logs/error.log info;
 
events {
  use kqueue;
  worker_connections 2048;
}
 
http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 64;
 
  log_format main ‘$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 main;
 
  sendfile on;
 
  keepalive_timeout 65;
 
  tcp_nopush on;
 
  upstream proxy {
    server 192.168.0.2:80 weight=2;
    server 192.168.0.3:80;
  }
 
  server {
    listen 80;
    server_name my.example.com 192.168.0.1;
 
    access_log logs/my.example.com.access.log main;
 
    location /status {
      stub_status on;
      access_log off;
      allow 192.168.0.1;
      deny all;
    }
 
    location / {
      root /usr/local/www/status;
      index index.php;
      allow 192.168.100.1;
      deny all;
    }
 
    location ~ \.php$ {
      fastcgi_pass unix:/tmp/php-fastcgi.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /usr/local/www/status$fastcgi_script_name;
      include fastcgi_params;
    }
 
    location /nagios {
      root /usr/local/www;
      allow 192.168.100.1;
      deny all;
    }
 
    location ~ \.cgi$ {
      root /usr/local/www/nagios/cgi-bin;
      rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
      fastcgi_index index.cgi;
      allow 192.168.100.1;
      deny all;
      fastcgi_pass unix:/tmp/perl_cgi-dispatch.sock;
      fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;
      fastcgi_param SCRIPT_FILENAME /usr/local/www/nagios/cgi-bin$fastcgi_script_name;
      include fastcgi_params;
    }
  }
 
  server {
    listen 80;
    server_name proxy.example.com;
 
    access_log logs/proxy.example.com.access.log main;
 
    location / {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://proxy;
    }
  }
}

nginx-rrd.conf

 
#####################################################
#
# dir where rrd databases are stored
RRD_DIR="/var/spool/nginx-rrd";
# dir where png images are presented
WWW_DIR="/usr/local/www/status";
# process nice level
NICE_LEVEL="-19";
# bin dir
BIN_DIR="/usr/sbin";
# servers to test
# server_utl;server_name
SERVERS_URL="http://my.example.com/status;my.example.com http://192.168.0.2/status;2"

fastcgi-php (创建 php fastcgi socket)

. /etc/rc.subr

name="fcgiphp"
rcvar=`set_rcvar`
 
load_rc_config $name
 
: ${fcgiphp_enable="NO"}
: ${fcgiphp_bin_path="/usr/local/bin/php-cgi"}
: ${fcgiphp_user="www"}
: ${fcgiphp_group="www"}
: ${fcgiphp_children="10"}
: ${fcgiphp_port="8002"}
: ${fcgiphp_socket="/tmp/php-fastcgi.sock"}
: ${fcgiphp_env="SHELL PATH USER"}
: ${fcgiphp_max_requests="500"}
: ${fcgiphp_addr="localhost"}
 
pidfile=/var/run/fastcgi/fcgiphp.pid
procname="${fcgiphp_bin_path}"
command_args="/usr/local/bin/spawn-fcgi -f ${fcgiphp_bin_path} -u ${fcgiphp_user} -g ${fcgiphp_group} -C ${fcgiphp_children} -P ${pidfile}"
start_precmd=start_precmd
stop_postcmd=stop_postcmd
 
start_precmd()
{
        PHP_FCGI_MAX_REQUESTS="${fcgiphp_max_requests}"
        FCGI_WEB_SERVER_ADDRS=$fcgiphp_addr
        export PHP_FCGI_MAX_REQUESTS
        export FCGI_WEB_SERVER_ADDRS
        allowed_env="${fcgiphp_env} PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"
# copy the allowed environment variables
        E=""
        for i in $allowed_env; do
                eval "x=\$$i"
                E="$E $i=$x"
        done
        command="env - $E"
 
        if [ -n "${fcgiphp_socket}" ]; then
                command_args=”${command_args} -s ${fcgiphp_socket}”
        elif [ -n "${fcgiphp_port}" ]; then
                command_args=”${command_args} -p ${fcgiphp_port}else
                echo “socket or port must be specified!”
                exit
        fi
}
 
stop_postcmd()
{
        rm -f ${pidfile}
#       eval “ipcs | awk ‘{ if (\$5 == \”${fcgiphp_user}\”) print \”ipcrm -s \”\$2}’ | /bin/sh”
}
 
run_rc_command “$1

perl-fcgi.pl (创建 perl socket) – 你需要FCGI的perl模块支持

#!/usr/bin/perl
 
use FCGI;
#perl -MCPAN -e 'install FCGI'
use Socket;
 
#this keeps the program alive or something after exec'ing perl scripts
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit}; if ($@) { exit unless $@ =~ /^fakeexit/; } ;
 
&main;
 
sub main {
        #$socket = FCGI::OpenSocket( ":3461", 10 ); #use IP sockets
        $socket = FCGI::OpenSocket( "/tmp/perl_cgi-dispatch.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folde
r!!
        $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
        if ($request) { request_loop()};
            FCGI::CloseSocket( $socket );
}
 
sub request_loop {
        while( $request->Accept() >= 0 ) {
 
           #processing any STDIN input from WebServer (for CGI-POST actions)
           $stdin_passthrough ='';
           $req_len = 0 + $req_params{'CONTENT_LENGTH'};
           if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){
                        while ($req_len) {
                            $stdin_passthrough .= getc(STDIN);
                            $req_len--;
                        }
            }
 
            #running the cgi app
            if ( (-x $req_params{SCRIPT_FILENAME}) &&  #can I execute this?
                 (-s $req_params{SCRIPT_FILENAME}) &&  #Is this file empty?
                 (-r $req_params{SCRIPT_FILENAME})     #can I read this file?
            ){
                foreach $key ( keys %req_params){
                   $ENV{$key} = $req_params{$key};
                }
                #http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
                open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app ret
urned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
                if ($cgi_app) {print <$cgi_app>; close $cgi_app;}
            }
            else {
                print("Content-type: text/plain\r\n\r\n");
                print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
            }
 
        }
}