Nginx use Proxy_cache to cache files, friends who need to refer to
| The code is as follows |
Copy Code |
|
User zhangy users;
Worker_processes 10;
Error_log/var/vlogs/nginx_error.log Crit;
Pid/var/vlogs/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 gb2312;
Server_names_hash_bucket_size 128;
Client_header_buffer_size 32k;
Large_client_header_buffers 4 32k;
Client_max_body_size 8m;
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 60;
Tcp_nodelay on;
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 128k;
//============
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;
Proxy_temp_path/usr/local/nginx/proxy_temp;
/*levels Setting the directory hierarchy
Keys_zone Setting the cache name and shared memory size
Inactive is deleted in 1 days if no one is visited within the specified time.
Max_size Maximum cache space */
Proxy_cache_path/usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
============ equal to the middle, the key is just add Proxy_cache_path
gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 16k;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascript text/css application/xml;
Gzip_vary on;
Upstream MYSELFXTAJMD {
Server 127.0.0.1:10002;
Server 127.0.0.1:10001 weight=5;
}
Server
{
Listen 10000;
server_name localhost;
Index index.html index.htm index.php;
Log_format access ' $remote _addr-$remote _user [$time _local] "$request"
' $status $body _bytes_sent ' $http _referer '
' "$http _user_agent" $http _x_forwarded_for ';
Access_log/var/log/test.log access;
Location/
{
Proxy_cache content; Content corresponding to root Keys_zone
Proxy_cache_valid 304 301 302 10d; Which states are cached for how long
Proxy_cache_valid any 1d; How long does the other cache
Proxy_cache_key $host $uri$is_args$args; Hash by key, define the value of key
Proxy_pass HTTP://MYSELFXTAJMD;
Proxy_redirect off;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
Dynamic let-off
Location ~. *. (php|jsp|cgi)? $
{
Proxy_set_header Host $host;
Proxy_set_header x-forwarded-for $remote _addr;
Proxy_pass HTTP://MYSELFXTAJMD;
}
}
}
|
The above is just the configuration of part of the content, the reverse proxy that part is not put in, refer to the Linux nginx reverse proxy, to achieve load balancing, when we browse http://localhost:10000/222.jpg, the proxy side of the picture cache, Without going to the proxy terminal to cache, you can save resources. The cached content is placed below the Proxy_cache_path path you set, see
Nginx Proxy_cache
The first level directory has only one character, which is set by Levels=1:2, a total of two levels, and the subdirectory name consists of two characters. Suddenly found that I in my system, configured with Varnish,squid,apache and Mod_cache,nginx and Proxy_cache, they can be cached files, do not know which of them to the file cache the best effect? Have a moment to compare, to the 4 kinds of cache, this blog has all, interested friends, can look at
Nginx uses Proxy_cache to cache files