NGINX

NGINX

服務器軟件
Nginx(enginex)就是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務。[1]Nginx是由IgorSysoev為俄羅斯訪問量第二的Rambler.ru站點開發的,第一個公開版本0.1.0發布于2004年10月4日。其将源代碼以類BSD許可證的形式發布,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。2011年6月1日,nginx1.0.4發布。Nginx是一款輕量級的Web服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,并在一個BSD-like協議下發行。
  • 中文名:nginx
  • 外文名:Nginx
  • 别名:engine x
  • 設計師:Igor Sysoev
  • 軟件許可:BSD許可
  • 軟件類型:開源軟件,網頁服務器軟件
  • 兼容性:Linux系統,Windows NT系統,OS X
  • 最新生産版本:1.90.10
  • 最新主線版本:1.20.0

簡介

Nginx(發音同 engine x)是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,并在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特點是占有内存少,并發能力強,事實上nginx的并發能力确實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:新浪、網易、 騰訊等。

優點

Nginx 可以在大多數 Unix like OS 上編譯運行,并有 Windows 移植版。 Nginx 的1.4.0穩定版已經于2013年4月24日發布,一般情況下,對于新建站點,建議使用最新穩定版作為生産版本,已有站點的升級急迫性不高。Nginx 的源代碼使用 2-clause BSD-like license。

Nginx 是一個很強大的高性能Web和反向代理服務器,它具有很多非常優越的特性:

在高連接并發的情況下,Nginx是Apache服務器不錯的替代品:Nginx在美國是做虛拟主機生意的老闆們經常選擇的軟件平台之一。能夠支持高達 50,000 個并發連接數的響應,感謝Nginx為我們選擇了 epoll and kqueue作為開發模型。最大限度地提高您的網站和應用程序的可用性和可靠性,并盡量減少失望的客戶和損失的收入。

服務器

Nginx作為負載均衡服務器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序對外進行服務,也可以支持作為 HTTP代理服務器對外進行服務。Nginx采用C進行編寫,不論是系統資源開銷還是CPU使用效率都比 Perlbal 要好很多。

代碼

Nginx代碼完全用C語言從頭寫成,已經移植到許多體系結構和操作系統,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows。Nginx有自己的函數庫,并且除了zlib、PCRE和OpenSSL之外,标準模塊隻使用系統C庫函數。而且,如果不需要或者考慮到潛在的授權沖突,可以不使用這些第三方庫。

郵件代理服務器

作為郵件代理服務器:Nginx 同時也是一個非常優秀的郵件代理服務器(最早開發這個産品的目的之一也是作為郵件代理服務器),Last. fm 描述了成功并且美妙的使用經驗。

Nginx 是一個安裝非常的簡單,配置文件非常簡潔(還能夠支持perl語法),Bugs非常少的服務器:Nginx 啟動特别容易,并且幾乎可以做到7*24不間斷運行,即使運行數個月也不需要重新啟動。你還能夠不間斷服務的情況下進行軟件版本的升級。

功能

支持的操作系統

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;

Windows XP,Windows Server 2003和Windows 7等。

結構與擴展

一個主進程和多個工作進程。工作進程是單線程的,且不需要特殊授權即可運行;

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 (臨時禁止事件), NOTE_LOWAT,EV_EOF,有效數據的數目,錯誤代碼;

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 非活動的 HTTP keep-alive 連接僅需要 2.5M内存。

最小化的數據拷貝操作;

其他HTTP功能:

基于IP 和名稱的虛拟主機服務;

Memcached 的 GET 接口;

支持 keep-alive 和管道連接;

靈活簡單的配置;

重新配置和在線升級而無須中斷客戶的工作進程;

可定制的訪問日志,日志寫入緩存,以及快捷的日志回卷;

4xx-5xx錯誤代碼重定向;

基于 PCRE 的 rewrite 重寫模塊;

基于客戶端IP 地址和 HTTP 基本認證的訪問控制;

PUT,DELETE,和 MKCOL 方法;

支持 FLV (Flash 視頻);

帶寬限制。

實驗特性

内嵌的 perl;

通過 aio_read()/aio_write() 的套接字工作的實驗模塊,僅在 FreeBSD 下;

對線程的實驗化支持,FreeBSD 4.x 的實現基于 rfork();

Nginx 主要的英語站點是 http://sysoev. ru/en/;

英語文檔草稿由 Aleksandar Lazic 完成 點擊。

