Replace squid[original with Nginx Proxy_cache cache function]

Source: Internet
Author: User
Tags failover nginx load balancing
Article Author: Zhang Yi This article version: v1.2 last modified: 2009.01.12 reprint please indicate the original link: http://blog.zyan.cc/nginx_cache/]

Nginx, starting with version 0.7.48, supports the caching function similar to squid. This cache is the URL and the associated combination as a key, with MD5 encoded hash saved on the hard disk, so it can support any URL links, but also support 404/301/302 such a non 200 status code. Although the official Nginx Web caching service can only set an expiration time for a specified URL or status code, and does not support a squid-like purge instruction, manually clear the specified cache page, but through a third-party nginx module, you can clear the cache for the specified URL.

The Nginx Web caching service consists of Proxy_cache related instruction set and Fastcgi_cache related instruction set, which is used to cache the back-end content source server for the reverse proxy, which is mainly used to cache the fastcgi dynamic program. The functions of both are basically the same.

The latest version of Nginx 0.8.32, Proxy_cache and Fastcgi_cache, has been perfected, plus a third party Ngx_cache_purge module (used to clear the cache for the specified URL), which can be completely replaced by squid. We have used the Nginx Proxy_cache cache function For more than two months in the production environment, very stable, not inferior to Squid.

Functionally, Nginx already has the Web cache Acceleration feature squid has, and the ability to clear the specified URL cache. But in the performance, Nginx to multi-core CPU's utilization, surpasses squid many. In addition, Nginx is much more powerful than squid in reverse proxies, load balancing, health checks, back-end server failover, rewrite rewriting, and ease of use. This allows a nginx to be used both as a load-balancing server and as a Web cache server.
  

1, Nginx load balancing and caching server in Linux under the compiler installation:
Ulimit-shn 65535
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.gz
Tar zxvf pcre-8.00.tar.gz
CD Pcre-8.00/
./configure
Make && make install
Cd.. /

wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
Tar zxvf ngx_cache_purge-1.0.tar.gz

wget http://nginx.org/download/nginx-0.8.32.tar.gz
Tar zxvf nginx-0.8.32.tar.gz
CD nginx-0.8.32/
./configure--user=www--group=www--add-module=. /ngx_cache_purge-1.0--prefix=/usr/local/webserver/nginx--with-http_stub_status_module--with-http_ssl_module
Make && make install
Cd.. /

2. The contents of the/usr/local/webserver/nginx/conf/nginx.conf configuration file are as follows:
User www www.

Worker_processes 8;

Error_log/usr/local/webserver/nginx/logs/nginx_error.log Crit;

Pid/usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors the can is 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;

Server_names_hash_bucket_size 128;
Client_header_buffer_size 32k;
Large_client_header_buffers 4 32k;
Client_max_body_size 300m;

Sendfile on;
Tcp_nopush on;

Keepalive_timeout 60;

Tcp_nodelay on;

Client_body_buffer_size 512k;
Proxy_connect_timeout 5;
Proxy_read_timeout 60;
Proxy_send_timeout 5;
Proxy_buffer_size 16k;
Proxy_buffers 4 64k;
Proxy_busy_buffers_size 128k;
Proxy_temp_file_write_size 128k;

gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 16k;
Gzip_http_version 1.1;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascript text/css application/xml;
Gzip_vary on;

#注: The paths specified by Proxy_temp_path and Proxy_cache_path must be in the same partition
Proxy_temp_path/data0/proxy_temp_dir;
#设置Web缓存区名称为cache_one, the memory cache space size is 200mb,1 Day has not been accessed automatically clear, hard disk cache space size of 30GB.
Proxy_cache_path/data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

Upstream Backend_server {
Server 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
Server 192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;
Server 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
}

Server
{
Listen 80;
server_name www.yourdomain.com 192.168.8.42;
Index index.html index.htm;
root/data0/htdocs/www;

Location/
{
#如果后端的服务器返回502, 504, execution timeout, and so on, automatically forwards the request to another server in the upstream load balancing pool to implement failover.
Proxy_next_upstream http_502 http_504 error timeout invalid_header;
Proxy_cache Cache_one;
#对不同的HTTP状态码设置不同的缓存时间
Proxy_cache_valid 304 12h;
#以域名, URI, and parameters are combined into the Web cache key value, nginx the cache content to the level two cache directory according to the key value hash
Proxy_cache_key $host $uri$is_args$args;
Proxy_set_header Host $host;
Proxy_set_header x-forwarded-for $remote _addr;
Proxy_pass Http://backend_server;
Expires 1d;
}

#用于清除缓存, assuming that a URL is http://192.168.8.42/test.txt, you can clear the cache of the URL by accessing the http://192.168.8.42/purge/test.txt.
Location ~/purge (/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存.
Allow 127.0.0.1;
Allow 192.168.0.0/16;
Deny all;
Proxy_cache_purge Cache_one $host $1$is_args$args;
}

Dynamic applications that end #扩展名以 PHP,. JSP,. 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 caching example:

  

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.