How to Set Expires With Try Files

Igor Sysoev igor at sysoev.ru
Fri May 20 09:36:15 MSD 2011


On Thu, May 19, 2011 at 09:54:41PM -0400, Joshua Pearce wrote:
> 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;
>         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;
> }

You do not need this block:

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

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


-- 
Igor Sysoev



More information about the nginx mailing list