Tengine和Nginx
Tengine簡介
從2011年12月開始:Tengine是由淘寶網發起的Web服務器項目。它在Nginx的基礎上,針對大訪問量網站的需求,添加了很多高級功能和特性。Tengine的性能和穩定性已經在大型的網站如淘寶網,天貓商城等得到了很好的檢驗。它的最終目標是打造一個高效、穩定、安全、易用的Web平臺。(來自於http://tengine.taobao.org/)
Nginx簡介
Nginx是一個開源項目,是一個高性能的HTTP server 和 reverse proxy,也可以作爲IMAP/POP的反向代理,通過簡單的配置,就可以實現高性能,且佔用系統資源較小。Nginx設計之初是爲了解決C10K問題,其非常輕量級,僅支持event模型。
特性
Nginx的特性
模塊化設計,較好的擴展性
高可靠性:master/worker
支持熱部署:不停機更新配置文件、更換日誌文件、更新服務器版本
低內存消耗:10000個keep-alive連接模式下的非活動連接僅消耗2.5M內存
event-driven,aio,mmap
DOS機制在 2016-04-26 nginx-1.10.0 版本中才支持
Tengine特性
繼承Nginx-1.6.2的所有特性,兼容Nginx的配置;
動態模塊加載(DSO)支持。加入一個模塊不再需要重新編譯整個Tengine;
支持SO_REUSEPORT選項,建連性能提升爲官方nginx的三倍;
同時支持HTTP v2協議和SPDY v3協議,可同時使用兩種協議;
流式上傳到HTTP後端服務器或FastCGI服務器,大量減少機器的I/O壓力;
更加強大的負載均衡能力,包括一致性hash模塊、會話保持模塊,還可以對後端的服務器進行主動健康檢查,根據服務器狀態自動上線下線,以及動態解析upstream中出現的域名;
輸入過濾器機制支持。通過使用這種機制Web應用防火牆的編寫更爲方便;
支持設置proxy、memcached、fastcgi、scgi、uwsgi在後端失敗時的重試次數
動態腳本語言Lua支持。擴展功能非常高效簡單;
支持管道(pipe)和syslog(本地和遠端)形式的日誌以及日誌抽樣;
支持按指定關鍵字(域名,url等)收集Tengine運行狀態;
組合多個CSS、JavaScript文件的訪問請求變成一個請求;
自動去除空白字符和註釋從而減小頁面的體積
自動根據CPU數目設置進程個數和綁定CPU親緣性;
監控系統的負載和資源佔用從而對系統進行保護;
顯示對運維人員更友好的出錯信息,便於定位出錯機器;
更強大的防***(訪問速度限制)模塊;
更方便的命令行參數,如列出編譯的模塊列表、支持的指令等;
可以根據訪問文件類型設置過期時間;
(來自於http://tengine.taobao.org/)
Tengine安裝
下載地址:http://tengine.taobao.org/
# yum -y install pcre-devel openssl-devel zlib-devel
# ./configure \
–prefix=/usr/local
–sbin-path=/usr/sbin/nginx
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–pid-path=/var/run/nginx.pid
–lock-path=/var/run/nginx.lock
–http-client-body-temp-path=/var/cache/nginx/client_temp
–http-proxy-temp-path=/var/cache/nginx/proxy_temp
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
–http-scgi-temp-path=/var/cache/nginx/scgi_temp
–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_gunzip_module
–with-http_gzip_static_module
–with-http_random_index_module
–with-http_secure_link_module
–with-http_stub_status_module
–with-http_auth_request_module
–with-threads
–with-stream
–with-stream_ssl_module
–with-http_slice_module
–with-file-aio
–with-http_v2_module
# make && make install
複製黏貼版:
./configure –prefix=/usr/local/nginx –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/run/nginx/nginx.lock –http-client-body-temp-path=/var/cache/nginx/client_temp –http-proxy-temp-path=/var/cache/nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp –http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp –http-scgi-temp-path=/var/cache/nginx/scgi_temp –user=nginx –with-http_ssl_module –with-http_gzip_static_module –with-http_auth_request_module –with-http_auth_request_module –with-http_stub_status_module && make && make install
啓動前配置:
echo “export PATH=$PATH:/usr/local/nginx/sbin” >/etc/profile.d/nginx.sh useradd -r nginx -s /sbin/nologin mkdir /var/cache/nginx/client_temp -pv
一、核心模塊
1、Main端配置
約定:
大部分模塊使用方式,tengine與nginx相同,nginx模塊使用可以參照nginx的官方站點,tengine。
Syntax: 表示功能或者選項,其後面有語法
Default: 默認值
Context: 可出現的上下文
A、必備常用配置
1、user 指定用於運行worker進程的用戶和組
Syntax: user user [group];
Default: user nobody nobody;
Context: main
2、pid 指定nginx進程的pid文件路徑
yntax: pid file;
Default:
pid nginx.pid;
Context: main
3、worker_rlimit_nofile number 單個worker進程所能夠打開的最大文件數
Syntax: worker_rlimit_nofile number;
Default: —
Context: main
B、性能優化相關配置
1、 worker_processes number | auto worker的進程數;通常應該爲CPU的核心數減1
Syntax: worker_processes number | auto;
Default:
worker_processes 1;
Context: main
2、worker_cpu_affinity cpumask … 將work進程綁定到cpu上
Syntax: worker_cpu_affinity cpumask …;
worker_cpu_affinity auto [cpumask];
Default: —
Context: main
For example:
2核cpu,開啓八個進程 worker_processes 8; worker_cpu_affinity 01 10 01 10 01 10 01 10; 8核cpu,開啓8個進程 worker_processes 8; worker_cpu_affinity 10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001; 8核cpu,開啓2個進程 worker_processes 2; worker_cpu_affinity 10101010 01010101;
3、worker_priority nice 定義work進程的優先級,數字越小優先級越高
[-20,19]
對應的優先級是:100-139
Syntax: worker_priority number;
Default:
worker_priority 0;
Context: main
C、調試定位問題
1、daemon on | off 是否以守護進程方式啓動nginx進程
Syntax: daemon on | off;
Default:
daemon on;
Context: main
2、master_process on | off 是否以master/worker模型啓動nignx進程
Syntax: master_process on | off;
Default:
master_process on;
Context: main
3、error_log file | stderr | syslog:server=address[,parameter=value] | memory:size [debug | info | notice | warn | error | crit | alert | emerg] 錯誤日誌記錄方式,及其日誌級別
Syntax: error_log file [level];
Default:
error_log logs/error.log error;
Context: main, http, mail, stream, server, location
方式:
file /PATH/TO/SOME_LOG_FILE;
stderr:發送到錯誤輸出;
syslog:server=address[,parameter=value]:發送給syslog服務器;
memory:size:緩衝區大小
For example:
error_log logs/error.log notice
2、events段配置
1、worker_connections number 每個worker進程所能夠併發打開的最大連接數
Syntax: worker_connections number;
Default:
worker_connections 512;
Context: events
最大併發數=最大連接數*worker_processes 4的數字
2、use method 指明併發連接請求處理時使用的方法
Syntax: use method;
Default: —
Context: events
3、accept_mutex on | off 啓用時,表示用於讓多個worker輪流地、序列化地響應新請求
Syntax: accept_mutex on | off;
Default:
accept_mutex on;
Context: events
3、server段常用配置
1、listen address[:port] [default_server] [ssl] [backlog=number] [rcvbuf=size] [sndbuf=size]
listen port [default_server] [ssl];
listen unix:path [default_server] [ssl]
default_server:默認虛擬主機
ssl:限制只能通過ssl連接提供服務
backlog:後緩隊列的長度
rcvbuf:接收緩衝大小
sndbuf:發送緩衝區大小
2、server_name name … 主機名
Syntax: server_name name …;
Default:
server_name “”;
Context: server
指明當前server的主機名;後可跟一個或空白字符分隔的多個主機;
支持使用*任意長度的任意字符;
支持~起始的正則表達式模式字符串;
應用策略:
(1) 首先精確匹配;
(2) 左則*通配符匹配;
(3) 右側*通配符匹配;
(4) 正則表達式模式匹配;
For example:
server_name www.example.com;
server_name *.example.com;
server_name www.example.*;
server_name ~^..example..$;
mail.example.com, www.example.com
3、tcp_nodelay on|off 對keepalived模式下的連接是否啓用TCP_NODELAY選項
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location
4、sendfile on | off 是否啓用sendfile功能
Syntax: sendfile on | off;
Default:
sendfile off;
Context: http, server, location, if in location
4、路徑相關配置
1、root path 設置web資源路徑映射
Syntax: root path;
Default:
root html;
Context: http, server, location, if in location
For example:
location /i/ { root /data/w3; }
2、location [ = | ~ | ~* | ^~ ] uri { … }
Syntax: location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
Default: —
Context: server, location
根據用戶請求的URI來匹配定義的location,匹配到時,此請求將被相應的location塊中的指令所處理;
=:URI精確匹配;
~:做正則表達式模式匹配,區分字符大小寫;
~*:做正則表達式模式匹配,不區分字符大小寫;
^~:對URI的左半部分做匹配檢查,不區分字符大小寫;
匹配優先級:=、^~、~/~*、不帶符號;
For example:
location = / { [ configuration A ]; }
location / { [ configuration B ]; }
location /documents/ { [ configuration C ]; }
location ^~ /images/ { [ configuration D ]; }
location ~* .(gif|jpg|jpeg)$ { [ configuration E ]; }
2、alias path 定義路徑別名
Syntax: alias path;
Default: —
Context: location
For example:
location /i/ { alias /data/w3/images/; }
3、error_page code … [=[response]] uri
根據用戶其你去的資源的http響應的狀態碼實現錯誤頁面重定向
Syntax: error_page code … [=[response]] uri;
Default: —
Context: http, server, location, if in location
Tengine配置選項衆多,此處就介紹到此,如果有興趣可以參考nginx官方文檔或者tengine的官方文檔,此處不在過多介紹,下面介紹幾個常見使用的模塊,ssl、rewrite、gzip、fastcgi、防盜鏈、stasus等功能
官方文檔:
http://tengine.taobao.org/documentation.html
http://nginx.org/en/docs/
二、Tengine常用模塊
1、Module ngx_http_rewrite_module
ngx_http_rewrite_module 模塊可以通過使用正則表達式,重定向請求url。
A、rewrite regex replacement [flag] 重定向請求
把用戶請求的URI基於regex做檢查,匹配到時將替換爲replacement指定的字符串
在同一個location中存在的多個rewrite規則會自上而下逐個被檢查(循環),可以使用flag控制此循環功能
如果replacement是以http://或https://開頭,則替換結果會直接以重定向方式返回給客戶端
[flag]:
last:重寫完成後停止對當前uri在當前location中的後續其它重寫操作,改爲對新uri的新一輪處理;
break:重寫完成後停止對當前uri在當前location中的後續其它重寫操作;
redirect:重寫完成後以臨時重定向方式直接返回重寫後生成的新URL給客戶端,由客戶對新URL進行請求;(302)
permanent:重寫完成後以永久重定向方式直接返回重寫後生成的新URL給客戶端,由客戶對新URL進行請求;(301)
B、rewrite_log on | off 是否啓用重寫日誌
啓用時,日誌信息被髮往錯誤日誌
Syntax:rewrite_log on | off;
Default:rewrite_log off;
Context:http, server, location, if
C、if (condition) { ... } 條件判斷機制
在條件滿足時,執行配置塊中的配置
condition:
比較表達式:
==, !=
~:模式匹配,區分字母大小寫;
~*:模式匹配,不區分字符大小寫;
!~:模式不匹配,區分字母大小寫;
!~*:模式不匹配,不區分字母大小寫;
文件及目錄存在性判斷:
-f, !-f:文件
-d, !-d:目錄
-e, !-e:存在
-x, !-x:執行
For example:
if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break; } if ($http_cookie ~* "id=([^;]+)(?:;|$)") { set $id $1; } if ($request_method = POST) { return 405; } if ($slow) { limit_rate 10k; } if ($invalid_referer) { return 403; }
D、return code [text
];停止處理並將指定的代碼返回給客戶端
Syntax:return code [text];
return code URL;
return URL;
Default:—
Context:server, location, if
E、set $variable value用戶自定義變量
Syntax:set $variable value;
Default:—
Context:server, location, if
ngx_http_rewrite_module 示例:
# 將所有訪問172.18.4.70下image都重定向到tupian目錄下 # vim /usr/local/nginx/conf/nginx.conf location /image { rewrite ^/image/(.*) /tupian/$1 last; rewrite ^/image/(.*) /tupian/$1 last; return 403; index index.html; }
測試效果:
# 添加測試頁面 # cat /usr/local/nginx/html/image/index.html <h1>image</h1> # cat /usr/local/nginx/html/tupian/index.html <h1>tupian</h1>
if (condition) { ... } 示例
if ($request_method = GET) { return 405; }
2、Module ngx_http_gzip_module
過濾器,對指定類型的資源壓縮傳輸以節約帶寬
gzip on | off;
啓用或禁用gzip壓縮響應報文;
Syntax:gzip on | off;
Default:gzip off;
Context:http, server, location, if in location
gzip_comp_level level;
指定壓縮比,1-9,默認爲1;
Syntax:gzip_comp_level level;
Default: gzip_comp_level 1;
Context:http, server, location
gzip_disable regex ...;
regex是匹配客戶端瀏覽器類型的模式,表示對所有匹配到的瀏覽器不執行壓縮響應;
Syntax:gzip_disable regex ...;
Default:—
Context:http, server, location
This directive appeared in version 0.6.23.
gzip_min_length length;
觸發啓用壓縮功能的響應報文的最小長度;
Syntax:gzip_min_length length;
Default:gzip_min_length 20;
Context:http, server, location
gzip_http_version 1.0 | 1.1;
設定啓用壓縮響應功能時,協議的最小版本;
Syntax:gzip_http_version 1.0 | 1.1;
Default:gzip_http_version 1.1;
Context:http, server, location
gzip_types mime-type ...;
指定僅執行壓縮的資源內容類型;默認爲text/html;
Syntax:gzip_types mime-type ...;
Default:gzip_types text/html;
Context:http, server, location
gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
對代理的請求基於何種屬性判斷其是否應該啓用壓縮功能;
Syntax:gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
Default:gzip_proxied off;
Context:http, server, location
Example Configuration:
示例:
gzip on;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_disable msie6;
gzip_min_length 2;
gzip_types text/plain text/css text/xml application/x-javascript application/xml application/json application/java-script;
測試示例:
未配置之前:查看Response Headers報文中是否有gzip選項
配置之後:查看Response Headers報文中是否有gzip選項
3、Module ngx_http_fastcgi_module
fastcgi_pass address address是fpm服務器監聽的地址和端口;
示例: fastcgi 127.0.0.1:9000;
fastcgi_index name fastcgi應用的主頁名稱
fastcgi_param parameter value [if_not_empty] 傳遞給fpm服務器的參數及其值
fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
path:文件系統路徑,用於存儲緩存的文件數據;
max_size=size:定義此路徑下的多大空間用於存儲緩存數據;
levels=#[:#[:#]]:緩存目錄層級定義;
levels=1:2
keys_zone=name:size 內存中用於緩存k/v映射關係的空間名稱及大小;
inactive=time
注意:只能定義在http上下文
For example:
fastcgi_cache_path /var/cache/nginx/client_temp levels=1:2 keys_zone=one:10m;
存儲格式:
/var/cache/nginx/client_temp/c/29/b7f54b2df7773722d382f4809d65029c
fastcgi_cache zone | off 是否啓用cache,如果啓用,數據緩存於哪個cache中
fastcgi_cache_key string 定義要使用的緩存鍵
例如: fastcgi_cache_key $request_uri;
fastcgi_cache_methods GET | HEAD | POST ... 緩存哪些類型的請求的相關數據
fastcgi_cache_valid [code ...] time 對不同響應碼設定其可緩存時長
注意:調用緩存時,至少應該 指定三個參數
fastcgi_cache
fastcgi_cache_key
fastcgi_cache_valid
測試示例:
# vim /usr/local/nginx/conf/nginx.conf fastcgi_cache_path /var/data/nginx/cache levels=1:2 keys_zone=mycache:10m inactive=4m; # 上面選項必須在http上下文 location \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_cache mycache; fastcgi_cache_key $request_uri; fastcgi_cache_valid any 10m; }
啓用前查看:/var/data/nginx/cache目錄
啓動後查看:/var/data/nginx/cache目錄
4、Module ngx_http_ssl_module
ssl on | off 是否啓用當前虛擬主機的ssl功能
Syntax:ssl on | off;
Default:ssl off;
Context:http, server
ssl_certificate file 當前虛擬主機使用的PEM格式的證書文件
Syntax: ssl_certificate file;
Default:—
Context:http, server
ssl_certificate_key file 當前虛擬機使用的證書文件中的公鑰配對兒的私鑰文件路徑 Syntax:ssl_certificate_key file;
Default:—
Context:http, server
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2] SSL協議的版本
Syntax:ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
Default:ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Context:http, server
ssl_session_cache off | none | [builtin[:size]] [shared:name:size] 指明ssl會話的緩存機制
Syntax:ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
Default:ssl_session_cache none;
Context:http, server
builtin:使用openssl內建的緩存機制,此爲各worker獨有;
shared:由各worker共享的緩存;
name:緩存空間的名稱;
size:字節爲單位的緩存空間的大小;每1MB內存空間可緩存4000個會話
ssl_session_timeout time ssl會話超時時長,指ssl session cache中緩存條目有效時長
Syntax:ssl_session_timeout time;
Default:ssl_session_timeout 5m;
Context:http, server
示例:
配置文件:證書使用方式請查看我的另外一篇博客 http://xinzong.blog.51cto.com/10018904/1763615
# vim /etc/nginx/nginx.conf server { listen 443 ssl; server_name www.ops.com; ssl_certificate /usr/local/nginx/ssl/nginx.crt; ssl_certificate_key /usr/local/nginx/ssl/nginx.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; location / { root html; index index.html index.htm; } } # nginx -s reload
訪問查看:
4、Module ngx_http_referer_module
valid_referers none | blocked | server_names | string ...
referers:定義合法的數據,此模塊用來定義哪些內容是合法的
Syntax:valid_referers none | blocked | server_names | string ...;
Default:—
Context:server, location
none表示請求報文首部沒有referer首部
blocked表示請求報文的referer首部沒有值
server_names表示其值是主機名
示例: 表示根據上面的定義,如果不合法就返回403
valid_referers none blocked server_names *.zx.com zx.* www.zx.org/galleries/ ~\.google\.; if ($invalid_referer) { return 403; }
5、Module ngx_http_stub_status_module
status_module:用於輸出nginx的基本狀態信息
示例:
# vim /etc/nginx/nginx.conf location /status { stub_status on; }
訪問並測試:
各參數的意義:
Active connection:活躍連接數
accepts:已經接受的客戶端請求的總數量
handled:已經處理的數量
request:客戶端發來的總請求數量
Reading:處於讀取客戶端請求報文首部的連接數
Writing:處於向客戶端發送響應報文過程的連接數
Waiting:處於等待客戶端發出請求的空閒請求連接數
對於Tengine就寫到這裏了,還有很多需要補充,後期會繼續更新此博客
作者:Ace
QQ1257465991
Linux運維攻城獅一隻
Q/A:如有問題請慷慨提出