Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I use the "apache style" nginx config (include /sites-enabled folder files and a single .conf for every website I host). How can I set this:

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";

... in my general nginx.conf? I'd like not to repeat that block on each server block (for every website I host).. the problem is that from what I understood the location block can only be positioned within a server block, and I can't define a server block on the general nginx.conf. I know a solution would be to add the block in a expires.conf file and include that on every website conf file (server block).. but: is there a way to set that "upstream" in the main nginx.conf?

share|improve this question
add comment

2 Answers

Configuring this in upstream is not possible, but you can just put it in the http block and it should work fine.

share|improve this answer
add comment
  • Create expires.conf on the same folder with nginx.conf
nano /etc/nginx/expires.conf
  • paste that expires configuration in this new file

  • on your vhost files for servers (usually in conf.d/ folder) include the new file with

include expires.conf;

hope that helps

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.