cc young
cc young

Reputation: 20213

with nginx, how do you rewrite a request to a different directory?

on site, favicon.ico is kept in the /images/ directory, not the root. how do I tell nginx to look there?

have tried - which looks right but does not work:

location = /favicon.ico$ { rewrite /(.*) /images/$1 last; }

returns 404 Not Found

the file is there: requesting http://www.example.com/images/favicon.ico is successful

Upvotes: 1

Views: 6285

Answers (2)

Civilian
Civilian

Reputation: 654

We can also solve this with rewrite. (I had to solve it using rewrite, because I'm using a try_files directive.)

Here's my config:

# Long cache times for static content
location ~ /_/static/(.+)$  {
    # hold the last five versions of our static content
    try_files
        /_/static_477526f-master/$1
        /_/static_05c8613-release/$1
        /_/static_05c8613-master/$1
        /_/static_db26497-release/$1
        /_/static_db26497-master/$1;
    expires  365d;
    add_header Cache-Control public;
    add_header Access-Control-Allow-Origin *;
}

location = /favicon.ico {
    rewrite . /_/static/favicon.ico;
    expires  14d;
    add_header Cache-Control public;
}

The . regex matches anything, and the second entry rewrites the url. Since this is a favicon-specific location, we can just hardcode it instead of using regex capturing and $1.

Upvotes: 2

Related Questions