第四節 Nginx緩存服務器

 用戶體驗php

•  頁面打開速度
•  良好的界面體驗
•  高質量量的內容
•  個性化內容推送html

 

頁面打開速度
•  帶寬
•  服務器器的響應速度
•  傳輸距離
•  網絡延遲nginx

 

 

 

 

 

 

 

Nginx安裝?
•  Nginx包得到(http://nginx.org)
http://nginx.org/download/nginx-1.15.8.tar.gz
•  緩存模塊包下載(http://labs.frickle.com/files/)
http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
•  安裝Nginxweb

Nginx緩存部署 
•  開啓緩存
•  緩存一個網站緩存

 

安裝Nginx服務器

nginx源碼包
[root@web01 ~]# wget http://nginx.org/download/nginx-1.15.8.tar.gz
•  nginx緩存模塊
[root@web01 ~]# wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
•  新建nginx管理理帳號
[root@web01 ~]# useradd -r www -s /sbin/nologin
•  nginx安裝---解壓源碼包
[root@web01 ~]# tar xf nginx-1.15.8.tar.gz
[root@web01 ~]# tar xf ngx_cache_purge-2.3.tar.gz
•  nginx安裝---安裝依賴
[root@web01 ~]# yum -y install pcre-* openssl-*
•  nginx安裝---配置nginx
[root@web01 ~]# cd nginx-1.15.8
[root@web01 nginx-1.15.8]# ./configure --prefix=/usr/local/nginx --user=www --group=www --add-module=../ngx_cache_purge-2.3 --with-http_stub_status_module
•  nginx安裝---編譯
[root@web01 nginx-1.15.8]# make –j4
•  nginx安裝---安裝
[root@web01 nginx-1.15.8]# make install網絡

chown www.www nginx -R網站

 修改啓動Nginx的用戶 spa

進入config3d

修改noboby爲 www

 

 

開啓緩存
在http配置中添加
1)設置緩存臨時路路徑
proxy_temp_path /cache/proxy_temp_dir;
2)緩存路路徑及開啓
proxy_cache_path /cache/proxy_dir levels=1:2 keys_zone=cache0:10m inactive=1d max_size=30g;
#levels=1:2 緩存目錄分爲兩級 第一級⽂文件夾命名用一個字母 第級文件夾用兩個字母命名,最多三級。
#keys_zone=cache0:10m 內存緩存區域10M 名字爲cache0。在共享內存中設置一塊存儲區域來存放緩存的key
和metadata,這樣nginx能夠快速判斷一個request是否命中或者未命中緩存,1m能夠存儲8000個key,10m能夠存儲80000個key
#inactive=1d 有效期爲1天,若是緩存內容在一天中沒人訪問則被刪除
#max_size=30g 硬盤空間中的30G作磁盤緩存,若是不不指定,會使用掉全部disk space,當達到配額後,會刪除最少使用的cache文件

 

建立文件夾

mkdir -p  /cache/proxy_temp_dir

chown www.www /cache -R

 

location / {
  proxy_pass http://www.ayitula.com;
  proxy_cache cache0;
  proxy_cache_valid 200 304 30m;
  proxy_cache_valid any 1m;
  proxy_cache_key $host$uri$is_args$args;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $remote_addr;
  expires 60m;
}

 修改config文件

保存退出

打開http://192.168.0.143/ 頁面展現爲 

 

若是不展現則關閉防火牆

 

 

 

 

 

location ~/purge(/.*)
{
allow 127.0.0.1;
allow 192.168.11.0/24;
proxy_cache_purge cache0 $host$1$is_args$args;
}

清理緩存

 

 

 

 

Nginx鏡像服務器

 

開啓緩存
#本地目錄
root /usr/local/nginx/html;
index index.html index.php;
proxy_store on; #開啓鏡像服務
proxy_store_access user:rw group:rw all:r; #緩存本地存儲⽂文件的權限
proxy_temp_path /usr/local/nginx/html/temp; #本地臨時緩存⽬目錄
#判斷本地⽬目錄中是否有⽂文件,沒有就去取源
if ( !-e $request_filename ) {
proxy_pass http://download.ayitula.com;
}

編輯config文件

 

保存退出啓動 

 

相關文章
相關標籤/搜索