1

I have an alias "/temp" that works ok, but when I add another location block, the alias returns 404.

Working config: http://mysite.com/temp/blank.gif -> OK

server {
    # ...
    location /temp {
        alias  /var/www/temp;
    }
    # ...
}

Error config: http://mysite.com/temp/blank.gif -> 404

server {
    # ...
    location /temp {
        alias  /var/www/temp;
    }
    location ~ \.(?:jpg|png|gif)$ {
        expires  30d;
    }
    # ...
}

Here is my full config: http://pastebin.com/yExq0nMY

How can I solve it?

2 Answers 2

2

Use a nested location for this.

nginx matches only one location block at the server level, so you have to nest them in this circumstance. For full details, see the official documentation: How nginx processes a request.

server {
    # ...
    location /temp {
        alias  /var/www/temp;
        location ~ /temp/\.(?:jpg|png|gif)$ {
            expires  30d;
        }
    }
    location ~ \.(?:jpg|png|gif)$ {
        expires  30d;
    }
    # ...
}
1
  • Thank you for the resource on how nginx processes these locations/request. I like include other static files simple example that location ~* \.(ico|css|js|gif|jpe?g|png|bmp|html|htm)$ { # headers, etc } and you could do this for PHP or CFML with something like this that matches your dynamic files location ~ \.(cfm|cfml|cfc|jsp|cfr)(.*)$ { include tomcat_proxy_params; }
    – timbrown
    Aug 31, 2014 at 23:13
0

Do you have a root directory set? Please post more of the config.

Some essential reading as well: https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .