Nginx Cache Configuration

Source: Internet
Author: User
Tags failover
Nginx Cache Configuration

Nginx starts with version 0.7.48 and supports a squid-like caching feature. This cache is a URL and associated combination as a key, with MD5 encoded hash saved on the hard disk, so it can support any URL link, but also support 404/301/302 such as non-200 status code. Although the official Nginx Web caching service can only set the expiration time for the specified URL or status code, does not support squid-like purge instructions, manually clears the specified cache page, but through a third-party Nginx module, the cache of the specified URL can be cleared. IT network, http://www.it.net.cn

Nginx Web Caching service is mainly composed of Proxy_cache related instruction set and Fastcgi_cache related instruction set, the former is used for reverse proxy, the backend content source server is cached, the latter is mainly used to cache fastcgi dynamic program. The two functions are essentially the same.

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

In the function, Nginx already has the Web cache acceleration function which squid owns, clears the function which the specified URL caches. In the performance, Nginx on the use of multi-core CPU, more than a lot of squid. In addition, Nginx is much more powerful than squid in reverse proxies, load balancing, health checks, backend server failover, rewrite rewriting, and ease of use. This allows an nginx to be used both as a "load balancer server" and a "Web cache server".

1, Nginx load balancer and cache server under Linux compilation and Installation:
Linux Learning, HTTP //linux.it.net.c

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/nginx--with-http_stub_status_module--with-http_ssl_module
Make && make install
Cd.. / It network, http://www.it.net.cn

2. The contents of the/usr/local/nginx/conf/nginx.conf configuration file are as follows:
IT network, http://www.it.net.cn

User www www;

Worker_processes 8;

Error_log/usr/local/nginx/logs/nginx_error.log Crit; IT network, http://www.it.net.cn

Pid/usr/local/nginx/logs/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; Linux Learning, HTTP //linux.it.net.cn

#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; IT network, http://www.it.net.cn

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; IT network, http://www.it.net.cn

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 path 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 size of the memory cache is automatically cleared for content that is not accessed for 200mb,1 days, and the disk cache space size is 30GB.
Proxy_cache_path/data0/proxy_cache_dir levels=1:2 keys_z 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.it.net.cn 192.168.8.42;
Index index.html index.htm;
root/data0/htdocs/www;

Location/
{
#如果后端的服务器返回502, 504, perform timeouts and other errors, and automatically forward the request to another server in the upstream load balancer pool for failover.
Proxy_next_upstream http_502 http_504 error timeout invalid_header;
Proxy_cache Cache_one;
#对不同的HTTP状态码设置不同的缓存时间
Proxy_cache_valid 304 12h;
#以域名, URI, parameters are combined into the Web cache key value, Nginx based on the key value hash, storage cache content in the level two cache directory
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;
}

# to clear the cache, assuming that 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 (/.*)
{
#设置只允许指定的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 with the extension 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;
}
} Linux Learning, HTTP //linux.it.net.cn


The above describes the Nginx cache configuration, 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.