Nginx Web cache service and xinlang open source NCACHE module

Source: Internet
Author: User
: This article mainly introduces Nginx's Web cache service and xinlang's open-source NCACHE module. For more information about PHP tutorials, see.
# Nginx Web cache service and xinlang open source NCACHE Module # What is web cache? the Web cache is located between the content source web server and the client. when a user accesses a URL, the web cache server returns to the backend web source server to retrieve the content to be output. when the next request arrives, if the same URL is accessed, the web cache server directly outputs the content to the client, instead of sending a request again like the source server. Web cache reduces the load on content source web servers and databases, reduces network latency, increases user response speed, and enhances user experience. The most famous is Squid Cache, which is mainly used in Unix systems. # Nginx Web cache service Nginx supports cache modules similar to Squid after 0.7.48. This cache uses the URL and Related combinations as the key and uses the md5 algorithm to obtain the corresponding hiha path on the hard disk and save the cached content in this directory. Any URL link is supported. It also supports non-404/301 status codes such as 302/200. Nginx Web cache service is mainly used for proxy_cache-related instruction sets and fastcgi-related instruction sets. when the former is used for reverse proxy, it caches the back-end content source, the latter is mainly used to cache FastCDI dynamic programs. The two functions are basically the same. ### Proxy_cache instruction set ** 1. proxy_cache command ** syntax: proxy_cache zone_name; default value: none environment: http, server, location this command is used to set the cache area to be applied. The value of zone_name is the name of the cache area created by the proxy_cache_path command. ** 2. proxy_cache_path command ** syntax: proxy_cache_path [levels = number] keys_z [max_size = size]; default value: none environment: HTTP ** eg: ** proxy_cache_path/data0/proxy_cache_dir levels = keys_z> _ one: 500 m inactive = 1d max_size = 30g. Note that this command can only be configured in the http label, levels specifies that the cache contains two levels of hash directories. The first layer is 1 letter and the second layer is 2 letters. the storage file name is similar to/data0/proxy_cache_dir/c/29/fdg35415fg35f4gsdf2g1535gh1_h; the key_zone parameter is used to name the cache area. The memory size of 500 MB is specified for MB. inactive 1d is deleted if the cache data is not accessed within one day. m The 30 GB of ax_size indicates that the cache space of the hard disk is 30 GB. ** 3proxy_cache_methods command ** syntax: proxy_cache_methods [get head post]; default value: proxy_cache_methods get head; environment: http, server, location this command is used to set the HTTP methods used for caching, the http get/HEAD method is cached by default, and the http post method is not cached. ** 4proxy_cache_min_uses command ** syntax: proxy_cache_min_uses the_number; default value: proxy_cache_min_uses 1; environment: http, server, location this command sets the minimum cache usage. the default value is 1. ** 5. proxy_cache_valid command ** syntax: proxy_cache_valid reply_code [reply_code...] time; default value: none environment: http, server, location this command is used to set different cache times for URLs of different return status codes, for example: proxy_cache_valid 200 302 10 m; proxy_cache_valid 404 1 m; if you do not specify the status, directly specify the time, then only 200, 301, 302 URL cache for 5 minutes. ** 6. Syntax: proxy_cache_key line. default value: none environment: http, server, location. This command is used to set the key value of the web cache, nginx stores the cache based on the key value md5. Generally, proxy_cache_key is synthesized based on the combination variables ''$ host (domain name) and $ request_uri (request path. example: 'proxy _ cache_key "$ host: $ server_port $ uri $ is_args $ args"; '# proxy_cache complete example suyum-y install pcre // install pcrewget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gztar Zxvf ngx_cache_purge-2.3.tar.gz // get nginx_cache_purgmcm nginx-1.6.3 // enter your nginx file directory (for nginx installation, refer to the previous blog ). /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; 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 $ response "$ http_referer"' # '"$ http_user_agent" "$ http_x_forwarded_for "'; # access_log logs/access. log main; # charset UTF-8; bytes 128; bytes 32 k; Bytes 4 32 k; 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 = key_z inactive = 1d max_size = 30g; upstream restart {server 192.168.1.2: 80 weight = 1 max_fails = 2 fail_timeout = 30 s; server 192.168.1.3: 80 weight = 1 max_fails = 2 fail_timeout = 30 s; server 192.168.1.4: 80 weight = 1 max_fails = 2 fail_timeout = 30 s;} # 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) $ {# use cache_oneproxy_cache cache_one in the web cache; # set different cache times for different status codes proxy_cache_valid 200 304 12 h; proxy_cache_valid 301 302 1 m; proxy_cache_valid any im; # set the web cache key value, nginx stores the cache based on the key value md5 hiha. here, the cache is combined into a key based on "domain name/URL parameters. Response $ host $ uri $ is_args $ args; # reverse proxy, access the backend content source server proxy_set_header Host $ host; proxy_set_header X-Forwarded-For $ remote_addr; proxy_pass http: my_server_pool ;} # used to clear the cache. assume that a URL is http://my.domain.com/text.gif Access http://my.domain.com/purge/test.gif You can clear the URK cache. Location ~ /Purge (/. *) {# sets only the specified IP address or IP segment to clear the URL cache. Allow 127.0.0.1allow192.168.0.0/16; denyall; 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/50 x.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. 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_se Ssion_cache shared: SSL: 1 m; # ssl_session_timeout 5 m; # ssl_ciphers HIGH :! ANULL :! MD5; # ssl_prefer_server_ciphers on; # location/{# root html; # index index.html index.htm ;#}#}}"

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces Nginx's Web cache service and xinlang's open-source NCACHE module, including some content, and hope to help friends who are interested in PHP tutorials.

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.