nginx-高性能WEB服務(wù)器
1 簡介
基礎(chǔ)篇:
Nginx介紹
Nginx編譯安裝
Nginx整合PHP
Nginx信號(hào)控制
應(yīng)用篇:
Nginx虛擬主機(jī)配置
Nginx日志切割
Nginx 與gzip設(shè)置
實(shí)戰(zhàn)篇:
Nginx與瀏覽器緩存配置
Nginx與Rewrite規(guī)則
Nginx與memcached
優(yōu)化篇:
Nginx連接數(shù)優(yōu)化
Nginx反向代理
Nginx集群與負(fù)載均衡
2 編譯安裝
linux># cd /usr/local/src
linux># wget http://nginx.org/download/nginx-1.14.2.tar.gz
linux># tar zxvf nginx-1.14.2.tar.gz
linux># cd nginx-1.14.2
linux># ./configure --prefix=/usr/local/nginx 【編譯】
這里報(bào)錯(cuò)了HTTP rewrite 缺少一個(gè)PCRE library
當(dāng)前目錄直接
linux># yum install pcre 【安裝pcre,正則表達(dá)式的庫】【ubuntu不支持yum的話根據(jù)提示來】
linux># yum install pcre-devel【或這個(gè)】【系統(tǒng)不一樣,安裝方式不一樣】
linux># ./configure --prefix=/usr/local/nginx 【編譯again】
linux># make && make install 【安裝】
3 簡單命令
3.1 啟動(dòng)
linux># cd /usr/local/nginx
linux># ll
....conf 配置文件
... html 網(wǎng)頁文件
...logs 日志文件
...sbin 主要二進(jìn)制程序
linux># ./sbin/nginx 【啟動(dòng)】
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
....
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
不能綁定80端口,80端口已經(jīng)被占用
(有時(shí)是自己裝了apache,nginx等,還有更多情況是操作系統(tǒng)自帶了apache并作為服務(wù)啟動(dòng))
解決: 把占用80端口的軟件或服務(wù)關(guān)閉即可.
kill -9 2985 【依據(jù)實(shí)際情況kill相關(guān)進(jìn)程】
pkill -9 http
3.2 關(guān)閉 【nginx的信號(hào)控制與進(jìn)程管理】
linux nginx># kill -INT 6425 【暴力關(guān)閉】【nginx master進(jìn)程id】
linux nginx># kill -HUP 6425 【平滑的刷新nginx的配置文件,不會(huì)立即生效,會(huì)幾秒后更新】
linux nginx>#
//信號(hào)
TERM,INT【Quick shutdown】
QUIT 優(yōu)雅的關(guān)閉進(jìn)程,即等請(qǐng)求結(jié)束后再關(guān)閉
HUP 改變配置文件,平滑的重讀配置文件
USR1 【Reopen the log files】【注意,nginx系統(tǒng)中,不會(huì)因?yàn)樾薷奈募薷闹赶颉? USR2 【平滑的升級(jí),用于升級(jí)的時(shí)候】
WITCH 【優(yōu)雅的關(guān)閉舊進(jìn)程(配合USR2來使用)】
linux nginx># kill -USR1 6425 【這樣就寫到新的日志文件中,不再指向舊的文件】
linux nginx># kill -HUP `cat logs/nginx.pid`【動(dòng)態(tài)獲取進(jìn)程號(hào)】
linux nginx># ./sbin/nginx -s reload 【效果同 -HUP】
linux nginx># ./sbin/nginx -s stop 【停止】
linux nginx># ./sbin/nginx -s reopen 【-USR1】
linux nginx># ./sbin/nginx -t 【校驗(yàn)配置文件】
4 nginx虛擬主機(jī)配置
linux nginx># vim ./conf/nginx.conf
4.1 Nginx配置段
//全局區(qū)001
worker_processes 1; // 有1個(gè)工作的子進(jìn)程,可以自行修改,但太大無益,因?yàn)橐獱帄ZCPU,一般設(shè)置為 CPU數(shù)*核數(shù)
//全局區(qū)002
Event {
// 一般是配置nginx連接的特性
// 如1個(gè)word能同時(shí)允許多少連接
worker_connections 1024; // 這是指 一個(gè)子進(jìn)程最大允許連1024個(gè)連接
}
//全局區(qū)003 這是配置http服務(wù)器的主要段
http {
server { // 這是虛擬主機(jī)段
Location { //定位,把特殊的路徑或文件再次定位 ,如image目錄單獨(dú)處理,如.php單獨(dú)處理
}
}
server {
}
}
4.2 虛擬主機(jī)配置
例子1: 基于域名的虛擬主機(jī)
server {
listen 80; #監(jiān)聽端口
server_name a.com; #監(jiān)聽域名
location / {
root a.com; #運(yùn)行根目錄:相對(duì)于nginx的根目錄
index index.html;
}
}
例子2: 基于端口的虛擬主機(jī)配置
server {
listen 2022;
server_name z.com;
location / {
root /var/www/html2022;
index index.html;
}
}
例子3: 基于IP的虛擬主機(jī)配置
server {
listen 8080;
server_name 192.168.1.200;
location / {
root html/ip;
index index.html;
}
}
5 nginx日志管理
我們觀察nginx的server段,可以看到如下類似信息
#access_log logs/host.access.log main;
這說明:該server, 它的訪問日志的文件是logs/host.access.log,使用的格式“main”格式.
除了main格式,你可以自定義其他格式.
main格式是什么?
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
main格式是我們定義好一種日志的格式,并起個(gè)名字,便于引用.
以上面的例子, main類型的日志,記錄的 remote_addr、http_x_forwarded_for等選項(xiàng).
5.1 日志格式,是指記錄哪些選項(xiàng)
默認(rèn)的日志格式: main,記錄這么幾項(xiàng)
遠(yuǎn)程IP--遠(yuǎn)程用戶/用戶時(shí)間--請(qǐng)求方法(如GET/POST)--請(qǐng)求體body長度--referer來源信息
http-user-agent用戶代理/蜘蛛 ,被轉(zhuǎn)發(fā)的請(qǐng)求的原始IP
http_x_forwarded_for:在經(jīng)過代理時(shí),代理把你的本來IP加在此頭信息中,傳輸你的原始IP
5.2:聲明一個(gè)獨(dú)特的log_format并命名
log_format mylog '$remote_addr- "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
在下面的server/location,我們就可以引用 mylog
在server段中,這樣來聲明:
access_log logs/access_8080.log mylog;
//聲明log log位置 log格式;
Nginx允許針對(duì)不同的server做不同的Log ,(有的web服務(wù)器不支持,如lighttp)
5.3 實(shí)際應(yīng)用:
shell+定時(shí)任務(wù)+nginx信號(hào)管理,完成日志按日期存儲(chǔ)
分析思路: 凌晨00:00:01,把昨天的日志重命名,放在相應(yīng)的目錄下;再USR1信息號(hào)控制nginx重新生成新的日志文件
腳本具體內(nèi)容:
#!/bin/bash
base_path='/usr/local/nginx/logs'
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p $base_path/$log_path
mv $base_path/access.log $base_path/$log_path/access_$day.log
#echo $base_path/$log_path/access_$day.log 【查看】
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
部分命令演示:
linux nginx># date
linux nginx># date -d yesterday
linux nginx># date -d yesterday +%Y%m%d%H%M
linux nginx># mkdir /data 【創(chuàng)建一個(gè)目錄】
linux nginx># cd /data
linux data># vim runlog.sh
#!/bin/bash
echo $(date -d yesterday +%Y%m%d) 【可以用$ 或者 反引號(hào)` 輸出命令的值】
linux data># sh runlog.sh 【執(zhí)行這個(gè)腳本】
#!/bin/bash【全版】
base_path='/usr/local/nginx/logs' 【聲明一個(gè)變量】
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p $base_path/$log_path 【創(chuàng)建文件夾】
mv $base_path/access.log $base_path/$log_path/access_$day.log
#echo $base_path/$log_path/access_$day.log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
#!/bin/bash【簡版】
LOGPATH=/usr/local/nginx/logs/host.access.log
BASEPATH=/data
bak=$BASEPATH/$(date -d yesterday +%Y%m%d%H%M).host.access.log
// echo $bak 【看一下輸出】
mv $LOGPATH $bak 【移動(dòng)日志到備份目錄】
touch $LOGPATH 【重新創(chuàng)建logpath】
kill -USR1 `/usr/local/nginx/logs/nginx.pid` 【通知一下nginx】
linux data> crontab -e
*/1 * * * * sh /data/runlog.sh 【添加定時(shí)任務(wù),讓runlog.sh每分鐘執(zhí)行一次】
【分時(shí)日月周】
2 0 */1 * * sh /data/runlogday.sh
6 location語法
6.1 location語法
location 有”定位”的意思, 根據(jù)Uri來進(jìn)行不同的定位.
在虛擬主機(jī)的配置中,是必不可少的,location可以把網(wǎng)站的不同部分,定位到不同的處理方式上.
比如, 碰到.php, 如何調(diào)用PHP解釋器? --這時(shí)就需要location
location 的語法
location [=|~|~*|^~] patt {
}
中括號(hào)可以不寫任何參數(shù),此時(shí)稱為一般匹配
也可以寫參數(shù)
因此,大類型可以分為3種
location = patt {} [精準(zhǔn)匹配]
location patt{} [一般匹配]
location ~ patt{} [正則匹配]
6.2 精準(zhǔn)匹配和一般匹配結(jié)合使用
如何發(fā)揮作用?:
首先看有沒有精準(zhǔn)匹配,如果有,則停止匹配過程.
location = patt {
config A
}
如果 $uri == patt,匹配成功,使用configA
//精準(zhǔn)匹配
location = / {
root /var/www/html/;
index index.htm index.html;
}
//一般匹配
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
如果訪問 http://xxx.com/
定位流程是
1: 精準(zhǔn)匹配中 ”/” ,得到index頁為 index.htm
2: 再次訪問 /index.htm , 此次內(nèi)部轉(zhuǎn)跳uri已經(jīng)是”/index.htm” ,
根目錄為/usr/local/nginx/html
3: 最終結(jié)果,訪問了 /usr/local/nginx/html/index.htm
6.2 一般匹配和正則匹配結(jié)合使用
再來看,正則也來參與.
//一般匹配
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
//正則匹配
location ~ image {
root /var/www/image;
index index.html;
}
如果我們?cè)L問 http://xx.com/image/logo.png
此時(shí), “/” 與”/image/logo.png” 匹配
同時(shí),”image”正則 與”image/logo.png”也能匹配,誰發(fā)揮作用?
正則表達(dá)式的成果將會(huì)使用.
圖片真正會(huì)訪問 /var/www/image/image/logo.png
6.3 兩個(gè)一般匹配【訪問長的】
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
location /foo {
root /var/www/html;
index index.html;
}
我們?cè)L問 http://xxx.com/foo
對(duì)于uri “/foo”, 兩個(gè)location的patt,都能匹配他們
即 ‘/’能從左前綴匹配 ‘/foo’, ‘/foo’也能左前綴匹配’/foo’,
此時(shí), 真正訪問 /var/www/html/index.html
原因:’/foo’匹配的更長,因此使用之.;
7 rewrite重寫【在location內(nèi)部使用】
7.1重寫中用到的指令
if (條件) {} 設(shè)定條件,再進(jìn)行重寫
set #設(shè)置變量
return #返回狀態(tài)碼
break #跳出rewrite
rewrite #重寫
7.2If 語法格式
if 空格 (條件) {
重寫模式
}
7.3 條件又怎么寫?【3種寫法】
1: “=”來判斷相等, 用于字符串比較
2: “~” 用正則來匹配(此處的正則區(qū)分大小寫)
“~*” 不區(qū)分大小寫的正則
3: -f -d -e來判斷是否為文件,為目錄,是否存在
7.4 rewrite:例子
if ($remote_addr = 192.168.1.100) {
return 403;
}
if ($http_user_agent ~ MSIE) {
rewrite ^.*$ /ie.htm;
break; #(不break會(huì)循環(huán)重定向)
}
if (!-e $document_root$fastcgi_script_name) {
rewrite ^.*$ /404.html break;
}
注, 此處還要加break;
以 xx.com/dsafsd.html這個(gè)不存在頁面為例,
我們觀察訪問日志, 日志中顯示的訪問路徑,依然是GET /dsafsd.html HTTP/1.1
提示: 服務(wù)器內(nèi)部的rewrite和302跳轉(zhuǎn)不一樣.
跳轉(zhuǎn)的話URL都變了,變成重新http請(qǐng)求404.html, 而內(nèi)部rewrite, 上下文沒變;
跳轉(zhuǎn)的話URL都變了,變成重新http請(qǐng)求404.html, 而內(nèi)部rewrite, 上下文沒變;
就是說 fastcgi_script_name 仍然是 dsafsd.html,因此 會(huì)循環(huán)重定向.
7.5 set:
set是設(shè)置變量用的, 可以用來達(dá)到多條件判斷時(shí)作標(biāo)志用.
達(dá)到apache下的 rewrite_condition的效果
如下: 判斷IE并重寫,且不用break; 我們用set變量來達(dá)到目的
if ($http_user_agent ~* msie) {
set $isie 1;
}
if ($fastcgi_script_name = ie.html) {
set $isie 0;
}
if ($isie 1) {
rewrite ^.*$ ie.html;
}
7.6 Rewrite 正則表達(dá)式 定向后的位置 模式
Goods-3.html ---->Goods.php?goods_id=3
goods-([\d]+)\.html ---> goods.php?goods_id =$1
//例
location /ecshop {
rewrite "goods-(\d{1,7})\.html" /ecshop/goods.php?id=$1;
}
//例
location /ecshop {
index index.php;
rewrite goods-([\d]+)\.html$ /ecshop/goods.php?id=$1;
rewrite article-([\d]+)\.html$ /ecshop/article.php?id=$1;
rewrite category-(\d+)-b(\d+)\.html /ecshop/category.php?id=$1&brand=$2;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5;
rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d+\.])-(\d+)-([^-]+)-([^-]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8;
}
//匹配第四個(gè)rewrite
//category-3-b2-min200-max1700-attr167.227.202.199.html
//category-3-b0-min0-max0-attr0-1goods_id-DESC.html
注意:用url重寫時(shí), 正則里如果有”{}”,正則要用雙引號(hào)包起來
8 nginx+php的編譯
8.1 編譯
apache一般是把php當(dāng)做自己的一個(gè)模塊來啟動(dòng)的.
而nginx則是把http請(qǐng)求變量(如get,user_agent等)轉(zhuǎn)發(fā)給 php進(jìn)程,即php獨(dú)立進(jìn)程,與nginx進(jìn)行通信. 稱為 fastcgi運(yùn)行方式.
因此,為apache所編譯的php,是不能用于nginx的.
注意: 我們編譯的PHP 要有如下功能:
連接mysql, gd, ttf, 以fpm(fascgi)方式運(yùn)行
linux src/php># ./configure -help | grep mysql
linux src/php># ./configure --prefix=/usr/local/fastphp \
--with-mysql=mysqlnd \
--enable-mysqlnd \
--with-gd \
--enable-gd-native-ttf \
--enable-gd-jis-conv
--enable-fpm 【這里所有的都需要】
8.2 nginx中l(wèi)ocation的指向php
編譯完畢后:
1:
nginx+php的配置比較簡單,核心就一句話----
把請(qǐng)求的信息轉(zhuǎn)發(fā)給9000端口的PHP進(jìn)程, 讓PHP進(jìn)程處理 指定目錄下的PHP文件.
如下例子:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
1: 碰到php文件,
2: 把根目錄定位到 html,
3: 把請(qǐng)求上下文轉(zhuǎn)交給9000端口PHP進(jìn)程,
4: 并告訴PHP進(jìn)程,當(dāng)前的腳本是 $document_root$fastcgi_scriptname
(注:PHP會(huì)去找這個(gè)腳本并處理,所以腳本的位置要指對(duì))
10 nginx gzip壓縮提升網(wǎng)站速度【優(yōu)化】
網(wǎng)頁內(nèi)容的壓縮編碼與傳輸速度優(yōu)化
我們觀察news.163.com的頭信息
請(qǐng)求:
Accept-Encoding:gzip,deflate,sdch
響應(yīng):
Content-Encoding:gzip
Content-Length:36093
再把頁面另存下來,觀察,約10W字節(jié),實(shí)際傳輸?shù)?6093字節(jié)
原因---->就在于gzip壓縮上.
原理:
瀏覽器---請(qǐng)求---->聲明可以接受 gzip壓縮 或 deflate壓縮 或compress 或 sdch壓縮
從http協(xié)議的角度看--請(qǐng)求頭,聲明 accept-encoding: gzip deflate sdch (是指壓縮算法,其中sdch是google倡導(dǎo)的一種壓縮方式,目前支持的服務(wù)器尚不多)
服務(wù)器-->回應(yīng)---把內(nèi)容用gzip方式壓縮---->發(fā)給瀏覽器
瀏覽<-----解碼gzip-----接收gzip壓縮內(nèi)容----
推算一下節(jié)省的帶寬:
假設(shè) news.163.com PV 2億
2*10^8 * 9*10^4 字節(jié) ==
2*10^8 * 9 * 10^4 * 10^-9 = 12*K*G = 18T
節(jié)省的帶寬是非常驚人的
//gzip 相關(guān)參數(shù)設(shè)置
gzip on;
gzip_buffers 32 4K;
zip_comp_level 6;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzio_type text/plain application/xml;
gzip配置的常用參數(shù)
gzip on|off; #是否開啟gzip
gzip_buffers 32 4K| 16 8K #緩沖(壓縮在內(nèi)存中緩沖幾塊? 每塊多大?)
gzip_comp_level [1-9] #推薦6 壓縮級(jí)別(級(jí)別越高,壓的越小,越浪費(fèi)CPU計(jì)算資源)
gzip_disable #正則匹配UA 什么樣的Uri不進(jìn)行g(shù)zip
gzip_min_length 200 # 開始?jí)嚎s的最小長度(再小就不要壓縮了,意義不在)
gzip_http_version 1.0|1.1 # 開始?jí)嚎s的http協(xié)議版本(可以不設(shè)置,目前幾乎全是1.1協(xié)議)
gzip_proxied # 設(shè)置請(qǐng)求者代理服務(wù)器,該如何緩存內(nèi)容
gzip_types text/plain application/xml # 對(duì)哪些類型的文件用壓縮 如txt,xml,html ,css
gzip_vary on|off # 是否傳輸gzip壓縮標(biāo)志
注意:
圖片/mp3這樣的二進(jìn)制文件,不必壓縮
因?yàn)閴嚎s率比較小, 比如100->80字節(jié),而且壓縮也是耗費(fèi)CPU資源的.
gzip 寫在http,server,location,等都行
另外:
nginx/conf/mime.types 有 txt,css等對(duì)應(yīng)mime類型
11 expires緩存【優(yōu)化】
11.1對(duì)于網(wǎng)站的圖片,尤其是新聞?wù)? 圖片一旦發(fā)布, 改動(dòng)的可能是非常小的.我們希望 能否在用戶訪問一次后, 圖片緩存在用戶的瀏覽器端,且時(shí)間比較長的緩存.
可以, 用到nginx的expires設(shè)置.
nginx中設(shè)置過期時(shí)間,非常簡單,
在location或if段里,來寫.
格式 expires 30s;
expires 30m;
expires 2h;
expires 30d;
location ~ image{
root html;
expires 1d;
}
// 圖片交給nginx
location ~* \.(jpg|jpeg|gif|png){
root html;
expires 1d;
}
11.2 304緩存
另: 304 也是一種很好的緩存手段
原理是: 服務(wù)器響應(yīng)文件內(nèi)容是,同時(shí)響應(yīng)etag標(biāo)簽(內(nèi)容的簽名,內(nèi)容一變,他也變), 和 last_modified_since 2個(gè)標(biāo)簽值
瀏覽器下次去請(qǐng)求時(shí),頭信息發(fā)送這兩個(gè)標(biāo)簽, 服務(wù)器檢測文件有沒有發(fā)生變化,如無,直接頭信息返回 etag,last_modified_since
瀏覽器知道內(nèi)容無改變,于是直接調(diào)用本地緩存.
這個(gè)過程,也請(qǐng)求了服務(wù)器,但是傳遞的內(nèi)容極少.
對(duì)于變化周期較短的,如靜態(tài)html,js,css,比較適于用這個(gè)方式
12 nginx反向代理服務(wù)器+負(fù)載均衡
12.1 nginx反向代理實(shí)現(xiàn)nginx+apache動(dòng)靜分離
用nginx做反向代理和負(fù)載均衡非常簡單,
支持兩個(gè)用法 1個(gè)proxy, 1個(gè)upstream,分別用來做反向代理,和負(fù)載均衡
以反向代理為例, nginx不自己處理php的相關(guān)請(qǐng)求,而是把php的相關(guān)請(qǐng)求轉(zhuǎn)發(fā)給apache來處理.
---->客戶端---->nginx.html---->proxy_pass---->apache php;
再依次返回給客戶端
----這不就是傳說的”動(dòng)靜分離”,動(dòng)靜分離不是一個(gè)嚴(yán)謹(jǐn)?shù)恼f法,叫反向代理比較規(guī)范.
location ~ \.php$ {
proxy_pass http://192.168.1.200:8080
}
12.2 nginx負(fù)載均衡
反向代理后端如果有多臺(tái)服務(wù)器,自然可形成負(fù)載均衡,但proxy_pass如何指向多臺(tái)服務(wù)器?
把多臺(tái)服務(wù)器用upstream指定綁定在一起并起個(gè)組名,然后proxy_pass指向該組
默認(rèn)的均衡的算法很簡單,就是針對(duì)后端服務(wù)器的順序,逐個(gè)請(qǐng)求.
也有其他負(fù)載均衡算法,如一致性哈希,需要安裝第3方模塊
upstream imgserver {
server 192.168.1.200:81 weight=1 max_fails=2 fail_timeout=3;
server 192.168.1.200:82 weight=1 max_fails=2 fail_timeout=3;
}
server{
listen 81;
server_name localhost;
root html/image;
access_log logs/81-access.log main;
}
server{
listen 82;
server_name localhost;
root html/image;
access_log logs/82-access.log main;
}
location ~* \.(jpg|jpeg|gif|png){
proxy_pass http://imgserver;
}
//這樣遇到圖片的時(shí)候就傳給了imageServer
反向代理導(dǎo)致了后端服務(wù)器的IP,為前端服務(wù)器的IP,而不是客戶真正的IP,怎么辦?
//添加一個(gè)header,X-Forwarded-For
location ~* \.(jpg|jpeg|gif|png){
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://imgserver;
}
========================================================================================================================
13 nginx連接memcached
13.1
nginx--->memcached---->無緩存----->php—--->DB
//來請(qǐng)求后,先去memcached中查找
location / {
set $ "$uri"
memcached_pass 127.0.0.1:11211;
error_page 404 /callback.php;
}
13.2 Nginx第三方模塊編譯及一致性哈希應(yīng)用
nginx根據(jù)hash($uri)--->某臺(tái)memcached_key
php 根據(jù)hash($uri)--->同一臺(tái)memcached_key
插件:
nginx consistentHash
13.2.1 安裝ngx_http_consistent_hash插件【這可能下不動(dòng),自己下載去】
linux #>cd /usr/local/src
linux #>wget https://github.com/replay/ngx_http_consistent_hash.git
linux #>unzip gx_http_consistent_hash-master
linux #>cd ngx_http_consistent_hash-master
linux #>ls
// cd到nginx的源文件下
linux #>cd /usr/local/src/nginx-1.14.2
linux #>ls src/
linux #> /usr/local/nginx/sbin/nginx -v 【查看原來的nginx是怎么編譯的】
linux #> .configure --help|grep with 【查看help】
【編譯,帶第三方模塊】
linux src/nginx-1.14.2#> ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_http_consistent_hash-master/【帶插件編譯】
linux src/nginx-1.14.2#> pkill -9 nginx【關(guān)掉之前的nginx,如何之前有的話】
linux src/nginx-1.14.2#> make && make install 【安裝】
linux #> 重啟nginx
13.2.2配置memcache集群
upstream memserver { 把用到的memcached節(jié)點(diǎn),聲明在一個(gè)組里
//hash_key $request_uri; // hash計(jì)算時(shí)的依據(jù),以u(píng)ri做依據(jù)來hash
consistent_hash $request_uri;
server 127.0.0.1:11211;
server 127.0.0.1:11212;
}
Location里
location / {
# root html;
set $memcached_key $uri;
memcached_pass memserver; // memserver為上面的memcache節(jié)點(diǎn)的名稱
error_page 404 /writemem.php;
index index.php index.html index.htm;
}
還要修改一下memcached的hash算法
---->php.ini
---->memcache.hash_strategy=consistent
14 大訪問量優(yōu)化整體思路
---詳看nginx集群筆記