Results tagged “nginx” from 清茶 Blog

cnc-a# cat nginx.conf

user  nobody nogroup;
worker_processes  5;

#error_log  logs/error.log  notice;
error_log  off;
#pid        logs/nginx.pid;

events {
    worker_connections  8192;
    use kqueue;  # for freebsd
}


http {
    include       mime.types;
    include       fastcgi_params;

    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  120;
    access_log  /dev/null;
   
    gzip  on;
    gzip_comp_level     4;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_http_version 1.0;
    gzip_proxied        any;
    gzip_types       text/plain text/xml text/javascript application/x-javascript text/css text/html application/xml;

## Default
include vhost-config/default.conf;

}

 

cnc-a# cat 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  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;

php-fastcgi.sh

|

Source : http://topfunky.net/svn/shovel/nginx/php-fastcgi.sh

 

#!/bin/bash

# Description: PHP-FastCgi start script from http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/
#
# Author: Alexey Kovyrin http://blog.kovyrin.net
# Comments by: Geoffrey Grosenbach http://topfunky.com
#
# This script is started once and receives PHP requests from Nginx for 
# all apps. The Nginx config passes the full path to the script being requested, so
# only one fastcgi runner is needed for all apps, virtual hosts, etc.
#
# See also the init.d script for starting this on boot.
#
# To install PHP, I had to also compile the following:
#
#   * http://www.gnu.org/software/m4/
#   * http://flex.sourceforge.net/
#   * http://php.net/ with './configure --prefix=/usr/local --enable-fastcgi'

## ABSOLUTE path to the PHP binary
PHPFCGI="/usr/local/bin/php"

## tcp-port to bind on
FCGIPORT="9000"

## IP to bind on
FCGIADDR="127.0.0.1"

## number of PHP children to spawn
PHP_FCGI_CHILDREN=5

## number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=1000

# allowed environment variables sperated by spaces
ALLOWED_ENV="PATH USER"

## if this script is run as root switch to the following user
USERID=deploy

################## no config below this line

if test x$PHP_FCGI_CHILDREN = x; then
  PHP_FCGI_CHILDREN=5
fi

ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN"
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS"
ALLOWED_ENV="$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS"

if test x$UID = x0; then
  EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID"
else
  EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT"
fi

echo $EX

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E="$E $i=${!i}"
done

# clean environment and set up a new one
nohup env - $E sh -c "$EX" &> /dev/null &

nginx.conf

|

Source : http://topfunky.net/svn/shovel/nginx/conf/nginx-php.conf

##
# Basic config modified only slightly from http://brainspl.at/articles/2007/01/03/new-nginx-conf-with-optimizations
#
# Turns SSI on and uses locations as defined in install-nginx.sh script.
#
# See also http://topfunky.net/svn/shovel/nginx
#
# USE AT YOUR OWN RISK!

# user and group to run as
user  deploy deploy;

# number of nginx workers
worker_processes  6;

# pid of nginx master process
pid /var/run/nginx.pid;

# Number of worker connections. 1024 is a good default
events {
  worker_connections 1024;
}

