Nginx+Proxy_cache高速緩存配置
前言
Nginx已經(jīng)具備Squid所擁有的Web緩存加速功能、清除指定URL緩存的功能。而在性能上,Nginx對多核CPU的利用,勝過Squid不少。另外,在反向代理、負(fù)載均衡、健康檢查、后端服務(wù)器故障轉(zhuǎn)移、Rewrite重寫、易用性上,Nginx也比Squid強大得多。這使得一臺Nginx可以同時作為“負(fù)載均衡服務(wù)器”與“Web緩存服務(wù)器”來使用。
一、 安裝nginx和ngx-purge:
ulimit -SHn 65535 cd /usr/local/nginx tar zxvf ngx_cache_purge-1.4.tar.gz cd nginx-1.6.1/ ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=../ngx_cache_purge-1.4 make && make install cd ../
二、 Nginx Cache配置:
http { \\添加以下內(nèi)容 ,不能定義在server{}上下文中 }
.......
#定義從后端服務(wù)器接收的臨時文件的存放路徑
proxy_temp_path /data/proxy_temp_dir;
#設(shè)置Web緩存區(qū)名稱cache_one,內(nèi)存緩存空間100MB,1天沒有被訪問的內(nèi)容自動清除,硬盤緩存空間10GB。
proxy_cache_path /nginx/cache/first levels=1:2:1 keys_zone=cache_one:100m inactive=1d max_size=10g;
upstream backend_server {
server 10.1.1.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 10.1.1.2:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name localhost;
index index.html index.htm;
root html;
location /
{
#如果后端的服務(wù)器返回502、504、執(zhí)行超時等錯誤,自動將請求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺服務(wù)器,實現(xiàn)故障轉(zhuǎn)移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#對不同的HTTP狀態(tài)碼設(shè)置不同的緩存時間
proxy_cache_valid 200 304 2h;
#以域名、URI、參數(shù)組合成Web緩存的Key值,Nginx根據(jù)Key值哈希,存儲緩存內(nèi)容到二級緩存目錄內(nèi)
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1;
expires 1d;
}
location ~ /purge(/.*)
{
#設(shè)置只允許指定的IP或IP段輸入正確的密碼才可以清除URL緩存。
auth_basic “Please Insert User And Password”;
auth_basic_user_file /tmp/htpasswd;
allow 127.0.0.1;
allow 10.1.1.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
........三、ginx Cache測試:
#啟動Nginx服務(wù),/usr/local/nginx/sbin/nginx
#然后配置好resin端口設(shè)置為8080
#如果需要刷新緩存的url地址為: http://10.1.1.10/purge/
四、如何清除緩存:
清除緩存有兩種方法,第一種是直接通過nginx.conf配置文件定義的/purge虛擬目錄去清除,第二種方法可以通過shell腳本去批量清除:
附上Shell腳本清空緩存的內(nèi)容:
#! /bin/sh
#Auto Clean Nginx Cache Shell Scripts
#2013-06-12 wugk
#Define Path
CACHE_DIR=/data/www/proxy_cache_dir/
FILE="$*"
#To determine whether the input script,If not, then exit 判斷腳本是否有輸入,沒有輸入然后退出
if
[ "$#" -eq "0" ];then
echo "Please Insert clean Nginx cache File, Example: $0 index.html index.js"
sleep 2 && exit
fi
echo "The file : $FILE to be clean nginx Cache ,please waiting ....."
#Wrap processing for the input file, for grep lookup,對輸入的文件進(jìn)行換行處理,利于grep查找匹配相關(guān)內(nèi)容
for i in `echo $FILE |sed 's//\n/g'`
do
grep -ra $i ${CACHE_DIR}| awk -F':' '{print $1}' > /tmp/cache_list.txt
for j in `cat/tmp/cache_list.txt`
do
rm -rf $j
echo "$i $j is Deleted Success !"
done
done到此這篇關(guān)于Nginx+Proxy_cache高速緩存配置的文章就介紹到這了,更多相關(guān)Nginx Proxy_cache緩存配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx 504 Gateway Time-out錯誤解決方法
我們經(jīng)常會發(fā)現(xiàn)大量的nginx服務(wù)器訪問時會提示nginx 504 Gateway Time-out錯誤了,下面我來總結(jié)了一些解決辦法,有需要了解的同學(xué)可進(jìn)入?yún)⒖?/div> 2014-11-11
NGINX基于cookie針對同一域名進(jìn)行分流轉(zhuǎn)發(fā)
本文介紹了利用NGINX基于cookie進(jìn)行多環(huán)境分流的方法,通過在Docker中部署兩個后端NGINX容器,并在前端NGINX配置中設(shè)置map規(guī)則,根據(jù)cookie值將請求分發(fā)到不同后端,感興趣的可以了解一下2025-07-07
centos環(huán)境下nginx高可用集群的搭建指南
為了防止Nginx單點故障造成服務(wù)器癱瘓,本文介紹了Nginx實現(xiàn)高可用集群構(gòu)建,下面這篇文章主要給大家介紹了關(guān)于centos環(huán)境下nginx高可用集群的搭建指南,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07最新評論

