Since version 0.7.48, Nginx supports a cache function similar to Squid. This cache uses the URL and related combinations as keys, uses md5 encoding and hashes them and saves them on the hard disk, so it can support any URL link, and also supports non-200 status codes such as 404/301/302. Although the current official Nginx Web cache service can only set the expiration time for the specified URL or status code, and does not support the Squid-like PURGE instruction to manually clear the specified cached page, however, through a third-party Nginx module, the cache of the specified URL can be cleared. .
Nginx's Web cache service is mainly composed of proxy_cache related instruction sets and fastcgi_cache related instruction sets. The former is used to cache the back-end content source server when reverse proxying, and the latter is mainly used to cache FastCGI dynamic programs. The functions of both are basically the same.
Nginx's proxy_cache and fastcgi_cache are relatively complete, and the third-party ngx_cache_purge module (used to clear the cache of the specified URL) can completely replace Squid. And very stable, the speed is not inferior to Squid.
1. Required software
yum -y install gcc gcc-c++ autoconf openssl openssl-devel psmisc zip
pcre-8.40.tar.gz ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
ngx_cache_purge-1.6.tar.gz http://labs.frickle.com/files/
Tengine-2.0.0.tar.gz http://tengine.taobao.org/download.html
Second, compile and install
/usr/sbin/groupadd www
/usr/sbin/useradd -g www -s /sbin/nologin www
mkdir /data/proxy_temp_dir
mkdir /data/proxy_cache_dir
tar zxvf pcre-8.40.tar.gz
tar zxvf ngx_cache_purge-1.6.tar.gz
tar zxvf tengine-2.0.0.tar.gz
cd tengine-2.0.0 /
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.6 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=../pcre-8.40
#Note: --with-pcre=../pcre-8.37 points to the path to decompress the source package, not the path to install, otherwise an error will be reported,
make && make install
cd ../
Three, nginx configuration file
##################################################################
user www www;
worker_processes 8;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/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 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;
#Note: The paths specified by proxy_temp_path and proxy_cache_path must be in the same partition
proxy_temp_path /data/proxy_temp_dir;
#Set the name of the web cache area to cache_one, the size of the memory cache space to 200MB, the content that has not been accessed for one day will be automatically cleared, and the size of the hard disk cache space to be 30GB.
proxy_cache_path /data/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;
index index.html index.htm;
root /data0/htdocs/www;
location /
{
#If the backend server returns errors such as 502, 504, execution timeout, etc., the request is automatically forwarded to another server in the upstream load balancing pool to achieve 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 12h;
#Combines the domain name, URI, and parameters to form the key value of the web cache. Nginx stores the cached content in the secondary cache directory according to the hash of the key value.
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;
}
#Used to clear the cache, assuming a URL is http://192.168.8.42/test.txt, you can clear the cache of the URL by visiting http://192.168.8.42/purge/test.txt.
location ~ /purge(/.*)
{
#Set to allow only the specified IP 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;
}
#Dynamic applications with extensions ending in .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;
}
}
##################################################################
Enable Nginx
/usr/local/nginx/sbin/nginx