用.htaccess设置网页/图片/js/css文件内容缓存

本文介绍了一种通过设置网页缓存来提升用户体验的方法。通过在 .htaccess 文件中添加特定代码,可以为不同类型的文件(如图片、CSS 和 JS 文件)设定缓存时间,从而减少服务器负载并加快页面加载速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

合理的设置网页图片、css/js文件、html静态网页,甚至是动态网页设定一定的缓存时间不仅仅是为了节省服务器开销,

更多的时候可以让用户感觉”网速更快“,合理设置缓存是必要的,特别是图片/css/js/txt/静态html文件等不经常变化的文件。

在.htaccess文件中加入下面的代码就是设置网页缓存时间的哦, 如果是在开发调试的阶段可以暂时关闭这个功能。

  1. <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|css|txt|pdf)$">
  2. Header set Cache-Control "max-age=36000"
  3. </FilesMatch>

这段代码设定的是缓存10个小时,可以根据自己的需要做修改。

转自:http://www.lao8.org/article_1260/tupianhuancun

作者:老修-走失的镜头盖


# 注:这是访问方式为 http://IP:PORT/ 的配置 server { listen 80; server_name X.X.X.X; root /var/www/nextcloud; index index.html index.htm index.php; # 下面这行是设置上传文件大小限制,0 不限制大小,100M 限制文件大小不超过 100M client_max_body_size 0; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php(?:$|/) { fastcgi_split_path_info ^(.+\.php)(/.+)$; # 要注意下面这行的接口的版本,版本要与 php 的版本一样,可以去指定的地址查看一下 fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } location ~ /\.ht { deny all; } # 解决 nextcloud 中的一个安全检查的 .mjs 问题 location ~* \.mjs$ { types { } default_type application/javascript; } location ^~ /.well-known { # The rules in this block are an adaptation of the rules # in `.htaccess` that concern `/.well-known`. location = /.well-known/carddav { return 301 /remote.php/dav/; } location = /.well-known/caldav { return 301 /remote.php/dav/; } location /.well-known/acme-challenge { try_files $uri $uri/ =404; } location /.well-known/pki-validation { try_files $uri $uri/ =404; } # Let Nextcloud's API for `/.well-known` URIs handle all other # requests by passing them to the front-end controller. return 301 /index.php$request_uri; } # Optional: set long EXPIRES header on static assets location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ { expires 30d; access_log off; } }
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值