Nginx+Proxy_cache高速緩存配置
前言
Nginx已經具備Squid所擁有的Web緩存加速功能、清除指定URL緩存的功能。而在性能上,Nginx對多核CPU的利用,勝過Squid不少。另外,在反向代理、負載均衡、健康檢查、后端服務器故障轉移、Rewrite重寫、易用性上,Nginx也比Squid強大得多。這使得一臺Nginx可以同時作為“負載均衡服務器”與“Web緩存服務器”來使用。
一、 安裝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 { \\添加以下內容 ,不能定義在server{}上下文中 }
.......
#定義從后端服務器接收的臨時文件的存放路徑
proxy_temp_path /data/proxy_temp_dir;
#設置Web緩存區(qū)名稱cache_one,內存緩存空間100MB,1天沒有被訪問的內容自動清除,硬盤緩存空間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 /
{
#如果后端的服務器返回502、504、執(zhí)行超時等錯誤,自動將請求轉發(fā)到upstream負載均衡池中的另一臺服務器,實現(xiàn)故障轉移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#對不同的HTTP狀態(tài)碼設置不同的緩存時間
proxy_cache_valid 200 304 2h;
#以域名、URI、參數(shù)組合成Web緩存的Key值,Nginx根據(jù)Key值哈希,存儲緩存內容到二級緩存目錄內
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(/.*)
{
#設置只允許指定的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服務,/usr/local/nginx/sbin/nginx
#然后配置好resin端口設置為8080
#如果需要刷新緩存的url地址為: http://10.1.1.10/purge/
四、如何清除緩存:
清除緩存有兩種方法,第一種是直接通過nginx.conf配置文件定義的/purge虛擬目錄去清除,第二種方法可以通過shell腳本去批量清除:
附上Shell腳本清空緩存的內容:
#! /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,對輸入的文件進行換行處理,利于grep查找匹配相關內容
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到此這篇關于Nginx+Proxy_cache高速緩存配置的文章就介紹到這了,更多相關Nginx Proxy_cache緩存配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
內網(wǎng)環(huán)境nginx配置https訪問的過程詳解
這篇文章主要介紹了內網(wǎng)環(huán)境nginx配置https訪問,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
Nginx反向代理location和proxy_pass配置規(guī)則詳細總結
nginx代理訪問很好用,但是好多人不清楚location和proxy_pass組合在一起使用時訪問的url被代理的url真實地址是什么,下面這篇文章主要給大家介紹了關于Nginx反向代理location和proxy_pass配置規(guī)則的相關資料,需要的朋友可以參考下2022-09-09

