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'm trying to set a longer (30 day) 'expires' header for my (images only) in the /misc-stuff/ directory.

This is what I'm using for my site :

# Serve static files directly from nginx
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
   add_header        Cache-Control public;
   add_header        Cache-Control must-revalidate;
   expires           7d;
}

I want to be able to keep that code in to handle regular site images, but create a new block to handle the /misc-stuff/ directory.

I have tried :

location ^~ /misc-stuff/ {
...
}

The problem I'm having now is that my backup .php files in that directory show up as plain text if someone tries to access it.

How do I set it up so ONLY .gif images in the /misc-stuff/ directory are effected?

share|improve this question

migrated from stackoverflow.com Jun 22 '11 at 12:45

This question came from our site for professional and enthusiast programmers.

    
Could some explain why adding the ^ and $ is necessary? –  Dave Jun 23 '11 at 1:50
    
Essentially ^ means start of the line and $ means end of the line. –  TheLQ Jun 23 '11 at 2:07

1 Answer 1

Did you have a good reason for selecting the ^~ literal-string matching prefix? ^~ does not support regular expressions, so what you have now cannot easily be fixed. If, however, you've blindly copied this chunk from elsewhere without understanding it, then you're in luck; change to the ~* case insensitive regular expression matching prefix, and write a regular expression that matches your needs:

location ~* ^/misc-stuff/.*\.(jpg|jpeg|gif|png)$ {
...
}

Of course, your .php files in that directory might still match another location directive; be sure to write a location directive that sends those to the PHP interpreter of your choice, if you want them executable. (Or change their permissions to forbid the server to read them, if you want to keep them in that directory for some reason but not execute them or let others read them.)

While the standard nginx docs on the location directive are a little lacking, I've found the nginx wiki to be quite good.

share|improve this answer
    
It was a blind copy - copied / pasted the wrong example. I have another location block that passes php to apache. This works perfectly, thank you very much! –  dave Jun 22 '11 at 2:46
    
You should anchor that match with ^ at the beginning and $ at the end. –  kolbyjack Jun 22 '11 at 13:21
    
@kolbyjack, good idea, thanks. –  sarnold Jun 23 '11 at 0:14

Your Answer

 
discard

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