Nginx安装与环境准备
鉴于Nginx日益普及,计划逐步替换现有的Apache服务,并在批量部署与管理层面进行考量。由于Nginx的模块均为静态编译,无法像Apache那样动态加载,因此初期必须充分评估需求。结合流行的LAMP架构,其中PHP最为常见,而管理FastCGI的主流工具有spawn-fcgi和php-fpm。旧版PHP需通过源码打补丁方式集成php-fpm,spawn-fcgi虽为lighttpd默认管理器,但涉及源码安装导致软件包版本在大规模部署时不够便捷,不过仍有解决途径。
Nginx的安装方式主要有两种:
1. 使用apt进行网络安装
修改软件源列表
vi /etc/apt/sources.list
#加入以下源
deb http://nginx.org/packages/debian/ squeeze nginx
deb-src http://nginx.org/packages/debian/ squeeze nginx
更新源列表后,通过apt命令直接安装nginx。
apt-key add nginx_signing.key
apt-get update
apt-get install nginx
2. 源码编译安装 首先搭建编译环境。鉴于Nginx后续运行可能需要Perl正则表达式、压缩算法及SSL等特性,需提前安装相应的库文件。
apt-get install build-essential
apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev libssl0.9.8
下载最新稳定版的Nginx源码包。
wget http://nginx.org/download/nginx-1.2.3.tar.gz 解压后查看可用的编译选项。
tar zxvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
#--help可以看到可以配置的参数
./configure --help
查看可选的配置参数(以下仅列举常用项):
--prefix=PATH 指定Nginx默认安装路径,未指定时默认为/usr/local/nginx。
--sbin-path=PATH 指定Nginx可执行文件路径,未指定时默认为/sbin/nginx。
--conf-path=PATH 指定配置文件路径,未指定时默认为/conf/nginx.conf。
--error-log-path=PATH 当nginx.conf中未设置error_log时,错误日志默认存放于/logs/error.log。
--http-log-path=PATH 定义访问日志路径,若nginx.conf中未设置access_log,则默认为/logs/access.log。
--pid-path=PATH 当nginx.conf中未设置pid时,PID文件默认存放于/logs/nginx.pid。
--lock-path=PATH 锁文件存放路径,若nginx.conf中未指定,默认为/logs/nginx.lock。
--user=USER 若Nginx.conf中未定义进程属主,默认为nobody。
--group=GROUP 若Nginx.conf中未定义进程属组,默认为nobody。
--builddir=DIR 设置构建目录。
--with-rtsig_module 启用rtsig模式。
--with-select_module 启用select模式,若当前平台无其他有效模式则默认编译。
--without-select_module 禁止select模式。
--with-poll_module 启用poll模式,若当前平台无其他有效模式则默认编译。
--without-poll_module 禁止poll模式。
--with-file-aio 启用文件AIO支持。
--with-ipv6 开启IPv6支持。
--with-debug 开启调试模式。
以下模块默认未开启,可通过--with命令启用:
--with-http_ssl_module 启用SSL模块。
--with-http_realip_module 允许在后端记录客户端真实IP。
--with-http_addition_module 启用ngx_http_addition_module。
--with-http_flv_module 启用FLV模块。
--with-http_mp4_module 启用MP4模块。
--with-http_gzip_static_module 启用Gzip模块。
--with-http_secure_link_module 启用ngx_http_secure_link_module。
--with-http_stub_status_module 启用状态查看模块。
以下模块默认已开启,可通过--without选项关闭:
--without-http_charset_module 禁用ngx_http_charset_module。
--without-http_gzip_module 禁用Gzip压缩模块。
--without-http_ssi_module 禁用SSI模块。
--without-http_userid_module 禁用ngx_http_userid_module。
--without-http_access_module 禁用Access模块。
--without-http_auth_basic_module 禁用Auth Basic认证模块。
--without-http_autoindex_module 禁用目录列表模块。
--without-http_geo_module 禁用Geo模块。
--without-http_map_module 禁用Map模块。
--without-http_referer_module 禁用ngx_http_referer_module。
--without-http_rewrite_module 禁用重定向模块。
--without-http_proxy_module 禁用代理模块。
--without-http_fastcgi_module 禁用FastCGI模块。
--without-http_uwsgi_module 禁用ngx_http_uwsgi_module。
--without-http_scgi_module 禁用ngx_http_scgi_module。
--without-http_memcached_module 禁用ngx_http_memcached_module。
--without-http_limit_conn_module 禁用连接限制模块。
--without-http_limit_req_module 禁用请求限制模块。
--without-http_empty_gif_module 禁用ngx_http_empty_gif_module。
--without-http_browser_module 禁用浏览器模块。
--without-http_upstream_ip_hash_module 禁用Upstream IP哈希模块。
--with-http_perl_module 启用Perl模块。 --with-perl_modules_path=PATH 设置Perl模块路径。 --with-perl=PATH 设置Perl库路径。 --http-client-body-temp-path=PATH 设置HTTP客户端请求体临时文件路径。 --http-proxy-temp-path=PATH 设置HTTP代理临时文件路径。 --http-fastcgi-temp-path=PATH 设置HTTP FastCGI临时文件路径。 --http-uwsgi-temp-path=PATH 设置HTTP uWSGI临时文件路径。 --http-scgi-temp-path=PATH 设置HTTP SCGI临时文件路径。 --without-http 禁用HTTP服务。 --without-http-cache 禁用HTTP缓存。 --with-mail 启用Mail服务。 --with-mail_ssl_module 在Mail服务中启用SSL。 --without-mail_pop3_module 禁用ngx_mail_pop3_module。 --without-mail_imap_module 禁用ngx_mail_imap_module。 --without-mail_smtp_module 禁用ngx_mail_smtp_module。 --with-google_perftools_module 启用Google Perftools模块。 --with-cpp_test_module 启用cpp_test模块。 --add-module=PATH 启用外部模块。 --without-pcre 禁止使用Perl正则库。 --with-pcre 强制使用Perl正则库。
源码编译参数详解
执行编译安装流程。
./configure --prefix=/etc/nginx/ --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module
--with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module
--with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-file-aio --with-ipv6
编译过程结果如图所示:

