Nginx 의 웹 캐 시 서비스 와 시 나 닷 컴 의 오픈 소스 NCACHE 모듈 (1)

7840 단어 nginxWeb캐 시
Nginx 의 웹 캐 시 서비스 와 시 나 닷 컴 의 오픈 소스 NCACHE 모듈
웹 캐 시
웹 캐 시 는 내 용 원 웹 서버 와 클 라 이언 트 사이 에 있 습 니 다. 사용자 가 URL 에 접근 할 때 웹 캐 시 서버 는 백 엔 드 웹 소스 서버 로 돌아 가 출력 할 내용 을 되 찾 은 다음 다음 요청 이 올 때 같은 URL 에 접근 하면 웹 캐 시 서버 는 원본 서버 가 아 닌 클 라 이언 트 에 게 직접 출력 합 니 다.웹 캐 시 는 내 용 원 웹 서버, 데이터 베이스 의 부 하 를 낮 추고 네트워크 지연 을 줄 이 며 사용자 의 응답 속 도 를 향상 시 켜 사용자 체험 을 강화 합 니 다.가장 유명한 것 은 Squid Cache 인 데 주로 유 닉 스 시스템 에서 실 행 됩 니 다.
Nginx 웹 캐 시 서비스
Nginx 는 0.7.48 이후 Squid 와 유사 한 캐 시 모듈 을 지원 합 니 다.이 캐 시 는 URL 및 관련 조합 을 key 로 하고 md5 알고리즘 으로 key 를 희 하 하여 하 드 디스크 에 대응 하 는 희 하 경 로 를 얻어 캐 시 내용 을 이 디 렉 터 리 에 저장 합 니 다.임의의 URL 링크 를 지원 합 니 다.404 / 301 / 302 와 같은 비 200 상태 코드 도 지원 합 니 다.Nginx 의 웹 캐 시 서 비 스 는 주로 proxy 에 사 용 됩 니 다.cache 관련 명령 집합 과 fastcgi 관련 명령 집합 으로 구성 되 어 있 습 니 다. 전 자 는 역방향 에이전트 에 사용 할 때 백 엔 드 내 용 원 을 캐 시 합 니 다. 후 자 는 주로 FastCDI 의 동적 프로그램 을 캐 시 하 는 데 사 용 됩 니 다.양자 의 기능 은 기본적으로 같다.
proxy_cache 관련 명령 집합
1、proxy_cache 명령 어 문법: proxycache zone_name; 기본 값: none 사용 환경: http, server, location 이 명령 은 캐 시 영역 을 설정 하 는 데 사 용 됩 니 다. zonename 의 값 은 proxycache_path 명령 으로 만 든 캐 시 영역 이름 입 니 다.2、proxy_cache_path 명령 어 문법: proxycache_path path[levels=number] keys_zone=zone_name:zone_size[incative=time] [max_size=size]; 기본 값: none 사용 환경: HTTP eg: proxycache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one: 500m inactive=1d max_size=30g; 이 명령 은 http 탭 에 만 설정 할 수 있 습 니 다. levels 는 이 캐 시 에 2 층 hash 디 렉 터 리 가 있 습 니 다. 1 층 은 1 글자 이 고 2 층 은 2 글자 입 니 다. 파일 이름 은 / data0 / proxy 와 유사 합 니 다.cache_dir/c/29/fdg35415fg35f4gsdf2g1535gh465h;key_zone 매개 변 수 는 캐 시 영역 이름 을 짓 는 데 사 용 됩 니 다. 500 m 에서 지정 한 메모리 공간 크기 는 500 MB 입 니 다.inactive 의 1d 는 캐 시 데이터 가 하루 안에 접근 하지 않 으 면 삭 제 됩 니 다.max_size 의 30g 은 하드디스크 의 캐 시 공간 이 30GB 인 것 을 말한다.3proxy_cache_methods 명령 어 문법: proxycache_methods [GET HEAD POST]; 기본 값: proxycache_methods GET HEAD; 사용 환경: http, server, location 이 명령 은 HTTP 방법 을 캐 시 하 는 데 사 용 됩 니 다. 기본 캐 시 HTTP GET / HEAD 방법, HTTP POST 방법 을 캐 시 하지 않 습 니 다.4proxy_cache_min_uses 명령 어 문법: proxycache_min_uses the_number; 기본 값: proxycache_min_uses 1; 사용 환경: http, server, location 이 명령 은 캐 시 최소 사용 횟수 를 설정 합 니 다. 기본 값 은 1. 5, proxy 입 니 다.cache_유효한 명령 어 문법: proxycache_valid reply_code [reply_code…]time; 기본 값: none 사용 환경: http, server, location 이 명령 은 상태 코드 를 되 돌려 주 는 URL 에 대해 서로 다른 캐 시 시간 을 설정 하 는 데 사 용 됩 니 다. 예 를 들 어 proxycache_valid 200 302 10m; proxy_cache_valid 404 1m; 상 태 를 지정 하지 않 고 시간 을 직접 지정 하면 200, 301, 302 상태의 URL 캐 시 5 분 밖 에 안 됩 니 다.6、proxy_cache_key 명령 문법: proxycache_key line; 기본 값: none 사용 환경: http, server, location 이 명령 은 웹 캐 시 키 값 을 설정 하 는 데 사 용 됩 니 다. Nginx 는 key 값 md5 히 하 에 따라 캐 시 를 저장 합 니 다.일반적으로 ‘$host( )、$request_uri( )’ 등 조합 변수 에 따라 proxy 를 합성 합 니 다.cache_key. 예 를 들 어: proxy_cache_key "$host:$server_port$uri$is_args$args";proxy_cache 전체 예제
  su
yum -y install pcre//  pcre
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz//  nginx_cache_purge
cd nginx-1.6.3//    nginx    (nginx          )
 ./configure --user=www --group=www --addmodule=../ngx_cache_purge-2.3 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module

nginx.conf
cd /usr/local/webserver/nginx/conf

  #user  www www;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    #charset utf-8;

    server_name_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  30;

    tcp_nodely  on;

    proxy_temp_path /data0/proxy_temp_path;

    proxy_temp_path /data0/proxy_temp_path levels=1:2 key_zone=cache_one:200m inactive=1d max_size=30g;
    upstream my_sever_pool{
        server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s;

    }



    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_pass http://my_server_pool;
       # root   html;
            #index  index.html index.htm;
        }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
    {
        #  web   cache_one
        proxy_cache cache_one;

        #              
        proxy_cache_valid 200 304 12h;
        proxy_cache_valid 301 302 1m;
        proxy_cache_valid any im;
        #  web   key ,nginx  key md5      ,    “  /URL   ”   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:my_server_pool;
    }
    #      ,    URL http://my.domain.com/text.gif    http://my.domain.com/purge/test.gif     URK  。
    location ~ /purge(/.*)
    {
        #        IP IP      URL  。
        allow       127.0.0.1
        allow       192.168.0.0/16;
        deny        all;
        proxy_cache_purge cache_one $shot$1$is-args$args;
    }
    access_log 0ff

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
:zhangli401X 2015/8/8 22:20:42
:60 :0

좋은 웹페이지 즐겨찾기