是否有办法在 Nginx 安装中为所有域设置全局过期标头?

是否有办法在 Nginx 安装中为所有域设置全局过期标头?

刚接触 nginx。我搜索过这个,但找不到我想要的东西,也许我的想法是错误的。

有没有办法将以下规则添加到某个 nginx conf 中,该规则将应用于安装中添加的任何新域,而不必将规则添加到每个新域的 conf 中?

# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html|xml|json)$ {
  expires -1;
#  access_log logs/static.log;
}

# Feed
location ~* \.(?:rss|atom)$ {
  expires 1h;
  add_header Cache-Control "public";
}

# Favicon
location ~* \.ico$ {
  expires 1w;
  access_log off;
  add_header Pragma public;
  add_header Cache-Control "public";
}

# Media: images, video, audio, HTC, WebFonts
location ~* \.(?:jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {
  expires 1M;
  access_log off;
  add_header Pragma public;
  add_header Cache-Control "public";
}

location ~* \.(js|css)$ {
  expires 60d;
  add_header Pragma public;
  add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
  expires 1y;
  access_log off;
  add_header Cache-Control "public";
}

# opt-in to the future
add_header "X-UA-Compatible" "IE=Edge,chrome=1";

谢谢。

答案1

将您在此处发布的配置放入单独的文件(例如/etc/nginx/conf.d/expires.global),然后使用该文件包含在您的虚拟主机中包括指示。

server {
  ...
  include /etc/nginx/conf.d/expires.global;
}

相关内容