Nginx ("engine x") 是一個(gè)高性能的 HTTP 和 反向代理 服務(wù)器,也是一個(gè) IMAP/POP3/SMTP 代理服務(wù)器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 站點(diǎn)開發(fā)的,它已經(jīng)在該站點(diǎn)運(yùn)行超過兩年半了。Igor 將源代碼以類BSD許可證的形式發(fā)布。盡管還是測(cè)試版,但是,Nginx 已經(jīng)因?yàn)樗姆€(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名了。
因?yàn)?span lang="EN-US"> Nginx 的文檔大部分是俄文的,Nginx 的中文維基的目的是幫助使用中文世界的用戶安裝、配置和交換有關(guān) Nginx 的使用經(jīng)驗(yàn)。大部分文檔源自Aleksandar Lazic <al-nginx AT none DOT at>的工作。有關(guān)本維基本身的問題請(qǐng)直接詢問Cliff Wells <cliff AT develix DOT com>。
Nginx 可以在大多數(shù) Unix like OS 上編譯運(yùn)行,并有 Windows 移植版。目前 Nginx 的開發(fā)版為 0.7.x,穩(wěn)定版為 0.6.x,歷史穩(wěn)定版為 0.5.x,建議使用 0.6 系列作為生產(chǎn)版本。 Nginx 的源代碼使用 2-clause BSD-like license。
Nginx 的中文維基:http://wiki.codemongers.com/NginxChs
Nginx 的中文站: http://www.nginx.cn
==為什么選擇Nginx ==
Nginx 是一個(gè)很牛的高性能Web和反向代理服務(wù)器, 它具有有很多非常優(yōu)越的特性:
在高連接并發(fā)的情況下,Nginx是Apache服務(wù)器不錯(cuò)的替代品: Nginx在美國(guó)是做虛擬主機(jī)生意的老板們經(jīng)常選擇的軟件平臺(tái)之一. 能夠支持高達(dá) 50,000 個(gè)并發(fā)連接數(shù)的響應(yīng), 感謝Nginx為我們選擇了 epoll and kqueue作為開發(fā)模型.
Nginx作為負(fù)載均衡服務(wù)器: Nginx 既可以在內(nèi)部直接支持 Rails 和 PHP 程序?qū)ν膺M(jìn)行服務(wù), 也可以支持作為 HTTP代理服務(wù)器對(duì)外進(jìn)行服務(wù). Nginx采用C進(jìn)行編寫, 不論是系統(tǒng)資源開銷還是CPU使用效率都比 Perlbal 要好很多.
作為郵件代理服務(wù)器: Nginx 同時(shí)也是一個(gè)非常優(yōu)秀的郵件代理服務(wù)器(最早開發(fā)這個(gè)產(chǎn)品的目的之一也是作為郵件代理服務(wù)器), Last.fm 描述了成功并且美妙的使用經(jīng)驗(yàn).
Nginx 是一個(gè) 安裝非常的簡(jiǎn)單 , 配置文件 非常簡(jiǎn)潔(還能夠支持perl語(yǔ)法), Bugs非常少的服務(wù)器: Nginx 啟動(dòng)特別容易, 并且?guī)缀蹩梢宰龅?span lang="EN-US">7*24不間斷運(yùn)行,即使運(yùn)行數(shù)個(gè)月也不需要重新啟動(dòng). 你還能夠 不間斷服務(wù)的情況下進(jìn)行軟件版本的升級(jí).
查看Nginx更多的特性: Feature Overview
==Nginx概述 ==
HTTP基礎(chǔ)功能:
處理靜態(tài)文件,索引文件以及自動(dòng)索引;
反向代理加速(無(wú)緩存),簡(jiǎn)單的負(fù)載均衡和容錯(cuò);
FastCGI,簡(jiǎn)單的負(fù)載均衡和容錯(cuò);
模塊化的結(jié)構(gòu)。過濾器包括gzipping, byte ranges, chunked responses, 以及 SSI-filter 。在SSI過濾器中,到同一個(gè) proxy 或者 FastCGI 的多個(gè)子請(qǐng)求并發(fā)處理;
SSL 和 TLS SNI 支持;
IMAP/POP3 代理服務(wù)功能:
使用外部 HTTP 認(rèn)證服務(wù)器重定向用戶到 IMAP/POP3 后端;
使用外部 HTTP 認(rèn)證服務(wù)器認(rèn)證用戶后連接重定向到內(nèi)部的 SMTP 后端;
認(rèn)證方法:
POP3: POP3 USER/PASS, APOP, AUTH LOGIN PLAIN CRAM-MD5;
IMAP: IMAP LOGIN;
SMTP: AUTH LOGIN PLAIN CRAM-MD5;
SSL 支持;
在 IMAP 和 POP3 模式下的 STARTTLS 和 STLS 支持;
支持的操作系統(tǒng):
FreeBSD 3.x, 4.x, 5.x, 6.x i386; FreeBSD 5.x, 6.x amd64;
Linux 2.2, 2.4, 2.6 i386; Linux 2.6 amd64;
Solaris 8 i386; Solaris 9 i386 and sun4u; Solaris 10 i386;
MacOS X (10.4) PPC;
結(jié)構(gòu)與擴(kuò)展:
一個(gè)主進(jìn)程和多個(gè)工作進(jìn)程。工作進(jìn)程是單線程的,且不需要特殊授權(quán)即可運(yùn)行;
kqueue (FreeBSD 4.1+), epoll (Linux 2.6+), rt signals (Linux 2.2.19+), /dev/poll (Solaris 7 11/99+), select, 以及 poll 支持;
kqueue支持的不同功能包括 EV_CLEAR, EV_DISABLE (臨時(shí)禁止事件), NOTE_LOWAT, EV_EOF, 有效數(shù)據(jù)的數(shù)目,錯(cuò)誤代碼;
sendfile (FreeBSD 3.1+), sendfile (Linux 2.2+), sendfile64 (Linux 2.4.21+), 和 sendfilev (Solaris 8 7/01+) 支持;
輸入過濾 (FreeBSD 4.1+) 以及 TCP_DEFER_ACCEPT (Linux 2.4+) 支持;
10,000 非活動(dòng)的 HTTP keep-alive 連接僅需要 2.5M 內(nèi)存。
最小化的數(shù)據(jù)拷貝操作;
其他HTTP功能:
基于IP 和名稱的虛擬主機(jī)服務(wù);
Memcached 的 GET 接口;
支持 keep-alive 和管道連接;
靈活簡(jiǎn)單的配置;
重新配置和在線升級(jí)而無(wú)須中斷客戶的工作進(jìn)程;
可定制的訪問日志,日志寫入緩存,以及快捷的日志回卷;
4xx-5xx 錯(cuò)誤代碼重定向;
基于 PCRE 的 rewrite 重寫模塊;
基于客戶端 IP 地址和 HTTP 基本認(rèn)證的訪問控制;
PUT, DELETE, 和 MKCOL 方法;
支持 FLV (Flash 視頻);
帶寬限制;
實(shí)驗(yàn)特性:
內(nèi)嵌的 perl
通過 aio_read()/aio_write() 的套接字工作的實(shí)驗(yàn)?zāi)K,僅在 FreeBSD 下。
對(duì)線程的實(shí)驗(yàn)化支持,FreeBSD 4.x 的實(shí)現(xiàn)基于 rfork()
Nginx 主要的英語(yǔ)站點(diǎn)是 http://sysoev.ru/en/
英語(yǔ)文檔草稿由 Aleksandar Lazic 完成 點(diǎn)擊。
==常見問題(FAQ)==
某些東東不工作 (URL重寫, 代理, 路徑, ...)
例如: 如URL重寫(rewrite)不工作了或者是unix的路徑(/$PATH)的問題云云...
請(qǐng)仔細(xì)閱讀 [NginxDebugging] 并且 逐行 查看錯(cuò)誤日志。
如果你沒找到錯(cuò)誤 打起精神 試著到IRC或郵件列表里說明一下你碰到的問題。
有沒有其它類似的Web服務(wù)器
Cherokee
Lighttpd (Lighty)
thttpd
關(guān)于各自的優(yōu)缺點(diǎn)請(qǐng)使用自己喜歡的搜索引摯查找
對(duì)于chroot的支持是否在計(jì)劃之中?
有人知道嗎?
在什么情況下使用Nginx比使用squid要好? 反之亦然。
大體上來(lái)說nginx主要用于反向加速代理而不是像squid那樣做為常規(guī)代理服務(wù)器。Nginx的最大優(yōu)勢(shì)在于高負(fù)載情況下內(nèi)存和CPU的低消耗。 我不認(rèn)為squid能給你帶來(lái)比nginx更好的性能。
有沒有人能給出一個(gè)完整的.conf配置文件來(lái)詳細(xì)的解讀一下怎么配置和測(cè)試 IMAP 模塊, 而不只是關(guān)于 IMAP 的只言片語(yǔ)啊?
依照 [NginxImapProxyExample] 開始你的配置. 關(guān)于不同配置參數(shù)的具體信息, 請(qǐng)查看 [NginxMailCoreModule] 頁(yè)。
示例1: 用運(yùn)行于apache上的php腳本做后端驗(yàn)證
示例2: 使用運(yùn)行于同一個(gè)服務(wù)器的 nginx-embedded-perl 模塊作為 imap/pop代理和認(rèn)證后端
怎么讓Nginx成為以postfix做為后端的SMTP代理?
有人知道不?
Nginx使用什么算法來(lái)實(shí)現(xiàn)負(fù)載均衡? 它能實(shí)現(xiàn)基于連接數(shù)的負(fù)載均衡嗎?
目前Nginx使用簡(jiǎn)單的輪巡算法,所以無(wú)法做基本鏈接計(jì)數(shù)的負(fù)載均衡。這個(gè)可能會(huì)在將來(lái)的版本中有所改變。
我能關(guān)閉從代理服務(wù)器到后端服務(wù)器的緩存嗎或者使用上傳進(jìn)度特性?
基于 太多人詢問下面的問題:
我能為了得到上傳進(jìn)度而關(guān)閉代理的緩存嗎
使用nginx我怎么才能給用戶顯示上傳進(jìn)度
...
到目前為止 (2007-Apr-26) 還沒有辦法關(guān)閉到后端服務(wù)器的緩存.
==安裝Nginx==
模塊依賴性
gzip 模塊需要 zlib 庫(kù)
rewrite 模塊需要 pcre 庫(kù)
ssl 功能需要 openssl 庫(kù)
預(yù)先編譯好的安裝包
Nginx在一些Linux發(fā)行版和BSD的各個(gè)變種版本的安裝包倉(cāng)庫(kù)中都會(huì)有,通過各個(gè)系統(tǒng)自帶的軟件包管理方法即可安裝。需要注意的是,很多預(yù)先編譯好的安裝包都比較陳舊,大多數(shù)情況下還是推薦直接從源碼編譯。
官方源代碼下載
點(diǎn)擊下載源代碼
特定平臺(tái)的安裝和記錄
Nginx 在 Slackware 上的編譯安裝腳本
Nginx 在ubuntu和debian上的安裝及腳本, debian針對(duì)nginx包的官方下載站點(diǎn)
使用源代碼進(jìn)行構(gòu)建
Nginx 使用 Unix 下常用的 './configure && make && make install' 過程來(lái)編譯安裝。
configure 腳本確定系統(tǒng)所具有一些特性,特別是 nginx 用來(lái)處理連接的方法。然后,它創(chuàng)建 Makefile 文件。
configure 支持下面的選項(xiàng):
--prefix=<path> - Nginx安裝路徑。如果沒有指定,默認(rèn)為 /usr/local/nginx。
--sbin-path=<path> - Nginx可執(zhí)行文件安裝路徑。只能安裝時(shí)指定,如果沒有指定,默認(rèn)為<prefix>/sbin/nginx。
--conf-path=<path> - 在沒有給定-c選項(xiàng)下默認(rèn)的nginx.conf的路徑。如果沒有指定,默認(rèn)為<prefix>/conf/nginx.conf。
--pid-path=<path> - 在nginx.conf中沒有指定pid指令的情況下,默認(rèn)的nginx.pid的路徑。如果沒有指定,默認(rèn)為 <prefix>/logs/nginx.pid。
--lock-path=<path> - nginx.lock文件的路徑。
--error-log-path=<path> - 在nginx.conf中沒有指定error_log指令的情況下,默認(rèn)的錯(cuò)誤日志的路徑。如果沒有指定,默認(rèn)為 <prefix>/logs/error.log。
--http-log-path=<path> - 在nginx.conf中沒有指定access_log指令的情況下,默認(rèn)的訪問日志的路徑。如果沒有指定,默認(rèn)為 <prefix>/logs/access.log。
--user=<user> - 在nginx.conf中沒有指定user指令的情況下,默認(rèn)的nginx使用的用戶。如果沒有指定,默認(rèn)為 nobody。
--group=<group> - 在nginx.conf中沒有指定user指令的情況下,默認(rèn)的nginx使用的組。如果沒有指定,默認(rèn)為 nobody。
--builddir=DIR - 指定編譯的目錄
--with-rtsig_module - 啟用 rtsig 模塊
--with-select_module --without-select_module - Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.
//允許或不允許開啟SELECT模式,如果 configure 沒有找到更合適的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(實(shí)時(shí)信號(hào))或者/dev/poll(一種類似select的模式,底層實(shí)現(xiàn)與SELECT基本相 同,都是采用輪訓(xùn)方法) SELECT模式將是默認(rèn)安裝模式
--with-poll_module --without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.
--with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.
//開啟HTTP SSL模塊,使NGINX可以支持HTTPS請(qǐng)求。這個(gè)模塊需要已經(jīng)安裝了OPENSSL,在DEBIAN上是libssl
--with-http_realip_module - 啟用 ngx_http_realip_module
--with-http_addition_module - 啟用 ngx_http_addition_module
--with-http_sub_module - 啟用 ngx_http_sub_module
--with-http_dav_module - 啟用 ngx_http_dav_module
--with-http_flv_module - 啟用 ngx_http_flv_module
--with-http_stub_status_module - 啟用 "server status" 頁(yè)
--without-http_charset_module - 禁用 ngx_http_charset_module
--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果啟用,需要 zlib 。
--without-http_ssi_module - 禁用 ngx_http_ssi_module
--without-http_userid_module - 禁用 ngx_http_userid_module
--without-http_access_module - 禁用 ngx_http_access_module
--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module
--without-http_autoindex_module - 禁用 ngx_http_autoindex_module
--without-http_geo_module - 禁用 ngx_http_geo_module
--without-http_map_module - 禁用 ngx_http_map_module
--without-http_referer_module - 禁用 ngx_http_referer_module
--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果啟用需要 PCRE 。
--without-http_proxy_module - 禁用 ngx_http_proxy_module
--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
--without-http_memcached_module - 禁用 ngx_http_memcached_module
--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module
--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module
--without-http_browser_module - 禁用 ngx_http_browser_module
--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module
--with-http_perl_module - 啟用 ngx_http_perl_module
--with-perl_modules_path=PATH - 指定 perl 模塊的路徑
--with-perl=PATH - 指定 perl 執(zhí)行文件的路徑
--http-log-path=PATH - Set path to the http access log
--http-client-body-temp-path=PATH - Set path to the http client request body temporary files
--http-proxy-temp-path=PATH - Set path to the http proxy temporary files
--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
--without-http - 禁用 HTTP server
--with-mail - 啟用 IMAP4/POP3/SMTP 代理模塊
--with-mail_ssl_module - 啟用 ngx_mail_ssl_module
--with-cc=PATH - 指定 C 編譯器的路徑
--with-cpp=PATH - 指定 C 預(yù)處理器的路徑
--with-cc-opt=OPTIONS - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".
--with-cpu-opt=CPU - 為特定的 CPU 編譯,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
--without-pcre - 禁止 PCRE 庫(kù)的使用。同時(shí)也會(huì)禁止 HTTP rewrite 模塊。在 "location" 配置指令中的正則表達(dá)式也需要 PCRE 。
--with-pcre=DIR - 指定 PCRE 庫(kù)的源代碼的路徑。
--with-pcre-opt=OPTIONS - Set additional options for PCRE building.
--with-md5=DIR - Set path to md5 library sources.
--with-md5-opt=OPTIONS - Set additional options for md5 building.
--with-md5-asm - Use md5 assembler sources.
--with-sha1=DIR - Set path to sha1 library sources.
--with-sha1-opt=OPTIONS - Set additional options for sha1 building.
--with-sha1-asm - Use sha1 assembler sources.
--with-zlib=DIR - Set path to zlib library sources.
--with-zlib-opt=OPTIONS - Set additional options for zlib building.
--with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro
--with-openssl=DIR - Set path to OpenSSL library sources
--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building
--with-debug - 啟用調(diào)試日志
--add-module=PATH - Add in a third-party module found in directory PATH
在不同版本間,選項(xiàng)可能會(huì)有些許變化,請(qǐng)總是使用 ./configure --help 命令來(lái)檢查一下當(dāng)前的選項(xiàng)列表。
示例 (最好能在同一行):
./configure \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=../pcre-4.4 \
--with-zlib=../zlib-1.1.3
Example on Ubuntu/debian with libgcrypt11-dev, libpcre3-dev and libssl-dev installed (choose EITHER --with-md5 OR --with-sha1, but not both; on debian and ubuntu, they should both point to /usr/lib)
./configure --with-openssl=/usr/lib/ssl/ --with-md5=/usr/lib
An Ubuntu Edgy .deb for version 0.5.2 can be found here: nginx_0.5.2-1_i386.deb.
(NOTE: According to an October 2006 message md5 was used in a now broken http cache module and sha1 is used in an incomplete mysql library module and so are currently not needed.)