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;
expires 30d;
}
上述配置能够实现对ico、gif、bmp、jpg、jpeg、swf、js、css、mp3等文件的本地缓存,从而避免每次访问都需从服务器重新获取资源。
压缩功能的配置代码如下:
gzip on;
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_types text/plain application/x-javascript text/css;
该配置主要针对文本、js和css文件进行压缩处理。通常情况下,经过压缩后的文件大小仅为原始大小的25%,甚至更小。
接下来介绍动态页面的缓存机制及其过期时间设置。
此处使用虚拟机环境进行了相关配置演示:
Nginx配置文件的具体内容如下:
核心配置语句如下:
proxy_cache_path /www/ levels=1:2 keys_zone=Z:10m inactive=1m max_size=30g;
该语句定义了一个名为Z的区域,其在内存中的占用空间为10MB,而在硬盘中的最大存储空间限制为30GB。
参数inactive=1m表示缓存失效时间为1分钟,之后将重新从源服务器请求。这里需要纠正一个概念:当缓存项在1分钟内未被访问时,Nginx会自动将其删除。
主配置文件路径为:/usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
proxy_cache_path /www/ levels=1:2 keys_zone=Z:10m inactive=1m max_size=30g;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /www/;
#expires max;
#proxy_store on;
#proxy_store_access user:rw group:rw all:rw;
#proxy_temp_path /www/;
proxy_cache Z;
proxy_cache_valid 200 1m;
#expires max;
include proxy.conf;
if ( !-e $request_filename) {
proxy_pass http://192.168.1.199:45815;
}
}
#这里设置当 访问 /ajax/目录下的内容时候,直接从源服务器读取,主要用于ajax 的访问请求,要求实时的
location /ajax/ {
include proxy.conf;
if ( !-e $request_filename) {
proxy_pass http://192.168.1.199:45815;
}
}
#location ~.*.(jpg|png|jpeg|gif)
#{
# expires max;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
代理相关配置文件路径为:/usr/local/nginx/conf/proxy.conf
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;
proxy_set_header Accept-Encoding 'gzip';
client_max_body_size 100m;
client_body_buffer_size 256k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 512k;
proxy_buffers 8 512k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
HTML文件可以被缓存,此前曾在此问题上向多方请教过。