HTTP基礎功能

處理靜态文件,索引文件以及自動索引;

反向代理加速(無緩存),簡單的負載均衡和容錯;

FastCGI,簡單的負載均衡和容錯;

模塊化的結構。過濾器包括gzipping,byte ranges,chunked responses,以及 SSI-filter。在SSI過濾器中,到同一個 proxy 或者 FastCGI 的多個子請求并發處理;

SSL 和 TLS SNI 支持;

IMAP/POP3代理服務功能:

使用外部 HTTP 認證服務器重定向用戶到 IMAP/POP3 後端;

使用外部 HTTP 認證服務器認證用戶後連接重定向到内部的 SMTP 後端;

其他HTTP功能

基于名稱和基于IP的虛拟服務器;

Keep-alive and pipelined connections support;保持活動和支持管線連接;

Flexible configuration;靈活的配置;

Reconfiguration and online upgrade without interruption of the client processing;重載配置,無間斷程序升級;

Access log formats,bufferred log writing,and quick log rotation;訪問日志格式,bufferred日志寫,快速登錄旋轉;

3xx-5xx error codes redirection; 3xx的- 5xx錯誤代碼重定向;

The rewrite module;重寫模塊;

Access control based on client IP address and HTTP Basic authentication;基于客戶端IP地址訪問控制和HTTP基本認證;

The PUT,DELETE,MKCOL,COPY and MOVE methods; 提交,删除,MKCOL,複制和移動方法;

FLV streaming;FLV視頻流;

Speed limitation;速度限制;

Limitation of simultaneous connections or requests from one address.限制同個IP地址請求數量。

Embedded perl.嵌入式的Perl。

郵件代理服務器功能

用戶重定向到IMAP/POP3後端使用外部HTTP認證服務器;

User authentication using an external HTTP authentication server and connection redirection to internal SMTP backend;用戶身份驗證使用外部HTTP認證服務器和連接重定向到内部的SMTP後端;

Authentication methods:驗證方法:

POP3: USER/PASS,APOP,AUTH LOGIN/PLAIN/CRAM-MD5;的POP3:用戶名/密碼,的APOP,AUTH的LOGIN/PLAIN/CRAM-MD5;

IMAP: LOGIN,AUTH LOGIN/PLAIN/CRAM-MD5; IMAP的:登錄,AUTH的LOGIN/PLAIN/CRAM-MD5;

SMTP: AUTH LOGIN/PLAIN/CRAM-MD5;的SMTP:AUTH的LOGIN/PLAIN/CRAM-MD5;

SSL support; SSL支持;

STARTTLS and STLS support. STARTTLS的和補充的支持。

認證方法

POP3: POP3 USER/PASS,APOP,AUTH LOGIN PLAIN CRAM-MD5;

IMAP: IMAP LOGIN;

SMTP: AUTH LOGIN PLAIN CRAM-MD5;

SSL 支持;

在 IMAP 和 POP3 模式下的 STARTTLS 和 STLS 支持。

安裝

模塊依賴性

gzip模塊需要zlib庫

rewrite模塊需要pcre庫

ssl 功能需要openssl庫

預先編譯好的安裝包

Nginx在一些Linux發行版和BSD的各個變種版本的安裝包倉庫中都會有,通過各個系統自帶的軟件包管理方法即可安裝。需要注意的是,很多預先編譯好的安裝包都比較陳舊,大多數情況下還是推薦直接從源碼編譯。

官方源代碼下載

特定平台的安裝和記錄

Nginx 在 Slackware 上的編譯安裝腳本

Nginx 在ubuntu和debian上的安裝及腳本,debian針對nginx包的官方下載站點

使用源代碼進行構建

Nginx 使用 Unix 下常用的 './configure && make && make install' 過程來編譯安裝。

configure腳本确定系統所具有一些特性,特别是 nginx 用來處理連接的方法。然後,它創建 Makefile 文件。

configure 支持下面的選項:

--prefix= - Nginx安裝路徑。如果沒有指定,默認為 /usr/local/nginx。

--sbin-path= - Nginx可執行文件安裝路徑。隻能安裝時指定,如果沒有指定,默認為/sbin/nginx。

--conf-path= - 在沒有給定-c選項下默認的nginx.conf的路徑。如果沒有指定,默認為/conf/nginx.conf。

