Nginx ("engine x") 是一款高性能的,輕量級的HTTP Web 服務器 和 反向代理服務器及電子郵件 IMAP/POP3/SMTP 代理服務器。php
Nginx 是由俄羅斯的程序設計師 Igor Sysoev 所開發,爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,它已經在該站點運行超過四年多時間了,Igor 將源代碼以類BSD許可證的形式發佈。自Nginx 發佈四年來,Nginx 已經因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名了。Nginx 超越Apache的高性能和穩定性,使得國內使用 Nginx 做爲 Web 服務器的網站也愈來愈多。目前國內各大門戶網站已經部署了Nginx,如新浪、網易、騰訊等;新近發現 Nginx 技術在國內日趨火熱,愈來愈多的網站開始應用部署Nginx。css
(1)Nginx 能夠在大多數 Unix like OS 上編譯運行,並有 Windows 移植版。 Nginx 的1.2.6穩定版已經於2012年12月11日發佈,[1]1.3.10開發版已經於2012年12月25日發佈,若是新建站點,建議使用最新穩定版做爲生產版本,已有站點升級急迫性不高。Nginx 的源代碼使用 2-clause BSD-like license。html
(2)Nginx 是一個很強大的高性能Web和反向代理服務器,它具備不少很是優越的特性:
在高鏈接併發的狀況下,Nginx是Apache服務器不錯的替代品:Nginx在美國是作虛擬主機生意的老闆們常常選擇的軟件平臺之一。可以支持高達 50,000 個併發鏈接數的響應,感謝Nginx爲咱們選擇了 epoll and kqueue做爲開發模型。linux
(3)Nginx做爲負載均衡服務器:Nginx 既能夠在內部直接支持 Rails 和 PHP 程序對外進行服務,也能夠支持做爲 HTTP代理服務器對外進行服務。Nginx採用C進行編寫,不管是系統資源開銷仍是CPU使用效率都比 Perlbal 要好不少。做爲郵件代理服務器:Nginx 同時也是一個很是優秀的郵件代理服務器(最先開發這個產品的目的之一也是做爲郵件代理服務器),Last. fm 描述了成功而且美妙的使用經驗。nginx
(4)Nginx 是一個安裝很是的簡單,配置文件很是簡潔(還可以支持perl語法),Bugs很是少的服務器:Nginx 啓動特別容易,而且幾乎能夠作到7*24不間斷運行,即便運行數個月也不須要從新啓動。你還可以不間斷服務的狀況下進行軟件版本的升級。web
第一步:從 http://nginx.org/download/ 上下載相應的版本
直接在Linux上用命令下載正則表達式
wget http://nginx.org/download/nginx-1.5.9.tar.gz
若是你下載的包是tar.gz 或者.tar包,直接使用tar命令解壓文件。算法
tar -zxvf nginx-1.5.9.tar.gz
在命令行下進入解壓好的nginx源碼文件夾中,在該文件夾中存在可執行的configure文件,該執行程序是配置Nginx的安裝參數,好比安裝路徑等。後端
Nginx的安裝參數有如下幾種:瀏覽器
–prefix=<path> – Nginx安裝路徑。若是沒有指定,默認爲 /usr/local/nginx。
–sbin-path=<path> – Nginx可執行文件安裝路徑。只能安裝時指定,若是沒有指定,默認爲<prefix>/sbin/nginx。
–conf-path=<path> – 在沒有給定-c選項下默認的nginx.conf的路徑。若是沒有指定,默認爲<prefix>/conf/nginx.conf。
–pid-path=<path> – 在nginx.conf中沒有指定pid指令的狀況下,默認的nginx.pid的路徑。若是沒有指定,默認爲 <prefix>/logs/nginx.pid。
–lock-path=<path> – nginx.lock文件的路徑。
–error-log-path=<path> – 在nginx.conf中沒有指定error_log指令的狀況下,默認的錯誤日誌的路徑。若是沒有指定,默認爲 <prefix>/logs/error.log。
–http-log-path=<path> – 在nginx.conf中沒有指定access_log指令的狀況下,默認的訪問日誌的路徑。若是沒有指定,默認爲 <prefix>/logs/access.log。
–user=<user> – 在nginx.conf中沒有指定user指令的狀況下,默認的nginx使用的用戶。若是沒有指定,默認爲 nobody。
–group=<group> – 在nginx.conf中沒有指定user指令的狀況下,默認的nginx使用的組。若是沒有指定,默認爲 nobody。
–builddir=DIR – 指定編譯的目錄
–with-rtsig_module – 啓用 rtsig 模塊
–with-select_module –without-select_module – 容許或不容許開啓SELECT模式.
–with-http_ssl_module – 開啓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-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
一般狀況下,咱們只須要默認設置便可。 執行設置編譯參數,並設置安裝路徑爲/usr/local/nginx
./configure –prefix=/usr/local/nginx
在Linux命令行下,切換到nginx的源碼包目錄中(即剛剛解壓的路徑)
執行make命令
make的過程是把各類語言寫的源碼文件,變成可執行文件和各類庫文件
./make
第四步中執行make命令完成,若沒有出錯,則能夠執行安裝命令。
make install是把這些編譯出來的可執行文件和庫文件複製到合適的地方
./make install
在配置信息的時候,也就是在第三步,可能會出現一些錯誤:
錯誤爲:./configure: error: the HTTP rewrite module requires the PCRE library.
這個錯誤是因爲Nginx的rewrite模塊依賴於PCRE功能,若是須要啓用rewrite功能,須要首先安裝PCRE庫。(rewrite是Nginx中很重要的模塊,因此咱們須要安裝PCRE)
安裝完成PCRE庫以後,執行make和make install,軟件順利安裝。
下載pcre
pcre主頁位置爲:http://www.pcre.org/
選擇適當的方式下載pcre安裝包
wget http://sourceforge.net/projects/pcre/files/pcre/7.8/pcre-7.8.tar.bz2/download
解壓縮
進入到下載文件所在目錄中,執行解壓縮命令
tar xjpf pcre-7.8.tar.bz2
編譯安裝
當前目錄切換到pcre解壓後的目錄中
執行設置安裝命令:
./configure --prefix=/usr/local/pcre-7.8 --libdir=/usr/local/lib/pcre --includedir=/usr/local/include/pcre
編譯
make
編譯安裝
make install
程序的啓動方式是/usr/local/nginx/sbin/nginx
此時打開瀏覽器,輸入服務器地址看到以下畫面即爲安裝成功。
Nginx的配置文件爲存放在根目錄的conf文件夾中的nginx.conf
默認安裝完Nginx後,服務器會自帶一個nginx默認配置,用戶若須要實現本身的功能,須要修改配置文件來實現。
一般狀況下,咱們不直接在主配置文件中添加nginx配置信息,而是分不一樣的站點,配置不一樣的配置文件,而後引入到主文件中。
在nginx的配置文件conf目錄下建立一個專門存仿配置文件的目錄,命名爲vhosts,能夠把虛擬目錄的配置所有放在這裏。在裏面建立名爲websuitA.conf的配置文件。在主文件中的引入配置文件的作法在http{***}中引入以下代碼:
include /usr/local/nginx/conf/vhosts/*.conf;
websuitA.conf文件寫法以下:
server {
listen 80;#監聽的端口號
server_name websuitA.com;#域名
location / {
root X:/wnmp/www/websuitA; #站點的路徑
#若是用戶是用IE訪問,則跳轉到/nginx-ie/下
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
}
#錯誤頁的配置
error_page 404 /error.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
一般來講 一個nginx服務器可能會配置多個站點信息,好比:
a1.newegg.com => 192.168.1.1
a2.newegg.com => 192.168.1.2
a3.newegg.com => 192.168.1.3
此時,能夠經過多個server_name以及proxy配置來實現:
server {
listen 80;
server_name a1.newegg.com; #訪問的站點URL地址
location / {
proxy_store off; #反向代理緩存
proxy_redirect off; #不展現給客戶真實域名
proxy_pass http://192.168.1.1; #須要反向代理的服務器地址
proxy_set_header Host $host; #設置host爲請求的主機名稱
proxy_set_header X-real-ip $remote_addr; #用戶請求的IP地址
}
}
server {
listen 80;
server_name a2.newegg.com;
location / {
proxy_store off;
proxy_redirect off;
proxy_pass http://192.168.1.2;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
}
}
server {
listen 80;
server_name a3.newegg.com;
location / {
proxy_store off;
proxy_redirect off;
proxy_pass http://192.168.1.3;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
}
}
a1.nginx.com=>192.168.1.1、192.1.168.1.2、192.168.1.3:8080
配置以下:
server {
listen 80;
server_name a1.newegg.com; #訪問的站點URL地址
location / {
proxy_store off; #反向代理緩存
proxy_redirect off; #不展現給客戶真實域名
proxy_pass http://a1; #須要反向代理的服務器地址
proxy_set_header Host $host; #設置host爲請求的主機名稱
proxy_set_header X-real-ip $remote_addr; #用戶請求的IP地址
}
}
upstream a1{
server 192.168.1.1;
server 192.168.1.2;
server 192.168.1.3:8080;
}
www.newegg.com/a1/ => 192.168.1.1
www.newegg.com/a2/ => 192.168.1.2
示例:
server {
listen 80;
server_name www.newegg.com; #訪問的站點URL地址
location /a1/ {
proxy_store off; #反向代理緩存
proxy_redirect off; #不展現給客戶真實域名
proxy_pass http://192.168.1.1; #須要反向代理的服務器地址
proxy_set_header Host $host; #設置host爲請求的主機名稱
proxy_set_header X-real-ip $remote_addr; #用戶請求的IP地址
}
location /a2/ {
proxy_store off; #反向代理緩存
proxy_redirect off; #不展現給客戶真實域名
proxy_pass http://192.168.1.2; #須要反向代理的服務器地址
proxy_set_header Host $host; #設置host爲請求的主機名稱
proxy_set_header X-real-ip $remote_addr; #用戶請求的IP地址
}
}
www.newegg.com/test.jpg => image1.newegg.com/test.jpg
server {
listen 80;
server_name www.newegg.com;
location ~* \.jpg$ {
proxy_store off;
proxy_redirect off;
proxy_pass http://images;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
}
}
upstream a1{
server image1.newegg.com;
server image2.newegg.com;
server image3.newegg.com;
}
upstream
nginx的upstream目前支持5種方式的分配
1、輪詢(默認)
每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。
2、weight
指定輪詢概率,weight和訪問比率成正比,用於後端服務器性能不均的狀況。
例如:
upstream a1 {
server 192.168.1.1 weight=10;
server 192.168.1.2 weight=10;
}
3、ip_hash
每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。
例如:
upstream a1 {
ip_hash;
server 192.168.1.1;
server 192.168.1.2;
}
4、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream a1 {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按訪問url的hash結果來分配請求,使每一個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
例:在upstream中加入hash語句,server語句中不能寫入weight等其餘的參數,hash_method是使用的hash算法
upstream a1 {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
示例:
upstream a1{
#定義負載均衡設備的Ip及設備狀態
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在須要使用負載均衡的server中增長
proxy_pass http://a1;
每一個設備的狀態設置爲:
1.down 表示單前的server暫時不參與負載
2.weight 默認爲1.weight越大,負載的權重就越大。
3.max_fails :容許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗後,暫停的時間.
5.backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。
nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
client_body_in_file_only 設置爲On 能夠講client post過來的數據記錄到文件中用來作debug
client_body_temp_path 設置記錄文件的目錄 能夠設置最多3層目錄
location 對URL進行匹配.能夠進行重定向或者進行新的代理 負載均衡
要用nginx作負載均衡的話,首先要在配置文件裏面定義一組用來負載均衡的後端服務器(backend servers),例如:
upstream a1 {
server 192.168.1.1;
server 192.168.1.2;
server 192.168.1.3;
}
那個server指令的語法是 server name [parameters],這裏的name是服務器名,能夠是域名、ip或者unix socket,也能夠指定端口,例如:
server 192.168.1.1:8080;
server指令可用的參數有:
weight —— 設置服務器的權重,默認值是1,權重值越大那麼該服務器被訪問到的概率就越大,例如 server 192.168.1.11 weight=5;
max_fails和fail_timeout —— 這倆是關聯的,若是某臺服務器在fail_timeout時間內出現了max_fails次鏈接失敗,那麼nginx就會認爲那個服務器已經掛掉,從而在fail_timeout時間內再也不去查詢它,fail_timeout的默認值是10s,max_fails的默認值是1(這意味着一發生錯誤就認爲服務器掛掉),若是把max_fails設爲0則表示把這個檢查取消。
down —— 表示該服務器已經停用,例如server 192.168.1.11 down;
backup —— 表示該服務器是備用服務器,只有其它後端服務器都掛了或者很忙纔會訪問到。
舉個例子:server 192.168.1.11 max_fails=3 fail_timeout=30s; 這表示,若是服務器192.168.1.11在30秒內出現了3次錯誤,那麼就認爲這個服務器工做不正常,從而在接下來的30秒內nginx再也不去訪問這個服務器。
Location
語法規則: location [=|~|~*|^~] /uri/ { … }
開頭表示精確匹配
^~ 開頭表示uri以某個常規字符串開頭,理解爲匹配 url路徑便可。nginx不對url作編碼,所以請求爲/static/20%/aa,能夠被規則^~ /static/ /aa匹配到(注意是空格)。
~ 開頭表示區分大小寫的正則匹配
~* 開頭表示不區分大小寫的正則匹配
!~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配 的正則
/ 通用匹配,任何請求都會匹配到。
多個location配置的狀況下匹配順序爲(參考資料而來,還未實際驗證,試試就知道了,沒必要拘泥,僅供參考):
首先匹配 =,其次匹配^~, 其次是按文件中順序的正則匹配,最後是交給 / 通用匹配。當有匹配成功時候,中止匹配,按當前匹配規則處理請求。
例子,有以下匹配規則:
location = / {
#規則A
}
location = /login {
#規則B
}
location ^~ /static/ {
#規則C
}
location ~ \.(gif|jpg|png|js|css)$ {
#規則D
}
location ~* \.png$ {
#規則E
}
location !~ \.xhtml$ {
#規則F
}
location !~* \.xhtml$ {
#規則G
}
location / {
#規則H
}
那麼產生的效果以下:
訪問根目錄/, 好比 http://localhost/ 將匹配規則A
訪問 http://localhost/login 將匹配規則B
訪問 http://localhost/register 則匹配規則H
訪問 http://localhost/static/a.html 將匹配規則C
訪問 http://localhost/a.gif, http://localhost/b.jpg 將匹配規則D和規則E,可是規則D順序優先,規則E不起做用
而 http://localhost/static/c.png 則優先匹配到規則C
訪問 http://localhost/a.PNG 則匹配規則E,而不會匹配規則D,由於規則E不區分大小寫。
訪問 http://localhost/a.xhtml 不會匹配規則F和規則G,http://localhost/a.XHTML 不會匹配規則G,由於不區分大小寫。規則F,規則G屬於排除法,符合匹配規則可是不會匹配到,因此想一想看實際應用中哪裏會用到。 http://localhost/a.XHTML不會匹配規則G,由於不區分大小寫。規則F,規則G屬於排除法,符合匹配規則可是不會匹配到,因此想一想看實際應用中哪裏會用到。
訪問 http://localhost/category/id/1111 則最終匹配到規則H,由於以上規則都不匹配,這個時候應該是nginx轉發請求給後端應用服務器,好比FastCGI(php),tomcat(jsp),nginx做爲方向代理服務器存在。
因此實際使用中,我的以爲至少有三個匹配規則定義,以下:
location = / {
proxy_pass http://tomcat:8080/index
}
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
location / {
proxy_pass http://tomcat:8080/
}
防盜鏈
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
根據文件類型設置過時時間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
禁止訪問某個目錄
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
ReWrite
last – 基本上都用這個Flag。
break – 停止Rewirte,不在繼續匹配
redirect – 返回臨時重定向的HTTP狀態302
permanent – 返回永久重定向的HTTP狀態301
1、下面是能夠用來判斷的表達式:
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執行
2、下面是能夠用做判斷的全局變量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
一些可用的全局變量:
Ø $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
Redirect
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ 「^star\.igrow\.cn$" {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}