大网数据广告活动
网站运营与故障问答

How to leverage browser cache using .htaccess on nginx?

· 52CE 我爱测

I'm trying to increase the pagespeed of our website ubercore.co . I'm using PageSpeed Insights for Chrome extension to analyze the pagespeed. It has suggested me to "Leverage browser cache". This is where I'm stuck. I've tried so…

问题原文

原帖链接 · Vikas Swami · CC BY-SA 3.0

I'm trying to increase the pagespeed of our website ubercore.co. I'm using PageSpeed Insights for Chrome extension to analyze the pagespeed. It has suggested me to "Leverage browser cache". This is where I'm stuck.

I've tried some modifications to .htaccess file like this:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

and get nothing.

Later did some research and found that .htaccess is only for Apache server. Correct me if I'm wrong.

But, when I see the header response of our webpage found that the server is nginx. Then why my host has the .htaccess file in web root? I'm confused.

What I need in short: How to cache static contents for this site?

Useful links: Nginx wiki

Update 1:

Here is a screenshot of what I have, server is nginx:

My question is simple, I need to cache all static content given the environment.

Update 2: As suggested by dan found that the server is indeed Apache not nginx. I don't really understand it but let it be it's off topic.

Now my problem:

Server software : Apache/2.4.7 (Unix)

Need : .htaccess code to set browser cache expiry time.

已采纳解答

原帖链接 · zigojacko · CC BY-SA 4.0

You can specify your cache expiry in the vhost configuration on Nginx server, for example:-

location ~* \.(js|css|png|jpg|jpeg|gif|ico)(\?ver=[0-9.]+)?$ {
    expires 1y;
}

You'll only be able to use location in a server block however (and not http block) but you could insert this into a separate file and then include it in each server block that requires this though.

Read up on leverage browser caching at Google.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2014-03-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。