# How to Set Expires With Try Files

**URL:** <https://www.ruby-forum.com/t/how-to-set-expires-with-try-files/207189>\
**Category:** NGINX\
**Created:** [May 20, 2011, 7:12am UTC](https://www.ruby-forum.com/t/how-to-set-expires-with-try-files/207189 "2011-05-20T07:12:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 20, 2011, 7:12am UTC](https://www.ruby-forum.com/t/how-to-set-expires-with-try-files/207189/1 "2011-05-20T07:12:11Z")

</div>

Below is my config file. How can I change the two “if” blocks to  
try\_files, and still do a 30d expires for static files?

server {  
listen 80;  
server\_name [www.joshpearce.org](http://www.joshpearce.org);  
client\_max\_body\_size 4M;  
access\_log /var/log/nginx/localhost.access.log;

```
    # root of website *exactly*, use '=' for speed
    location = / {
            root /var/www/joshpearce.org;
            index index.php index.html index.htm;
    }
    location / {
            root /var/www/joshpearce.org;
            index index.php index.html index.htm;
            if (-f $request_filename) {
                    expires 30d;
                    break;
            }
            if (!-e $request_filename) {
                    rewrite ^(.+)$ /index.php?q=$1 last;
            }
    }
    # use: htpasswd - htpwds NewUser NewPassword
    location ^~ /info {
            auth_basic "Restricted";
            auth_basic_user_file /etc/nginx/htpwds;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/usr/local/var/run/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME 

```

/var/www/joshpearce.org$fastcgi\_script\_name;  
}  
location ~ .php$ {  
include /etc/nginx/fastcgi\_params;  
fastcgi\_pass unix:/usr/local/var/run/php-fpm.sock;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
/var/www/joshpearce.org$fastcgi\_script\_name;  
}

```
    #error_page 404 /404.html;

```

}

---

<div class="post-metadata">

**Author:** ![Joshua\_Pearce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@Joshua\_Pearce](https://www.ruby-forum.com/u/Joshua_Pearce)\
**Post date:** [May 20, 2011, 7:36am UTC](https://www.ruby-forum.com/t/how-to-set-expires-with-try-files/207189/2 "2011-05-20T07:36:51Z")

</div>

On Thu, May 19, 2011 at 09:54:41PM -0400, Joshua Pearce wrote:

> ```
> root /var/www/joshpearce.org;
> rewrite ^(.+)$ /index.php?q=$1 last;
> }
> location ~ \.php$ {
> include /etc/nginx/fastcgi_params;
> fastcgi_pass unix:/usr/local/var/run/php-fpm.sock;
> fastcgi_index index.php;
> fastcgi_param SCRIPT_FILENAME 
> 
> ```

/var/www/joshpearce.org$fastcgi\_script\_name;

> ```
> }
> 
> #error_page 404 /404.html;
> 
> ```
> 
> }

You do not need this block:

```
             if (-f $request_filename) {
                     expires 30d;
                     break;
             }

```

at all, since nginx adds Expires only to existant files.

–  
Igor S.
