Use the proxy_cache cache function of nginx to replace squid [original] [Article banquet version: v1.2 last modified: 2009.01.12 reprinted. Please specify the original article link: http://blog.zyan.cc/nginx_cache/] nginx from version 0.7.48, supports cache functions similar to squid. This cache uses the URL and related combinations as the key and stores them on the hard disk after MD5 hash. Therefore, it supports any URL link, it also supports non-404/301 status codes such as 302/200. Although the official nginx Web Cache service can only set an expiration time for a specified URL or status code, unlike the squid purge command, you can manually clear the specified cache page. However, A third-party nginx module can clear the cache of a specified URL. The nginx Web Cache service consists of the proxy_cache instruction sets and fastcgi_cache instruction sets. The former is used to cache the back-end content source server for reverse proxy, the latter is mainly used to cache FastCGI dynamic programs. The two functions are basically the same. In the latest nginx 0.8.32, proxy_cache and fastcgi_cache have been well-developed. In addition, the third-party ngx_cache_purge module (used to clear the cache of the specified URL) can completely replace squid. We have used the proxy_cache cache function of nginx in the production environment for more than two months, which is very stable and not inferior to squid. In terms of functions, nginx already has the Web cache acceleration function of squid and the function of clearing the specified URL cache. In terms of performance, nginx's use of multi-core CPU is much better than squid. In addition, nginx is much more powerful than squid in reverse proxy, Server Load balancer, health check, backend server failover, rewrite, and ease of use. This allows an nginx instance to be used as both the "Server Load balancer" and "Web Cache Server. 1. nginx load balancing and Cache Server compilation and installation in Linux: ulimit-shn 65535 wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.gztar zxvf pcre-8.00.tar.gzcd pcre-8.00 /. /configuremake & make installcd .. /wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gztar zxvf ngx_cache_purge-1.0.tar.gzwget http://nginx.org/download/nginx-0.8.32.tar.gztar zxvf nginx-0.8.32.tar.gzcd /. /configur E -- user = WWW -- group = WWW -- add-module = .. /ngx_cache_purge-1.0 -- prefix =/usr/local/webserver/nginx -- with-http_stub_status_module -- with-http_ssl_modulemake & make installcd .. /2./usr/local/webserver/nginx/CONF/nginx. the content of the conf configuration file is as follows: User WWW; worker_processes 8; error_log/usr/local/webserver/nginx/logs/nginx_error.log crit; PID/usr/local/webserver/nginx. PID; # specifies the value for maximum File descriptors that can be opened by this process. worker_rlimit_nofile 65535; events {use epoll; worker_connections 65535;} HTTP {include mime. types; default_type application/octet-stream; charset UTF-8; Limit 128; Limit 32 K; limit 4 32 K; client_max_body_size 300 m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; Limit 512 K; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; Limit 16 K; proxy_buffers 4 64 K; Limit 128 K; Limit 128 K; gzip on; gzip_min_length 1 K; gzip_buffers 4 16 K; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/X-JavaScript text/CSS application/XML; gzip_vary on; # Note: proxy_temp _ The paths specified by path and proxy_cache_path must be in the same partition: proxy_temp_path/data0/proxy_temp_dir; # Set the Web cache name to cache_one and the memory cache size to 200 MB, the content not accessed within one day is automatically cleared, and the size of the hard disk cache space is 30 GB. Export/data0/proxy_cache_dir levels = keys_zone = cache_one: 200 m inactive = 1D max_size = 30g; upstream backend_server {server 192.168.8.43: 80 Weight = 1 max_fails = 2 fail_timeout = 30 s; server 192.168.8.44: 80 Weight = 1 max_fails = 2 fail_timeout = 30 s; server 192.168.8.45: 80 Weight = 1 max_fails = 2 fail_timeout = 30 s;} server {Listen 80; SERVER_NAME www.yourdomain.com 192.168.8.42; index index.html in Dex.htm; root/data0/htdocs/WWW; Location/{# If the backend server returns errors such as 502, 504, and execution timeout, requests are automatically forwarded to another server in the upstream Server Load balancer pool for failover. Proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; # set different cache times for different HTTP status codes proxy_cache_valid 200 304 12 h; # combine domain names, Uris, and parameters into the Web cache key values. nginx stores the cached content to the proxy_cache_key $ host $ URI $ is_args $ ARGs in the second-level cache directory based on the hash of the key values; proxy_set_header host $ host; proxy_set_header X-forwarded-for $ remote_addr; proxy_pass http: // backend_server; expires 1D ;}# used to clear the cache. Assume that a URL is http: // 192.168.8.42/te St.txt, you can clear the URL cache by accessing http: // 192.168.8.42/purge/test.txt. Location ~ /Purge (/. *) {# sets only the specified IP address or IP segment to clear the URL cache. Allow 127.0.0.1; allow 192.168.0.0/16; deny all; proxy_cache_purge cache_one $ host $1 $ is_args $ ARGs;} # extension. PHP ,. JSP ,. dynamic applications ending with CGI are not cached. Location ~. * \. (PhP | JSP | CGI )? $ {Proxy_set_header host $ host; proxy_set_header X-forwarded-for $ remote_addr; proxy_pass http: // backend_server;} access_log off;} 3. Start nginx: /usr/local/webserver/nginx/sbin/nginx 4. Clear the specified URL cache example: Click to browse the image in a new window.
Use the proxy_cache cache function of nginx to replace squid [original]