执行make && make install完成编译与安装。
make && make install
FastCGI原理与PHP-FPM配置
PS:FastCGI回顾 FastCGI是一种可伸缩且高速的接口,用于在HTTP服务器与动态脚本语言之间通信。多数主流HTTP服务器(如Apache、Nginx和lighttpd)均支持FastCGI,同时PHP等脚本语言也广泛支持该接口。FastCGI是对传统CGI的改进。传统CGI性能较差,因为每次HTTP服务器遇到动态程序时都需重新启动脚本解析器,这在处理高并发时几乎不可用,且存在安全隐患,现已少用。FastCGI采用C/S结构,将HTTP服务器与脚本解析服务器分离,并在解析服务器上启动一个或多个守护进程。当HTTP服务器遇到动态程序时,直接交付给FastCGI进程执行并返回结果,使HTTP服务器能专注处理静态请求,从而大幅提升系统性能。 Nginx不直接解析外部程序(包括PHP),必须通过FastCGI接口调用。在Linux下,FastCGI接口表现为socket(文件或IP socket)。为此需要一个FastCGI wrapper来绑定固定socket。当Nginx发送CGI请求至该socket时,wrapper接收请求并派生新线程调用解释器处理脚本并读取数据;随后wrapper将数据通过FastCGI接口沿固定socket传回Nginx,最后由Nginx发送给客户端。这就是Nginx+FastCGI的完整运作机制。

关于php-fpm
PHP 5.3版本的源码已默认支持php-fpm,但Debian 6认为其未经过广泛测试,因此在Debian 6的软件仓库中,尽管PHP版本为5.3.3,却未包含php-fpm。若不愿手工编译安装PHP,可更换软件源。 修改源列表
vi /etc/apt/sources.list
deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all
更新源列表,并安装php5-fpm。
apt-get update wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -
apt-get install php5-fpm
安装其他常用的PHP 5组件。
apt-get install php5 php5-cgi php5-cli php5-mysql php5-memcache
启动php-fpm服务。
/etc/init.d/php5-fpm start
至此,最基本的Nginx+PHP环境搭建完毕。
Nginx核心配置解析
Nginx配置文件简要解析 Nginx配置文件结构大致如下:

