Replace squid with nginx's proxy_cache cache Function

Source: Internet
Author: User
Tags md5 hash

[Original article link: http://blog.s135.com/nginx_cache/]
Nginx versions 0.7.48 and later support 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.
Nginx Web Cache service mainly consists of proxy_cache instruction sets and fastcgi_cache instruction sets. The former is used to cache backend content source servers for reverse proxy, and the latter is mainly used for FastCGI dynamicProgramCache. 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. Compile and install nginx Server Load balancer and cache server in Linux:

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/usr/local/webserver/nginx/CONF/nginx. 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;
Server_names_hash_bucket_size 128;
Client_header_buffer_size 32 K;
Large_client_header_buffers 4 32 K;
Client_max_body_size 300 m;
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 60;
Tcp_nodelay on;
Client_body_buffer_size 512 K;
Proxy_connect_timeout 5;
Proxy_read_timeout 60;
Proxy_send_timeout 5;
Proxy_buffer_size 16 K;
Proxy_buffers 4 64 K;
Proxy_busy_buffers_size 128 K;
Proxy_temp_file_write_size 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: The paths specified by proxy_temp_path and proxy_cache_path must be in the same partition.
Proxy_temp_path/data0/proxy_temp_dir;
# Set the name of the Web cache area to cache_one. The size of the memory cache space is 200 MB. The content not accessed within one day is automatically cleared. The size of the hard disk cache space is 30 GB.
Proxy_cache_path/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 index.htm;
Root/data0/htdocs/WWW;
Location/
{
# If the backend server returns errors such as 502, 504, and execution timeout, the request is 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 key values of the Web cache. nginx hashes the cached content to the second-level cache directory based on the key values.
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;
}
# It is used to clear the cache. If a URL is http: // 192.168.8.42/test.txt, you can clear the cache of the URL by accessing http: // 192.168.8.42/purge/test.txt.
Location ~ /Purge (/.*)
{
# Only the specified IP address or IP segment is allowed 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;
}
# Dynamic applications whose extensions end with. php,. jsp, And. 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. Example of clearing the specified URL cache:

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.