# start the http module where we config http access.
http {
  # pull in mime-types. You can break out your config 
  # into as many include's as you want to make it cleaner
  include /usr/local/nginx/conf/mime.types;

  # set a default type for the rare situation that
  # nothing matches from the mimie-type include
  default_type  application/octet-stream;

  # configure log format
  log_format main '$remote_addr - $remote_user [$time_local] '
                  '"$request" $status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  # main access log
  access_log  /var/log/nginx_access.log  main;

  # main error log
  error_log  /var/log/nginx_error.log debug;

  # no sendfile on OSX
  sendfile on;

  # These are good default values.
  tcp_nopush        on;
  tcp_nodelay       off;
  # output compression saves bandwidth 
  gzip            on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  # this is where you define your mongrel clusters. 
  # you need one of these blocks for each cluster
  # and each one needs its own name to refer to it later.
  #
  # Rename to mongrel_site1, mongrel_site2, etc if using
  # virtual hosts.
  upstream mongrel {
    server 127.0.0.1:5000;
    # server 127.0.0.1:5001;
    # server 127.0.0.1:5002;
  }


  # Copy this section on down and put into a separate file 
  # if you want to organize your virtual hosts in files.
  #
  # Then include here with
  #
  #   include /usr/local/nginx/conf/vhosts/my_subdomain.conf
  #
  # the server directive is nginx's virtual host directive.
  server {
    # port to listen on. Can also be set to an IP:PORT.
    listen 80;
    
    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # sets the domain[s] that this vhost server requests for
    # server_name www.[engineyard].com [engineyard].com;

    # doc root
    root /var/www/apps/mysite.com/current/public;

    # vhost specific access log
    access_log  /var/www/apps/mysite.com/shared/log/nginx.vhost.access.log  main;

    # NOTE Uncomment and edit to redirect all subdomains back to domain.com
    #      Useful for sending .net and .org variants back to your site.
    # if ($host !~ ^domain\.com$) {
    #   rewrite ^.+ http://domain.com$uri permanent;
    #   break;
    # }

    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }

    location / {
      # Uncomment to allow server side includes so nginx can 
      # post-process Rails content
      ## ssi on;

      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
      
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) { 
        break; 
      }

      # check for index.html for directory index
      # if its there on the filesystem then rewite 
      # the url to add /index.html to the end of it
      # and then break to send it to the next config rules.
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }

      # this is the meat of the rails page caching config
      # it adds .html to the end of the url and then checks
      # the filesystem for that file. If it exists, then we
      # rewite the url to have explicit .html on the end 
      # and then send it on its way to the next config rule.
      # if there is no file on the fs then it sets all the 
      # necessary headers and proxies to our upstream mongrels
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        # Use other cluster name here if you are running multiple
        # virtual hosts.
        proxy_pass http://mongrel;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /var/www/apps/mysite.com/current/public;
    }
  }

  # This server is setup for ssl. Uncomment if 
  # you are using ssl as well as port 80.
  # server {
  #   # port to listen on. Can also be set to an IP:PORT
  #   listen 443;
  #   
  #   # Set the max size for file uploads to 50Mb
  #   client_max_body_size 50M;
  # 
  #   # sets the domain[s] that this vhost server requests for
  #   # server_name www.[engineyard].com [engineyard].com;
  #
  #   # doc root
  #   root /var/www/apps/mysite.com/current/public;
  # 
  #   # vhost specific access log
  #   access_log  /var/www/apps/mysite.com/shared/log/nginx.vhost.access.log  main;
  # 
  #   # NOTE See also http://blog.imperialdune.com/2007/3/31/setting-up-godaddy-turbo-ssl-on-nginx
  #   #      if you are buying a GoDaddy SSL cert.
  #   ssl                  on;
  #   ssl_certificate      /var/keys/domain.com.crt;
  #   ssl_certificate_key  /var/keys/domain.com.key;
  #
  #   # NOTE Uncomment and edit to redirect all subdomains back to domain.com
  #   #      Useful for sending .net and .org variants back to your site.
  #   if ($host !~ ^domain\.com$) {
  #     rewrite ^.+ https://domain.com$uri permanent;
  #     break;
  #   }
  #
  #   # this rewrites all the requests to the maintenance.html
  #   # page if it exists in the doc root. This is for capistrano's
  #   # disable web task
  #   if (-f $document_root/system/maintenance.html) {
  #     rewrite  ^(.*)$  /system/maintenance.html last;
  #     break;
  #   }
  # 
  #   location / {
  #     # needed to forward user's IP address to rails
  #     proxy_set_header  X-Real-IP  $remote_addr;
  # 
  #     # needed for HTTPS
  #     proxy_set_header X_FORWARDED_PROTO https;
  # 
  #     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  #     proxy_set_header Host $http_host;
  #     proxy_redirect false;
  #     proxy_max_temp_file_size 0;
  #     
  #     # If the file exists as a static file serve it directly without
  #     # running all the other rewite tests on it
  #     if (-f $request_filename) { 
  #       break; 
  #     }
  # 
  #     # check for index.html for directory index
  #     # if its there on the filesystem then rewite 
  #     # the url to add /index.html to the end of it
  #     # and then break to send it to the next config rules.
  #     if (-f $request_filename/index.html) {
  #       rewrite (.*) $1/index.html break;
  #     }
  # 
  #     # this is the meat of the rails page caching config
  #     # it adds .html to the end of the url and then checks
  #     # the filesystem for that file. If it exists, then we
  #     # rewite the url to have explicit .html on the end 
  #     # and then send it on its way to the next config rule.
  #     # if there is no file on the fs then it sets all the 
  #     # necessary headers and proxies to our upstream mongrels
  #     if (-f $request_filename.html) {
  #       rewrite (.*) $1.html break;
  #     }
  # 
  #     if (!-f $request_filename) {
  #       proxy_pass http://mongrel;
  #       break;
  #     }
  #   }
  # 
  #   error_page   500 502 503 504  /500.html;
  #   location = /500.html {
  #     root   /var/www/apps/mysite.com/current/public;
  #   }
  # }


}

nginx-php.conf [zz]

|

Source : http://topfunky.net/svn/shovel/nginx/conf/nginx-php.conf

##
# Basic config modified only slightly from http://brainspl.at/articles/2007/01/03/new-nginx-conf-with-optimizations
#
# Allows PHP to run alongside Ruby on Rails. Also 
# turns SSI on and uses locations as defined in install-nginx.sh script.
#
# See also http://topfunky.net/svn/shovel/nginx

# user and group to run as
user  deploy deploy;

# number of nginx workers
worker_processes  6;

# pid of nginx master process
pid /var/run/nginx.pid;

# Number of worker connections. 1024 is a good default
events {
  worker_connections 1024;
}

