Objective: To cache static files on the nginx server. Such as CSS, JS, htm, HTML, JPG, GIF, PNG, FLV, SwF, these files are not updated frequently. It is easy to cache to reduce the pressure on the server.
Implementation: nginxproxy_cache can cache users' requests to a local directory. When the next request is sent, the cached files can be directly retrieved, so that the files do not need to be retrieved from the backend server.
Configuration: Open the configuration file/etc/nginx. conf.
UserWWW;
Worker_processes 2;
Error_log/Var/log/nginx/nginx_error.logCrit;
Worker_rlimit_nofile 65535;
Events
{
Use epoll;
Worker_connections 65535;
}
HTTP
{
Include Mime. types;
Default_typeApplication/octet-stream;
Server_names_hash_bucket_size 128;
Client_header_buffer_size 32 K;
Large_client_header_buffers 4 32 K;
Client_max_body_size 8 m;
Sendfile on;
Tcp_nopush On;
Keepalive_timeout 0;
Tcp_nodelay on;
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64 K;
Fastcgi_buffers 4 64 K;
Fastcgi_busy_buffers_size 128 K;
Fastcgi_temp_file_write_size 128 K;
# Cache ##
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;
Proxy_temp_path/home/temp_dir;
Proxy_cache_path/home/cache levels = 1: 2keys_zone = cache_one: 200 m inactive = 1D max_size = 30g;
# End ##
Gzip On;
Gzip_min_length 1 K;
Gzip_buffers 4 8 K;
Gzip_http_version1.1;
Gzip_types Text/plain application/X-JavaScript text/CSSApplication/XML;
Gzip_disable "MSIE [1-6] \.";
Log_formatAccess'$ Remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" $ http_x_forwarded_for ';
Upstream appserver {
Server 192.168.1.htm;
}
Server {
Listen 80 default;
SERVER_NAME blog.slogra.com;
Location ~ . * \. (GIF | JPG | PNG | HTM | HTML | CSS | JS | FLV | ICO | SWF )(.*){
Proxy_passHttp: // appserver;
Proxy_redirect
Off;
Proxy_set_header
Host $ host;
Proxy_cache
Cache_one;
Proxy_cache_valid
200 302 1 h;
Proxy_cache_valid
301 1D;
Proxy_cache_valid
Any 1 m;
Expires
30d;
}
Location
~ . * \. (PHP )(.*){
Proxy_passHttp: // appserver;
Proxy_set_header Host
$ Host;
Proxy_set_header X-real-IP
$ Remote_addr;