Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an nginx server setup to act as a fileserver. I want to set the expires directive on images. This is how a part of my config file looks like.

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    location ~* \.(ico|jpg|jpeg|png)$ {
        expires 1y;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

I get the following error when I reload config - "Location directive not allowed here".

Can someone tell me what the right syntax for this is?

Thanks in advance.

EDIT : Found the answer myself. Added it in a comment. Closing this.

share|improve this question
up vote 0 down vote accepted

location is valid only within a server block, not within the parent http block. You'll need to put this config inside your server blocks.

share|improve this answer
Yeah after some digging around, I edited the sites-enabled/default file to add the expires header inside a server block. Thanks anyway – ryan Jun 20 '12 at 19:53

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.