Nginx设置静态页面压缩和缓存过期时间的方法
使用nginx服务器的朋友可能都知道需要设置html静态页面缓存与页面压缩与过期时间的设置了,下面我来给各位同学介绍一下配置方法,包括对ico,gif,bmp,jpg,jpeg,swf,js,css,mp3文件进行本地缓存.
可以通过nginx对服务器上的静态资源进行过期时间设置和对资源进行压缩传输来减少服务器的带宽开销。
以下是nginx对静态资源过期时间的设置方法:
location~*.(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3){
root/var/www/opkeep;
expires30d;
}
上面的配置可以对ico,gif,bmp,jpg,jpeg,swf,js,css,mp3文件进行本地缓存,不用每次访问都重新从服务器获取。
压缩的配置如下:
gzipon; gzip_min_length1000; gzip_buffers48k; gzip_typestext/plainapplication/x-javascripttext/css;
对文本、js和css文件进行压缩,一般情况下,压缩后的大小是原始大小的25%,甚至更小。
缓存动态页面,缓存过期时间
这里用虚拟机配置了下:
nginx配置文件内容:
主要是这一句:
proxy_cache_path/www/levels=1:2keys_zone=Z:10minactive=1mmax_size=30g;
这一句定义一个区域,名字是Z,在内存中的空间为10MB,硬盘中的最大空间为30G;
inactive=1m 是,1分钟之后缓存失效,从新从源服务器请求
这里纠正一下,inactive=1m 如果缓存1分钟没人访问,nginx会删除掉这些缓存
/usr/local/nginx/conf/nginx.conf
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
events{
worker_connections1024;
}
http{
includemime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
#'$status$body_bytes_sent"$http_referer"'
#'"$http_user_agent""$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfileon;
#tcp_nopushon;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
proxy_cache_path/www/levels=1:2keys_zone=Z:10minactive=1mmax_size=30g;
server{
listen80;
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
location/{
root/www/;
#expiresmax;
#proxy_storeon;
#proxy_store_accessuser:rwgroup:rwall:rw;
#proxy_temp_path/www/;
proxy_cacheZ;
proxy_cache_valid2001m;
#expiresmax;
includeproxy.conf;
if(!-e$request_filename){
proxy_passhttp://192.168.1.199:45815;
}
}
#这里设置当访问/ajax/目录下的内容时候,直接从源服务器读取,主要用于ajax的访问请求,要求实时的
location/ajax/{
includeproxy.conf;
if(!-e$request_filename){
proxy_passhttp://192.168.1.199:45815;
}
}
#location~.*.(jpg|png|jpeg|gif)
#{
#expiresmax;
#}
#error_page404/404.html;
#redirectservererrorpagestothestaticpage/50x.html
#
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
#proxythePHPscriptstoApachelisteningon127.0.0.1:80
#
#location~.php${
#proxy_passhttp://127.0.0.1;
#}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
#
#location~.php${
#roothtml;
#fastcgi_pass127.0.0.1:9000;
#fastcgi_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}
#denyaccessto.htaccessfiles,ifApache'sdocumentroot
#concurswithnginx'sone
#
#location~/.ht{
#denyall;
#}
}
#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
#
#server{
#listen8000;
#listensomename:8080;
#server_namesomenamealiasanother.alias;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
#HTTPSserver
#
#server{
#listen443;
#server_namelocalhost;
#sslon;
#ssl_certificatecert.pem;
#ssl_certificate_keycert.key;
#ssl_session_timeout5m;
#ssl_protocolsSSLv2SSLv3TLSv1;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
}
/usr/local/nginx/conf/proxy.conf
proxy_redirectoff; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_set_headerAccept-Encoding'gzip'; client_max_body_size100m; client_body_buffer_size256k; proxy_connect_timeout60; proxy_send_timeout60; proxy_read_timeout60; proxy_buffer_size512k; proxy_buffers8512k; proxy_busy_buffers_size512k; proxy_temp_file_write_size512k;
html文件可以被缓存,后来就到很多地方去问