Nginx 教程

主要文档

Nginx功能概述 为什么选择Nginx Nginx安装 常见问题(FAQ) 配置符号参考 调试 nginx 优化 Nginx 运行和控制Nginx

核心模块

Nginx事件模块 Nginx主模块

基本模块

Browser模块 Charset模块 Geo模块 HttpAccess模块 HttpAuthBasic模块 HttpAutoindex模块 HttpEmptyGif模块 HttpFcgi模块 HttpGzip模块 HttpHeaders模块 HttpIndex模块 HttpIndex模块. HttpLimit zone HttpLimitReqest模块 HttpLog模块 HttpProxy模块 HttpRewrite模块 HttpSSI模块 HttpUserId http核心模块 map Memcached

其他模块

Addition模块 EmbeddedPerl flv GooglePerftools HttpDav模块 HttpGeoIP HttpGzipStatic HttpImageFilter HttpRealIp HttpSecureLink HttpSSL HttpSubstitution HttpXSLT RandomIndex StubStatus模块

mail模块

MailAuth MailCore MailProxy MailSSL

安装

nginx php-fpm安装配置 nginx在fedora上的安装 nginx在freebsd上的安装 nginx在ubuntu上的安装 nginx在windows上的安装

配置示例和方法

HWLoadbalancerCheckErrors nginx防盗链 负载均衡 完整例子 完整例子2 虚拟主机

nginx在freebsd上的安装


一 . 安装必备软件 MySQL+PHP+Pcre

cd /usr/ports/database/mysql50-server && make install clean  
cd /usr/lang/php5/ && make install clean   选择对cgi mysql等的支持  
cd /usr/devel/pcre && make install clean 

用ports安装 /usr/ports/www/nginx, make install clean

二、弄了一个fastcgi的脚本,来自lighttpd

1) cd /usr/ports/www/lighttpd
2) make
3) cp /usr/ports/www/lighttpd/work/lighttpd-1.4.18/src/spawn-cgi /usr/bin
4) make clean

三、修改配置文件:

1,/usr/local/etc/nginx/nginx.conf:

user www www;  
worker_processes 10;  
error_log /usr/local/etc/nginx/logs/nginx_error.log crit;  
worker_rlimit_nofile 51200;  
events  
{  
   use epoll;  
   worker_connections 51200;  
}  
http  
{  
   include   conf/mime.types;  
   default_type application/octet-stream;  
   charset gb2312;  
   server_names_hash_bucket_size 128;  
   keepalive_timeout 60;  
   tcp_nodelay on;  
   gzip on;  
   gzip_min_length 1k;  
   gzip_buffers   4 8k;  
   gzip_http_version 1.1;  
   gzip_types   text/plain application/x-javascript. text/css text/html application/xml;  
   server  
   {  
   listen   80;  
   server_name www.test.com;  
   index index.html index.htm index.php;  
   root /usr/local/www/data/;  
   location ~ .*\.php?$  
   {  
   include fcgi.conf;  
   fastcgi_pass 127.0.0.1:9000;  
   fastcgi_index index.php;  
   }  

   log_format access '$remote_addr - $remote_user [$time_local] "$request" '  
   '$status $body_bytes_sent "$http_referer" '  
   '"$http_user_agent" $http_x_forwarded_for';  
   access_log /usr/local/nginx/logs/access.log access;  
   }  

   } 

2 ,先将php.ini的配置中 cgi.fix_pathinfo=1 这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。

3,编辑fcgi.conf文件,加入

fastcgi_param GATEWAY_INTERFACE CGI/1.1;  
fastcgi_param SERVER_SOFTWARE   nginx;  

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 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; 

四,启动

1, 启动fcgi

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/local/bin/php-cgi 

参数说明:

-f 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置
-a 绑定到地址addr
-p 绑定到端口port
-s 绑定到unix socket的路径path
-C 指定产生的FastCGI的进程数,默认为5(仅用于PHP)
-P 指定产生的进程的PID文件路径
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等

也可建立脚本
1) ee /usr/bin/php-fastcgi

#!/bin/sh  

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/local/bin/php-cgi 

2) chmod 755 /usr/bin/php-fastcgi

3) ee /usr/local/etc/rc.d/init-fastcgi

#!/bin/bash

PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php
RETVAL=$?
;;
restart)
killall -9 php
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL 

4) chmod 755 /usr/local/etc/rc.d/init-fastcgi

2,启动nginx
nginx -t -c /usr/local/etc/nginx/nginx.conf 测试配置是否正确
如果屏幕显示以下两行信息,说明配置文件正确:

the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok  
the configuration file /usr/local/etc/nginx/nginx.conf was tested successfully 

启动:

nginx -c /usr/local/etc/nginx/nginx.conf 

开机自动启动加入/etc/rc.conf

nginx_enable="YES" 

nginx参数:
-c 为 Nginx 指定一个配置文件,来代替缺省的。
-t不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
-v显示 nginx 的版本。
-V显示 nginx 的版本,编译器版本和配置参数。