Nginx Web caching service with Sina's Open source Ncache module (1)

Source: Internet
Author: User
Tags http post

Nginx Web cache service with Sina's Open source Ncache module what is Web caching

The Web cache is located between the content source Web server and the client, and when the user accesses a URL, the Web cache server goes back to the backend Web source server to retrieve the content to be output, and then, when the next request arrives, if the same Url,web cache server is accessing the direct output to the client, Instead of sending the request again like the source server. The Web cache reduces the load of the content source Web server, the database, reduces the network delay, improves the user's response speed and enhances the user experience.
The most famous is the Squid Cache, which operates primarily on Unix-like systems.

Nginx Web Caching Service

Nginx supports squid-like cache modules from 0.7.48. This cache is the URL and the associated combination as key, using the MD5 algorithm to syrah the key, to get the corresponding Syrah path on the hard disk, so that the cache content stored in the directory. Supports any URL link. Non-200 status codes such as 404/301/302 are also supported.
Nginx Web caching service is mainly used for proxy_cache related instruction set and fastcgi related instruction set, the former is used for reverse proxy, the backend content source is cached, the latter is mainly used to cache FASTCDI dynamic program. Both functions are basically the same.

Proxy_cache Related Instruction Set

1. proxy_cache directive
Syntax: Proxy_cache zone_name;
Default value: None
Usage Environment: http,server,location
This instruction is used to set the buffer that the buffer will be applied to, and the value of Zone_name is created by the proxy_cache_path instruction.
2. proxy_cache_path Directive
Syntax: Proxy_cache_path path[levels=number] keys_zone=zone_name:zone_size[incative=time] [max_size=size];
Default value: None
Usage Environment: HTTP
eg:
Proxy_cache_path/data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;
Note that this directive can only be configured within the HTTP tag, levels specifies that the cache has a two-layer hash directory, the first layer is 1 letters, the second layer is 2 letters, and the file name is similar to/data0/proxy_cache_dir/c/29/ The Fdg35415fg35f4gsdf2g1535gh465h;key_zone parameter is used for the cache name, 500m specifies that the memory size of 500mb;inactive 1d is deleted if the cached data is not accessed within 1 days; max_ A size of 30g means that the hard disk has a cache space of 30GB.
3proxy_cache_methods Directive
Syntax: proxy_cache_methods [GET HEAD POST];
Default value: Proxy_cache_methods GET HEAD;
Usage Environment: http,server,location
This directive is used to set the HTTP Get/head method that is used to cache those HTTP methods, the default cache, and does not cache the HTTP POST method.
4proxy_cache_min_uses Directive
Syntax: Proxy_cache_min_uses the_number;
Default value: Proxy_cache_min_uses 1;
Usage Environment: http,server,location
This instruction sets the minimum number of uses for the cache, and the default value is 1.
5. proxy_cache_valid Directive
Syntax: Proxy_cache_valid reply_code [Reply_code ...] Time
Default value: None
Usage Environment: http,server,location
This directive is used to set different cache times for URLs of different return status codes, for example:
Proxy_cache_valid 302 10m;
Proxy_cache_valid 404 1m;
If you do not specify a state, directly specify the time, then only the 200, 301, 302-state URLs are cached for 5 minutes.
6. proxy_cache_key Directive
Syntax: Proxy_cache_key line;
Default value: None
Usage Environment: http,server,location
This instruction is used to set the Web cache key value, Nginx MD5 Syrah storage cache according to the key value. Generally based on‘$host(域名)、$request_uri(请求路径)’Composite variables such as Proxy_cache_key. For example:proxy_cache_key "$host:$server_port$uri$is_args$args";

Proxy_cache Complete Example
suyum -y install pcre//安装pcrewget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gztar zxvf ngx_cache_purge-2.3.tar.gz//获取nginx_cache_purgecd 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

Configure 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  1024x768;}http{includeMime.types;Default_typeApplication/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  -;client_header_buffer_size 32k;large_client_header_buffers 4 32k;Sendfile         on;#tcp_nopush on;    Keepalive_timeout   -;tcp_nodely   on;Proxy_temp_path/data0/proxy_temp_path;Proxy_temp_path/data0/proxy_temp_path levels=1:2Key_zone=cache_one:200mInactive=1dMax_size=30g;Upstreammy_sever_pool{Server 192.168.1.2:80weight=1max_fails=2fail_timeout=101;Server 192.168.1.3:80weight=1max_fails=2fail_timeout=30s;Server 192.168.1.4:80weight=1max_fails=2fail_timeout=101; }#gzip on;    Server{Listen        the;server_namelocalhost#charset koi8-r;        #access_log Logs/host.access.log main;         Location/ {Proxy_set_headerHost$host;Proxy_set_headerX-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_cacheCache_one;#对不同状态码设置不同缓存时间        Proxy_cache_valid  $ 304 12h;Proxy_cache_valid 301 302 1m;Proxy_cache_validany IM;#设置web缓存的key值, Nginx MD5 Syrah storage cache according to the key value, which is combined into key according to "Domain name/url parameter".         Proxy_cache_key $host$uri$is _args$args;#反向代理, accessing back-end content source Servers        Proxy_set_headerHost$host;Proxy_set_headerX-forwarded-for$remote _addr;Proxy_passHttp:my_server_pool; }#用于清除缓存, assume that a URL of http://my.domain.com/text.gif by accessing Http://my.domain.com/purge/test.gif can clear the Urk cache.      Location ~/purge (/.*){#设定只允许指定的IP或IP段才可以清除URL缓存.          Allow       127.0.0.1Allow192.168.0.0/ -;DenyAllProxy_cache_purgeCache_one$shot $$is-args$args; }Access_log0ff#error_page 404/404.html;        # REDIRECT Server error pages to the static page/50x.html        #Error_page - 502 503 504/50x.html; Location=/50x.html {Roothtml }# 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;    #    }    #}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nginx Web caching service with Sina's Open source Ncache module (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.