www.nginx.org
Tenginx:Taobao enginx
epoll();
模塊化設計,較好擴展性:但不支持模塊的動態裝卸載;Tenginx支持;
高可靠性
master –> worker
低內存消耗
支持熱部署
不停機而跟新配置文件,日志文件滾動,升級程序版本
靜態資源的web服務器,能夠緩存打開的文件描述符
http,smtp,pop3協議的反向代理服務器
緩存加速、負載均衡機制
支持FastCGI(ftm,LNMP),uWSGI(python)
模塊化(非DSO機制)、過濾器zip、SSI及圖像的大小調整
支持SSL
代理的概念
正向代理:客戶端請求的服務都被代理服務器代替來請求
反向代理:反向代理服務器會幫我們把請求轉發到真實的服務器那里去
正向代理代理的對象是客戶端,反向代理隱藏了真實的服務端
基于名稱和IP的虛擬主機
支持keepalive
支持平滑升級
定制訪問日志,支持使用日志緩沖區提供日志存儲性能
支持url rewrite
支持路徑名稱
支持基于IP及用戶的訪問控制
支持速率限制,支持并發訪問數限制
Nginx的架構特性
一個master進程,生存一個或多個worker進程
事件驅動模型:epoll(邊緣觸發)linux
復用器:select,poll,rt signal
支持sendfile,sendfile64
支持AIO
支持mmap(內存映射)
Nginx的工作模式:非阻塞、事件驅動、由一個master進程生成多個worker線程,每個worker響應n個請求
worker * n
Nginx模塊類型:
核心模塊
standard HTTP modules
Optional HTTP modules
Mail modeules
3rd party modules
rpm包:epel
源碼:編譯安裝
制作好的程序包:rpm包
PCRE(Perl Compatible Regular Expressions中文含義:perl語言兼容正則表達式),實現對URL重寫
1.安裝所依賴的pcre-devel包
#yum -y install pcre-devel
#groupadd -r nginx
#groupadd -g nginx -r nginx
3.編譯
#tar xf nginx-1.6.2.tar.gz
#cd nginx-1.6.2
#./configure –prefix=/usr/local/nginx –conf-path=/etc/nginx/nginx.conf –user=nginx –group=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/lock/nginx.lock –with-http_ssl_module –with-http_stub_status_module –with-http_gzip_static_module –with-http_flv_module –with-http_mp4_module –http-client-body-temp-path=/var/tmp/nginx/client –http-proxy-temp-path=/var/tmp/nginx/proxy –http-fastcgi-temp-path=/var/tmp/nginx/fastcgi –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
#make && make install
#mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi,uwsgi}
#/usr/local/nginx/sbin/nginx -t //檢查配置文件的語法
#/usr/local/nginx/sbin/nginx
Nginx的配置/etc/nginx/nginx.conf
main配置段:全局配置段
event:定義event模型工作特性
http {}:定義http協議相關的配置
配置指令:要以分好結尾,語法格式:
dirctive value1 [value2…]
支持使用變量
內置變量
模塊會提供內建變量的定義
自定義變量
set var_name value
主配置段的指令:
用于調試、定位問題
正常運行必備的配置
優化性能的配置
事件相關的配置
blocking nonblocking mutiplexing event-driven AIO
阻塞I/O 非阻塞I/O 復用性I/O 事件驅動I/O 異步I/O
Nginx: non-blocking event-drive aio
Nginx-1.8的新特性:
hash load balancing method
backend SSL certificate verification
thread pools support
proxy request buffering
主配置段的指令:
正常運行的必備配置
1.user USERNANE [GROUPNAME] //指定運行worker進程的用戶和組
user nginx nginx;
2.pid /path/to/pid_file; //指定nginx守護進程的pid文件
pid /var/run/nginx/nginx.pid
3.worker_rlimit_nofile #; //指定所有worker進程所能夠打開的最大文件句柄數
4.worker_rlimit_core size;
5.
性能優化相關的配置
1.worker_processes #; //指明worker進程的個數,通常略應該少于cpu物理核心數
2.worker_cpu_affinity cpumask …; //避免不了context switch,會提升緩存命中率;將worker進程綁定到指定的cpu上運行
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
4.timer_resolution interval; //計時器解析度,降低此值,可減少gettimeofday()系統調用次數;
5.worker_priority number; //指明worker進程的nice值 -20–19
事件相關的配置
1.accept_mutex {off|on};
master調度用戶請求至各worker進程時使用的負載均衡鎖:on表示能讓多個worker輪流地 序列化的響應請求
2.lock_file file;
accept_metex用到的鎖文件路徑
3.use method;
use [epoll|rtsig|select|poll] 指明使用的事件模型,建議讓Nginx自行選擇;
4.worker_connections #;
設定單個worker進程所能夠處理的最大并發連接數量
用于調試、定位問題:要使用debug級別,編譯時要啟用–with-debug選項
1.daemon {on|off} 是否以守護進程方式運行nginx,調試時應該設置為off
2.master_process {on|off} 是否以master/worker模型來運行Nginx;調試時可以設置為off
3.error_log file [level];
總結:常需要進行調整的參數
worker_precess worker_connections worker_cpu_affinity worker_priority
#nginx -s stop|reload|quit|reopne
Nginx做為web服務器時使用的配置
http{}:由ngx_http_core_module模塊所引入
配置框架:
http {
upstream {
}
server {
location URL {
root “/path/to/somedir”; //指定本地文件系統路徑;
if … {
}//if語句
}//類似于http中的<location>,用于定義URL于本地文件系統的映射關系;可以使用多次
} //每個server類似于httpd中的一個<VirtualHost>;server可以使用多次;
}
文中的某種
配置指令:
1.server {} //定義一個虛擬主機;
server {
listen 8080; //監聽的端口
server_name www.xiangx.com; //主機名稱
root “/web/www/html”; //類似DcoumentRoot
}
注意:提供root對應的文件
#/usr/local/nginx/sbin/nginx -t //檢查配置文件的語法
#/usr/local/nginx/sbin/nginx -s reload //重載配置文件
測試訪問
2.listen //監聽指定的地址和端口
listen address[:port];
listen port;
3.server_name NAME […];
后可以跟多個主機:名稱還可以使用正則表達式或通配符;
(1)先做精確匹配檢查;
(2)左側通配符匹配檢查;
(3)右側通配符匹配檢查;
(4)正則表達匹配檢查;如~^.*\.xiangx\.com$
(5)default_server;
4.root path;
設置資源路徑映射:用于指明請求的URL所對應的資源所在的文件系統上的起始路徑;
5.location
location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
功能:允許根據用戶請求的URL來匹配定義的各location,匹配到時,此請求將被響應的location配置塊中的匹配處理;
例如做訪問控制等功能
= :精確匹配檢查
~ :正則表達式模式匹配檢查,區分字符大小寫;
~* :正則表達式模式匹配檢查,不區分字符大小寫;
^~ :URI前半部分匹配,不檢查正則表達式;
匹配的優先級:= –> ^~ –> ~ –> ~* –> 不帶任何符號的location
sever {
listen 80;
server_name www.xiangx.com;
location / {
root “/web/www”;
}
location /images/ {
root “/web/images/”;
}
location ~*\.php$ {
fcgipass
}
}
6.alias path;
用于location匹配段,定義路徑別名
location /images/ {
root “/vhost/web/”;
}
http://www.xiangx.com/images/a.jpg <– /vhost/web/images/a.jpg
location /images/ {
alias “/www/pictures”;
}
http://www.xiangx.com/images/a.jpg <– /www/picture/a.jpg
注意:root表示指明路徑為對應的location “/” URL
alias表示路徑映射,即location指令后定義的URL是相對于alias所指明的路徑而言;
7.index file;
設置默認主頁面:
index index.php index.html;
8.error_page code […] [=code] URI @name
根據http響應狀態碼來指明特定的錯誤頁面;
error_page 404 /404_customed.html;
=code:以指定的響應碼進行響應,而不是默認的原來的響應,默認表示以新資源的響應碼為其響應碼;
server {
listen 8080;
server_name www.xiangx.com;
location / {
root “/web/www”;
error_page 404 /404.html;
}
}
9.基于IP的訪問控制機制
allow IP/NETWORK
deny IP/NETWORK
10.基于用戶的訪問的控制
basic,digest;
auth_basic “string” //說明認證原因
auth_basic_user_file //指定認證用戶的文件列表
賬號密碼文件建議使用htpasswd來創建
server {
listen 8080;
server_name www.xiangx.com;
location / {
root “/web/www/html”;
auth_basic “xiangx auth”;
auth_basic_user_file /etc/nginx/users/.htpasswd;
}
}
指定認證的用戶:
#htpasswd -c -m /etc/nginx/users/.htpasswd tom
11.Nginx的https實現
創建私有CA
#cd /etc/pki/CA
#(umask 077;openssl genrsa -out private/cakey.pem
#openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655
#touch index.txt serial
#echo 01 > serial
#mkdir /etc/nginx/ssl
#cd /etc/nginx/ssl
#(umask 077;openssl genrsa -out nginx.key 1024
#openssl req -new -key nginx.key -out nginx.csr
#openssl req -new -key nginx.key -out nginx.csr
server {
listen 443 ssl;
server_name www.ting.com
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_timeout 5m;
ssl_prefer_server_ciphers on;
root /web/www/html;
index index.html index.htm;
}
}
狀態頁面,僅能用于Location中
location /status {
stub_status on;
allow 192.168.100.0/24;
deny all;
}
測試訪問:http://192.168.100.2:8080/status
顯示結果:
Active connections: 4 //當前所有處于打開狀態的連接數
server accepts handled requests
44 44 29 //已經接收進來的連接 處理的連接 已經處理過的請求數,在保持連接模式下,請求數量可能會多余連接數量
Reading: 0 Writing: 1 Waiting: 3
Reading:正處于接收請求狀態的連接數
Writing:請求已經接收完成,正處于處理請求或發送響應過程的連接數
Waiting:保持連接模式,且處于活動連接數;
13 rewrite regex replacement flag;
如:rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
http://www.xiangx.com/images/a/b/c/1.jpg –> http:www.xiangx.com/imgs/a/b/c/1.jpg
flag
last:一旦rewrite規則重寫完成后,不再被后面其它的rewrite規則進行處理,而是由Uer Agent重新對重寫后的URL
再一次發送請求,并從頭開始執行類似的過程;
break:一旦此rewrite規則重寫完成后,不再被后面其它的rewrite規則進行處理,User Agent重新對重寫后的URL重新發起請求,且不再會被當前location內的任何rewrite規則檢查;
redirect:以302響應碼,返回新的URL 即臨時重定向
permanent:以301響應碼,返回新的URL 即永久重定向
location / {
root “/web/www/html”;
rewrite ^/bbs/(.*)$ /forum/$1 break;
}
備注:準備forum文件 echo “forum teting” > /web/www/html/forum/index.html
測試訪問:http://192.168.100.2:8080/bbs/
14 if
語法: if (condition) {…}
上下文:server location
condition:
1.變量名:變量值為空串,或者以“0”開始,則為false,其它均為true;
2.以變量為操作數均構成比較表達式,可使用= !=類似的比較操作符進行測試
3.正則表達式模式匹配操作
~:區分大小寫的模式匹配檢查
~*:不區分大小寫的模式匹配檢查
!~ 和 ~!*:對上面的兩種測試取反
4.測試路徑為文件存在性: -f !-f
5.測試指定路徑為目錄的可能性:-d !-d
6.測試文件的存在性:-e !-e
7.檢查文件是否有執行權限:-x !-x
如:
if ($http_user_agent ~* MSIE) {
rewrite ^(.*) /msie/$1 break;
}
15.防止盜鏈
location ~* \.(jpg|gif|png|jpeg)$ {
valid_referer none blocked www.xiangx.ocm; //定義合法引用
if ($invalid_referer) {
rewrite ^/ http://www.xiangx.com/403.html;
}
}
16.定制訪問日志格式
log_format
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
注意:此處可用變量為nginx各模塊內建變量;
17.網絡連接相關的配置
1.keepalive_timeout #; //長連接的超時時長,默認為75s;
2.keepalive_requests #; //在一個長連接上所能夠允許請求的最大資源數;
3.keepalive_disasble [msie6|safari|none]; //為指定類型的User Agent禁用長連接
4.tcp_nodelay on|off; //是否對長連接使用TCP_NODELAY選項;將每次請求的資源都單獨發送,不進行多個小資源合并發送,減少延遲;
5.client_header_timeout #; //讀取http請求報文首部的超時時長
6.client_body_timeout # //讀取http請求報文body部分的超時時長;
7.send_timeout #; //發送響應報文的超時時長;
18.fastcgi的相關配置
LNMP:PHP啟用fpm模型
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
原創文章,作者:xiangx,如若轉載,請注明出處:http://www.www58058.com/74505