--pid-path= - 在nginx.conf中沒有指定pid指令的情況下,默認的nginx.pid的路徑。如果沒有指定,默認為 /logs/nginx.pid。

--lock-path= - nginx.lock文件的路徑。

--error-log-path= - 在nginx.conf中沒有指定error_log指令的情況下,默認的錯誤日志的路徑。如果沒有指定,默認為 /logs/error.log。

--http-log-path= - 在nginx.conf中沒有指定access_log指令的情況下,默認的訪問日志的路徑。如果沒有指定,默認為 /logs/access.log。

--user= - 在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的用戶。如果沒有指定,默認為 nobody。

--group= - 在nginx.conf中沒有指定user指令的情況下,默認的nginx使用的組。如果沒有指定,默認為 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(實時信号)或者/dev/poll(一種類似select的模式,底層實現與SELECT基本相 同,都是采用輪訓方法) SELECT模式将是默認安裝模式

--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請求。這個模塊需要已經安裝了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" 頁

--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 執行文件的路徑

--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預處理器的路徑

--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 庫的使用。同時也會禁止 HTTP rewrite 模塊。在 "location" 配置指令中的正則表達式也需要 PCRE。

--with-pcre=DIR - 指定 PCRE 庫的源代碼的路徑。

--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 - 啟用調試日志

--add-module=PATH - Add in a third-party module found in directory PATH

在不同版本間,選項可能會有些許變化,請總是使用 ./configure --help 命令來檢查一下當前的選項列表。

使用技巧

對于chroot的支持是否在計劃之中

在什麼情況下使用Nginx比使用squid要好? 反之亦然。

大體上來說nginx主要用于反向加速代理而不是像squid那樣做為常規代理服務器。Nginx的最大優勢在于高負載情況下内存和CPU的低消耗。我不認為squid能給你帶來比nginx更好的性能。

有沒有人能給出一個完整的.conf配置文件來詳細的解讀一下怎麼配置和測試 IMAP模塊, 而不隻是關于 IMAP 的隻言片語啊?

依照 [NginxImapProxyExample] 開始你的配置. 關于不同配置參數的具體信息,請查看 [NginxMailCoreModule] 頁。

示例1: 用運行于apache上的php腳本做後端驗證。

示例2: 使用運行于同一個服務器的 nginx-embedded-perl模塊作為 imap/pop代理和認證後端。

某些東東不工作

(URL重寫,代理,路徑,...)

例如:如URL重寫(rewrite)不工作了或者是unix的路徑(/$PATH)的問題雲雲。

請仔細閱讀 [NginxDebugging] 并且 逐行 查看錯誤日志。

如果你沒找到錯誤 打起精神 試着到IRC或郵件列表裡說明一下你碰到的問題。

有沒有其它類似的Web服務器

Cherokee

Lighttpd (Lighty)

thttpd

關于各自的優缺點請使用自己喜歡的搜索引擎查找。

讓Nginx成為以postfix做為後端的SMTP代理。

Nginx使用什麼算法來實現負載均衡它能實現基于連接數的負載均衡嗎?

Nginx使用簡單的輪巡算法,所以無法做基本鍊接計數的負載均衡。這個可能會在将來的版本中有所改變。

我能關閉從代理服務器到後端服務器的緩存嗎或者使用上傳進度特性?

Nginx反向代理實踐

nginx + substitutions 安裝

nginx 自帶一個Substitution模塊,但該模塊隻能寫一行,所以我們改用 substitutions。

下面是安裝一些預備軟件

yum -y --noplugins install wget zip

yum -y --noplugins install unzip

yum -y --noplugins install gcc

yum -y --noplugins install make

yum -y --noplugins install pcre-devel

yum -y --noplugins install openssl-devel

編譯軟件

tar zxf nginx-1.0.8.tar.gz

cd nginx-1.0.8.tar.gz

/configure ./configure --add-module=path/substitutions4nginx-read-only //注意這裡的path是相對應的真實路徑。

make

make install

配置 nginx.conf

此時,nginx應該安裝在于 /usr/local/nginx下面

server_name servername;

location :

subs_filter ca-pub-ca-pub-; //把google ad 的用戶号 ca-pub-9805743306566114 改成你自己的,比如 ca-pub-

subs_filter; //把google ad 的廣告号改成你自己的,比如,你懂的!:D

proxy_pass 這裡是填寫需要代理的網址; //反向代理站。

index index.html index.htm;

