nginx 這個輕量級、高性能的 web server 主要能夠幹兩件事情:php
〉直接做爲http server(代替apache,對PHP須要FastCGI處理器支持);
〉另一個功能就是做爲反向代理服務器實現負載均衡html
http://www.netcraft.com/
解決 c10k
connection 10000
http://blog.csdn.net/jysg9/article/details/7901321前端
阻塞模型
(只能一個用戶,其餘用戶等待)
-----------------------------------------------------------------------------
多進程模型
fork--->專子進程--->專用戶
(時間片,內存資源,大量進程切換(內核切換)額外內核時間,無助於用戶響應)
非DMA機制
用戶請求(HTML)----->進程(阻塞,不可中斷sleep, WAIT)---->內核(取)------>磁盤文件(HTML)--->
到內核空間內存(buffer cache)----->copy to 進程空間內存mysql
磁盤塊(1K,2K,4K)(內核要管理)----------->page frame 4k(內核管理)------>用戶空間
linux
DMA機制,直接內存防問
用戶進程請求 ---->(CPU給內核內存地址交給->DMA)---->磁盤文件---->DMA加載內核空間空間內存完成--CPU中斷(中斷當前進程)-->一次拷貝到用戶空間內存中
這樣CPU能夠處理網卡請求nginx
缺陷:每一個進程的地址空間是重複數據,內存使用效益低
-----------------------------------------------------------------------
一個進程多線程模型:
thread
linux輕量級進程 :lwp
linux 技持多少種類型的線程庫?如何切換切換線程庫
線程並行執行流
code----BSS(所有變量,未初始化變量)--- HEAP(文件)----動態內存區---STACK(局部變量)
解決每一個進程的地址空間是重複數據,共享數據,文件,內存需求小了許了(相對進程)web
每一個線程響應一個請求:
線程依然切換,可是輕量級算法
進程切換,保存現場,恢復現場(一級數據緩存,一級指令緩存據,二級數據緩存,CPU寄存器 須要恢復)
線程間只須要CPU寄存器切換sql
線程分配到多核CPU的每一個CPU中apache
http://www.cnblogs.com/EthanCai/p/3705834.html
CPU忙等:時間片內,每隔一段時間看看,自旋鎖
CPU閒等: 立刻切換掉,不會佔所有時間片
快速切換時,會致使線程抖動
---------------------------------------------------------------------------
多程程多線程模型
開機時,留一個CPU核,給系統,其餘CPU核進程綁定到第個進程,進程這樣須要切換
(多進程下)select (1024),每當內核準備好了一個IO,內核進行掃描文件描述符,設置,通 知用戶進程
-------------------------------------------------------------------------------
多線程:N個請求
一個線程響應多個請求
一個線程有多個IO(磁盤IO與網絡IO)
磁盤IO用AIO,不能阻塞線程,
多路IO機制,IO複用 (SELECT POLL)
基於事件驅動 epoll(linux) /dev/poll(solaris) kqueue(freebsd)
http://www.cnblogs.com/Anker/archive/2013/08/14/3258674.html
aio 不阻塞異步IO
NGINX:AIO,MMAP EVENT-DRIVEN,具備前端反向代理
httpd:
mpm
prefork:一個進程響應一個請求,1024
worker:一個線程響應一個請求,多進程,一個進程生成多個線程
event:基於事件驅動
支持反向代理
LAMP:
--->NGINX(反向代理)---->緩存服務器 ----> [apache(web server)(php模塊)] ---->mysqlproxy---->mysql主從
| | ----->緩存服務器----> [web server (動態內容,php頁面)]
| |
| NGINX(服務器靜態 CSS JPG)
|
HTTP附件服務器
IO模型:
同步阻塞:
異步阻塞: IO複用
異步阻塞:event-driven
異步非阻塞:AIO
NGINX:
mmap
event-driver :一個進程響應多個請求:單線程進程
aio
lemp:
-------->nginx(輸入與輸出緩存區)----->fastcgi(php服務器):同步
--------nginx(輸入與輸出緩存區)------暫存請求NGINX------> PHP服務器 :異步
enginx(fastcgi)+php-fpm
nginx轉發:
location ~*\>php${
fastcgi_pass 127.0.0.1:9000
}
nginx配製文件
main
events{
事件驅動相關內容
}
location /forum/{
directive <parameters>;
proxy_pass http://172.16.100.11/bbs/;
URL的訪問屬性
}
http://www.magedu.com/forum/----->http://172.16.100.11:8080/bbs/;
httpd{
http相關的配置
}
server
{
虛擬主機
listen 80;
server_name www.baidu.com
location /{
後端服務器:
}}
模式:
location ~* ^/forum{
proxy_pass http://172.16.100.11:8080 :不加(FIRUM)
}
http://www.magedu.com/forum/----->http://172.16.100.11:8080/forum/;(forum,在服務器本地)
eg:
yum insqll httpd
cd /var/www/html/index.html
mv /var/www/html/index.html /var/www/html/bbs.html
location /forum/{
proxy_pass http://172.16.100.6/bbs/;
}
--------------------------------------------------------------
location ~* ^/forum/{
proxy_pass http://172.16.100.6;
}
http://172.16.100.6/forum
----------------------------------------
proxy_set_header X-Real-IP
$remote_addr
$remote_user
$request_filename
$request_method HTTP請求方法
GET POST HEAD PUT TRACE OPTIONS CONNECTION DELETE
http://172.16.100.6/bbs/
$request_uri /bbs/
$scheme http https
$server_name 代理服務器名
日誌反映真實客戶端IP
location ~* ^/forum/{
proxy_pass http://172.16.100.6;
proxy_set_header X-Real-IP $remote_addr
}
vim /etc/httpd/conf/httpd.conf
logformat 加入 %(X-Real-IP)i
---------------------------------------------------
整站轉換
location /{
proxy_pass http://172.16.100.6/;
proxy_set_header X-Real-IP $remote_addr
}
----------------------------------------------------
/var/www/html/index.html
upstream :負載均衡
upstream webservs{
ip_hash;
server 172.16.100.6 weight=1;
server 172.16.100.7 weight=1 max_fails=2 fail_timeout=2;
server 127.0.0.1:8080 backup; ////當 上面的SERVER 全DOWN 掉 重定向本機 //ip_hash不能使用 這行
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://websrvs/;
proxy_set_header X-Real-IP $remote_addr;
}}
----------------------------------------------------------------------------------
server{
listen 8080;
server_name localhost;
//定義本機錯誤頁面
root /web/errorpages; //本機目錄
index index.html;
location / {
proxy_pass http://websrvs/;
proxy_set_header X-Real-IP $remote_addr;
}}
-------------------------------------------------------------------
nginx: 算法
round-robin
ip_hash
least_conn
----------------------------------------------------------------------
netstat -ant |awk '/:80\>/{S[$NF]++}END{for(A in S){print A,S[A]}}'
----------------------------
upstream webservs{
#ip_hash;
server 172.16.100.6 weight=1;
server 172.16.100.7 weight=1 max_fails=2 fail_timeout=2;
#server 127.0.0.1:8080 backup; ////當 上面的SERVER 全DOWN 掉 重定向本機 //ip_hash不能使用 這行
}
緩存目錄級別:eg /nginx/cache/firest/2/b3/bc909.......
proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g
server{
listen 80;
server_name localhost;
add_header X-Via $server_addr; //誰提供的緩存IP
add_header X-Cache $upstream_cache_status from $server_addr"; //緩存是否命中 HIT MISS
location / {
proxy_pass http://websrvs/;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache first;
proxy_cache_valid 200 10m;
}}
mkdir /niinx/cache/first
cache_manager:lru
-------------------------------------------------------------------------
另外三種緩存:
open_log_cache:將日誌寫到緩存,而後再寫到磁盤
open_file_cache:文本無數據緩存,提供命中
fastcgi_cache:
NGINX 的limit 限制也基於共享內存實現
nginx:gzip以壓縮向客戶響應
gzip on
-------------------------------
nginx 區別應用
upstream phpservs{
server
server
}
upstream imgsrvs{
server
server
}
upstream staticfilesrvs{
server
server
}
location / {
root /web/htdocs;
index index.php index.html;
}
localhost ~* \.php${
fastcgi_pass http://phpservs;
}
location ~* "\.(jpg|jpeg|gif|png)$"{
proxy_pass http://imgsrvs;
}
-------------------------
nginx 區別應用
rewite:url重寫模塊
if (condition){
}
locaction /images/{
rewrite http://172.16.100.19/images/
}
支持正測表達式
location /{
root html;
index index.html;
rewrite "^/bbs/(.*)" http://172.16.100.19/forum/$1;
}
測試:雙目測試:
~ ,!~
= ,!=
~*,!~*
if ($request_method="POST"){
}
if ($request_uri ~* "/forum"){
}
單目測試:
referer:
location /photos/{
valid_referers none /blocked www.baidu.com
if($invalid_referer){
return 430;
}
}
----------------------------------------------------
http://tengine.taobao.org/book/index.html Nginx開發從入門到精通
last:本次與完成以後,重啓一下輪檢查
location / {
root html
index index.html
rewrite "^/bbs/(.*)images/(.*) \.jpg$ http://www.magedu.com/bbs/$2/images/$1.jpg last
http://www.magedu.com/bbs/a/image/b.jpg --->http://www.magedu.com/bbs/b/image/a.jpg
循環問題
-------------------------------------------------------
break:本次重寫完成以後,直接執行後續操做;
webdev:
/etc/httpd/conf/httpd.conf
Directory
dav on ----->PUT
setfacl -m u:apache:rwx /var/www/html/
curl -T /etc/issue http://172.16.100.7
讀寫分離:
location /{
proxy_pass http://172.16.100.6/;
if($request_method = "PUT"){
proxy_pass http://172.16.100.7;
}
}
curl http://172.16.100.106
curl -T /etc/fstab http://172.16.100.106
主 RSYNC+INOTIFY SERVER
讀寫分離
------------------------------------------------------------------------