# start the http module where we config http access.
http {
  # pull in mime-types. You can break out your config 
  # into as many include's as you want to make it cleaner
  include /usr/local/nginx/conf/mime.types;

  # set a default type for the rare situation that
  # nothing matches from the mimie-type include
  default_type  application/octet-stream;

  # configure log format
  log_format main '$remote_addr - $remote_user [$time_local] '
                  '"$request" $status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  # main access log
  access_log  /var/log/nginx_access.log  main;

  # main error log
  error_log  /var/log/nginx_error.log debug;

  # no sendfile on OSX
  sendfile on;

  # These are good default values.
  tcp_nopush        on;
  tcp_nodelay       off;
  # output compression saves bandwidth 
  gzip            on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;


  # this is where you define your mongrel clusters. 
  # you need one of these blocks for each cluster
  # and each one needs its own name to refer to it later.
  upstream mongrel {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    # server 127.0.0.1:8002;
  }


  # the server directive is nginx's virtual host directive.
  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 80;
    
    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # sets the domain[s] that this vhost server requests for.
    # None means listen to all.
    #server_name www.rubyonrailsworkshops.com rubyonrailsworkshops.com;

    # doc root
    root /var/www/apps/rubyonrailsworkshops/current/public;

    # vhost specific access log
    access_log  /var/log/nginx.vhost.access.log  main;

    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }

    location / {
      # Uncomment to allow server side includes so nginx can 
      # post-process Rails content
      ## ssi on;

      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
      
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) { 
        break; 
      }

      # check for index.html for directory index
      # if its there on the filesystem then rewite 
      # the url to add /index.html to the end of it
      # and then break to send it to the next config rules.
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }

      # Look for existence of PHP index file.
      # Don't break here...just rewrite it.
      if (-f $request_filename/index.php) {
        rewrite (.*) $1/index.php;
      }

      # this is the meat of the rails page caching config
      # it adds .html to the end of the url and then checks
      # the filesystem for that file. If it exists, then we
      # rewite the url to have explicit .html on the end 
      # and then send it on its way to the next config rule.
      # if there is no file on the fs then it sets all the 
      # necessary headers and proxies to our upstream mongrels
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /var/www/apps/rubyonrailsworkshops/current/public;
    }

    # Pass the PHP scripts to FastCGI server listening on ip:port.
    #
    # Requires you to start one instance of http://topfunky.net/svn/shovel/nginx/php-fastcgi.sh
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  /var/www/apps/rubyonrailsworkshops/current/public$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
    }

  }

  # This server is setup for ssl. Uncomment if 
  # you are using ssl as well as port 80.
  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 443;
    
    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # sets the domain[s] that this vhost server requests for
    # server_name www.[engineyard].com [engineyard].com;

    # doc root
    root /var/www/apps/rubyonrailsworkshops/current/public;

    # vhost specific access log
    access_log  /var/log/nginx.vhost.access.log  main;

    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }

    location / {
      ssi on;

      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header X_FORWARDED_PROTO https;

      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
      
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) { 
        break; 
      }

      # check for index.html for directory index
      # if its there on the filesystem then rewite 
      # the url to add /index.html to the end of it
      # and then break to send it to the next config rules.
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }

      # this is the meat of the rails page caching config
      # it adds .html to the end of the url and then checks
      # the filesystem for that file. If it exists, then we
      # rewite the url to have explicit .html on the end 
      # and then send it on its way to the next config rule.
      # if there is no file on the fs then it sets all the 
      # necessary headers and proxies to our upstream mongrels
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /var/www/apps/rubyonrailsworkshops/current/public;
    }
  }


}

xcache ZendOptimizer

|
之前一直是eaccelerator + ZendOptimizer的组合
现在换成了xcache+ZendOptimizer, 按照其安装方法,没有什么难度,尤其在BSD上,还是通过ports来安装
不过在重启php就出现问题了,总是报错.......
最终的解决方法是,把xcache.ini里
[xcache-common]
;; install as zend extension (recommended, but not working yet)
;; zend_extension = /usr/local/lib/php/20060613/xcache.so
; zend_extension_ts = /usr/local/lib/php/20060613/xcache.so
;; or install as extension
;;extension = xcache.so
关于用zend 扩展模式的两个加入到php.ini里在ZendOptimizer之前即可

freebsd php-cgi script

|

%cat /usr/local/etc/rc.d/fastcgi-php.sh
#!/bin/sh
#  FreeBSD rc.d script for fastcgi+php
#  in rc.conf
# fcgiphp_enable (bool):        Set it to "YES" to enable fastcgi+php
#                               Default is "NO".
# other options see below
#

. /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="16"}
: ${fcgiphp_port="9999"}
: ${fcgiphp_socket="/tmp/php-fastcgi.sock"}
: ${fcgiphp_env="SHELL PATH USER"}
: ${fcgiphp_max_requests="500"}
: ${fcgiphp_addr="localhost"}


pidfile=/var/run/fcgiphp/fcgiphp.pid
procname="${fcgiphp_bin_path}"
command_args="/usr/local/bin/spawn-fcgi -a ${fcgiphp_addr} -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"