캐 시 를 인식 하 는 서버 캐 시 (Nginx)
2. Nginx 캐 시
1. 의존 구성 요소
Nginx - 0.7.44 버 전부터 Nginx 는 Squid 와 유사 한 정규 캐 시 기능 을 지원 하기 시 작 했 습 니 다. 이 캐 시 는 링크 를 md5 인 코딩 으로 하 쉬 를 거 쳐 저장 하고 임의의 링크 를 지원 합 니 다.404 / 301 / 302 와 같은 비 200 상 태 를 동시에 지원 합 니 다.Nginx 에 서 는 proxy 기반 입 니 다.cache 구현 캐 시 기능.
2, Nginx 설치 ngxcache 확장
먼저 Nginx 캐 시 모듈, ngx 다운로드cache_Purge 해당 버 전 은 nginx - 1.4 버 전 을 다운로드 하고 버 전 마다 Nginx 버 전에 대응 하 며 설치 할 때 주의해 야 합 니 다.
ulimit -SHn 65535
yum install pcre pcre-devel -y
wget http://nginx.org/download/nginx-1.0.11.tar.gz
http://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz
tar zxvf ngx_cache_purge-1.4.tar.gz
tar zxvf nginx-1.0.11.tar.gz
useradd www
cd nginx-1.0.11/
./configure --user=www--group=www--add-module=../ngx_cache_purge-1.4 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
메모: 이것 은 nginx 서버 를 설치 할 때 컴 파일 과정 에서 추가 해 야 합 니 다.나중에 설치 가 필요 하 다 면 nginx 서버 를 다시 컴 파일 하고 플러그 인 을 컴 파일 해 야 합 니 다.
3. nginx cache 설정
user nobody nobody;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $remote_addr';
proxy_temp_path /data/proxy_temp_dir;
proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=2m clean_time=1m;
upstream backend_server {
server 127.0.0.1:8800 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8801 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name localhost;
index index.html index.htm;
root /data/webapps/www;
location /
{
# 502、504、 , upstream
# , 。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
# cache_one keys_zone
proxy_cache cache_one;
#200 302 1
proxy_cache_valid 200 302 1h;
# 、URI、 web key ,nginx 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://backend_server;
add_header X-Cache '$upstream_cache_status from $host';
expires 1d;
}
location ~ /purge(/.*)
{
# IP IP URL
auth_basic "Please Insert User And Password";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
# IP IP URL
allow 127.0.0.1;
allow 192.168.5.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
# php、jsp、cgi
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
access_log /usr/local/nginx/logs/access.log main;
}
}
캐 시 부분 설정 에 대한 자세 한 설명:
4. 캐 시 지우 기
캐 시 를 지 우 는 데 는 두 가지 방법 이 있 습 니 다. 첫 번 째 는 nginx. conf 설정 파일 로 정 의 된 / puge 가상 디 렉 터 리 를 직접 지 우 는 것 입 니 다. 두 번 째 방법 은 셸 스 크 립 트 를 통 해 대량으로 알 수 있 습 니 다.다음은 셸 스 크 립 트 가 캐 시 를 비 우 는 내용 입 니 다.
#! /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//
/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