Nginx Web Caching service and the open source Ncache module of Sina Network

Source: Internet
Author: User
  #nginx Web cache service with Sina's Open source Ncache module    # # What is the  Web cache Web cache located between the content source Web server and the client, when the user accesses a URL, the Web cache server goes back to the backend Web source server to retrieve the content to output, and then When the next request arrives, if the access is the same as the Url,web cache server directly outputs the content 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 cache 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 primarily used to cache FASTCDI dynamic programs. Both functions are basically the same.  # # #proxy_cache related instruction set * *1, proxy_cache directive * * syntax: Proxy_Cache Zone_name;Default value: Noneusage Environment: http,server,locationThis instruction is used to set the buffer for which the buffer will be applied, and the value of zone_name isthe cache that is created by the proxy _ Cache_path instruction.  * *2, proxy_cache_path directive * * syntax: Proxy_Cache_path Path[levels=number ]keys_z  [max_size=size];Default value: Noneusage Environment: HTTP * *eg:* * Proxy_Cache_path/data0/proxy_Cache_dir levels=1:2 keys  _z>_ 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/fdg35415fg35f4gsdf2g1535gh465h;key_Zone parameter is used to name the buffer, 500m specifies the size of the memory space is 500MB The inactive 1d is deleted if the cached data is not accessed within 1 days; Max_size 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,locationthis 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,locationThis 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: Noneusage Environment: http,server,locationthis 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: Noneusage Environment: http,server,locationThis instruction is used to set the Web cache key value, Nginx MD5 Syrah storage cache according to the key value. Generally according to the  "$host (domain name), $request _uri (Request path )" and other composite variables to synthesize the proxy_Cache_ key. For example:'proxy_cache_key ' $host: $server _port$uri$is_args$args ";  `  # #Proxy_cache Complete Example    suyum-y install pcre//installationpcre wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gztar zxvf ngx_cache_purge-2.3.tar.gz//get nginx_ Cache_purgecd nginx-1.6.3//into your nginx file directory (Nginx installation Please 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      * * Configuration 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_z 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_oneProxy_cache Cache_one;#对不同状态码设置不同缓存时间Proxy_cache_valid 304 12h;Proxy_cache_valid 301 302 1m;Proxy_cache_valid any 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 ServersProxy_set_header Host $host;Proxy_set_header x-forwarded-for $remote _addr;Proxy_pass Http: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/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 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;#    }#}}```

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

The above describes the Nginx Web cache service and Sina Open source Ncache module, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.