캐 시 를 인식 하 는 서버 캐 시 (Nginx)

앞에서 소개 한 아파 치 캐 시 든 지금 소개 할 Nginx 캐 시 든 사실은 squid 를 참고 했다.웹 서버 가 발전 함 에 따라 웹 서버 의 캐 시 기능 은 점점 강해 지고 있 습 니 다. 웹 서버 를 사용 하 는 캐 시 기능 을 설정 함으로써 저 희 는 squid 를 사용 할 필요 가 없 을 때 가 많 습 니 다.다음은 Nginx 의 proxycache 모듈 소개.
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;
  }
}

   캐 시 부분 설정 에 대한 자세 한 설명:
  • proxy_temp_path 와 proxycache_path 가 지정 한 경 로 는 같은 구역 에 있어 야 합 니 다
  • proxy_cache_path: 이 설정 은 서버 탭 외 에 levels 에서 이 캐 시 공간 에 HASH 디 렉 터 리 2 층, 1 층 1 자, 2 층 2 자가 있 습 니 다. 저 장 된 파일 이름 은 / data / proxy 와 유사 합 니 다.cache_dir/c/29/asdf777asdf7as6;key_zone 은 이 공간 에 이름 을 짓 고 200 m 는 공간 크기 가 200 M 이라는 것 을 말 합 니 다.inactive 의 1d 는 캐 시 기본 시간 이 1 일 이라는 것 을 말 합 니 다.max_size 의 2m 는 하나의 파일 이 2m 를 넘 으 면 캐 시 되 지 않 는 다 는 것 을 말한다.clean_time 지정 1 분 에 한 번 캐 시 를 청소 합 니 다

  • 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

    좋은 웹페이지 즐겨찾기