記住subs_filter 命令的格式即可随意發揮想象力替換你想替換的廣告、超鍊接,等等。

基于太多人詢問下面的問題:

我能為了得到上傳進度而關閉代理的緩存嗎。

使用nginx我怎麼才能給用戶顯示上傳進度。

到目前為止 (2007-Apr-26) 還沒有辦法關閉到後端服務器的緩存。

NginxRewrite

1.Nginx Rewrite 基本标記(flags)複制内容到剪貼闆代碼:last – 基本上都用這個Flag。

break – 中止Rewirte,不在繼續匹配

redirect – 返回臨時重定向的HTTP狀态302

permanent – 返回永久重定向的HTTP狀态301

2. 正則表達式匹配,其中:代碼:

CODE:

* ——  為區分大小寫匹配

* ——* 為不區分大小寫匹配

* !——和!——*分别為區分大小寫不匹配及不區分大小寫不匹配

3. 文件及目錄匹配,其中CODE:

* -f和!-f用來判斷是否存在文件

* -d和!-d用來判斷是否存在目錄

* -e和!-e用來判斷是否存在文件或目錄

* -x和!-x用來判斷文件是否可執行

4.Nginx 的一些可用的全局變量,可用做條件判斷:

代碼:

CODE:

$args

$content_length

$content_type

$document_root

$document_uri

$host

$http_user_agent

$http_cookie

$limit_rate

$request_body_file

$request_method

$remote_addr

$remote_port

$remote_user

$request_filename

$request_uri

$query_string

$scheme

$server_protocol

$server_addr

$server_name

$server_port

$uri

配置

cls

@ECHO OFF

SET NGINX_PATH=D:

SET NGINX_DIR=D:nginx-1.3.6

color 0a

TITLE Nginx 管理程序 Power By ChenJianxiang

GOTO MENU:MENU

CLS

ECHO.

ECHO. * * * * Nginx 管理程序 Power By ChenJianxiang * * *

ECHO. * *

ECHO. * 1 啟動Nginx *

ECHO. * *

ECHO. * 2 關閉Nginx *

ECHO. * *

ECHO. * 3 重啟Nginx *

ECHO. * *

ECHO. * 4 退 出 *

ECHO. * *

ECHO. * * * * * * * * * * * * * * * * * * * * * * * *

ECHO.

ECHO.請輸入選擇項目的序号:

set /p ID=

IF "%id%"=="1" GOTO cmd1

IF "%id%"=="2" GOTO cmd2

IF "%id%"=="3" GOTO cmd3

IF "%id%"=="4" EXIT

PAUSE:cmd1

ECHO.

ECHO.啟動Nginx......

IF NOT EXIST %NGINX_DIR%nginx.exe ECHO %NGINX_DIR%nginx.exe不存在

%NGINX_PATH%

cd %NGINX_DIR%

IF EXIST %NGINX_DIR%nginx.exe start %NGINX_DIR%nginx.exe

ECHO.OK

PAUSE

GOTO MENU:cmd2

ECHO.

ECHO.關閉Nginx......

taskkill /F /IM nginx.exe > nul

ECHO.OK

PAUSE

GOTO MENU:cmd3

ECHO.

ECHO.關閉Nginx......

taskkill /F /IM nginx.exe > nul

ECHO.OK

GOTO cmd1

GOTO MENU

版本發布

2012年08月22日,Nginx 1.3.5 開發版發布。

2012年09月12日,Nginx 1.3.6 開發版發布。

2012年12月11日,Nginx 1.2.6 穩定版發布

2013年02月12日,Nginx 1.2.7 穩定版發布

2013年02月19日,Nginx 1.3.13 開發版發布

2013年04月03日,Nginx 1.2.8 穩定版發布。

2013年04月16日,Nginx 1.3.16 開發者版本發布。

2013年04月24日,Nginx 1.4.0 穩定版發布。

2013年05月07日,Nginx 1.4.1 穩定版發布。

2013年05月07日,Nginx 1.5.0 開發版本發布。

2013年06月04日,Nginx 1.5.1 主版本發布。

2013年07月02日,Nginx 1.5.2 開發版發布。

2013年07月17日,Nginx 1.4.2 穩定版發布。

2013年07月30日,Nginx 1.5.3 開發版本發布。

2013年08月27日,Nginx 1.5.4 開發版本發布。

2013年09月17日,Nginx 1.5.5 開發版本發布。

相關詞條

相關搜索

其它詞條