配置文件主要参照编译完成后生成的默认配置。
主模块配置选项
user指令用于设置进程运行用户。若在源码编译时指定了Nginx用户,则按指定运行;若未指定,默认使用nobody账户。配置文件中该行通常处于注释状态。user指令可设两个参数,第一个指定进程所属用户,第二个为可选,指定进程所属组。
user nginx nobody;
设置工作进程数。每个工作进程为单线程,在CPU密集型环境中,可将worker_processes数目设置为CPU核数。
worker_processes 4;
指定Nginx错误日志文件位置。若要禁止错误日志,可使用error_log /dev/null。error_log可出现在main、http、server等不同字段中,文件后可指定记录的日志默认等级。
error_log logs/error.log;
设置PID文件路径,便于使用kill命令发送相关信号。
pid logs/nginx.pid;
event模块配置选项,主要用于控制Nginx处理连接的方式。
events {
#如果在configure时指定的不止一个事件模型,可以通过use告诉nginx要使用哪一个模型:seletc、poll、kqueue、epoll、rtsig、/dev/poll、eventport等
use epoll;
#worker_connections和worker_processes可以计算你的理论最大链接数, worker_connections*worker_processes
worker_connections 1024;
}
http模块主要用于设置HTTP服务器相关属性。
http {
#可以用include指令包含一些其他文件,支持通配符,可以使用绝对路径,也可以使用相对路径,相对路径以nginx.conf为根据
include mime.types;
#设置默认的MIME类型
default_type application/octet-stream;
#描述记录日志的格式,在定义格式时可以使用一些变量,各个变了的含义可以从各个模块汇总查找
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拷贝文件在内核态完成,更加高效
sendfile on;
#tcp_nopush on;
#可以设置两个值,第一个表示客户端与服务器长连接的超时时间,超过这个时间,服务器将关闭连接。第二个值指定的应答头中keep-alive中timeout的值,让浏览器知道什么时候关闭连接。
keepalive_timeout 65;
#开启gzip压缩
gzip on;
#在三次握手时,发送给客户端应答后的超时时间,目前还没进入连接状态,只完成了两次握手,如果在规定时间没收到应答包,nginx将关闭链接
send_timeout 30
server {
xxx
}
}
server模块嵌套在http模块中,主要用于配置虚拟主机。
server {
#指定server字段中可以被访问到的ip地址及端口
listen 80;
#将http请求的主机头与server中的server_name参数进行匹配,并找出第一个结果,如果没有server_name参数匹配上,则第一个出现listen的server将被匹配,多域名用空格分割
server_name www.nginx.com;
#设个指令是应答头重的content-type字段使用指定的编码集,off表示不在应答头重添加content-type信息
charset off;
#指定www.nginx.com域名的访问日志路径及格式
access_log logs/host.access.log main;
#如果在url中没有指定文件,则设置一个默认主页,可以设置多个文件,空格分开,可以在http、server、location中设置
index index.php index.htm;
#根据URL的不同需求进行配置,可以使用字符串和正则匹配,最确切的匹配被使用,搜索到第一个后会停止
# ~* 不区分大小写;~ 区分大小写;^~ 禁止在字符串匹配后检查正则;= 在URL和location之间精确匹配,匹配完成后不做额外搜索。
location /i/ {
#请求到达后的文件根目录,在请求中root会把location匹配的值加到root指定的值后面,请求/i/a.php,则会是/html/i/a.php响应
root html;
#在location中设置index
index index.html index.htm;
}
#为错误代码指定相应的错误界面,可以用在http、server、location字段中。
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
#精确匹配50x.html,真实响应是/html/50x.html
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
location ~ \.php$ {
proxy_pass http://127.0.0.1;
}
#配置php脚本传至fastcgi
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#/scripts是php脚本所在的目录
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
#拒绝访问.htaccess文件
location ~ /\.ht {
deny all;
}
}