1

I am currently building a web application. In order for it to work properly I wrote this rule:

  location / {
    if (!-e $request_filename) {
      expires 168h;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";

      rewrite ^(.*)$ /libraries/render.php;
    }
  }

It is working for URLs with non-existent files like this: http://test.test/test but not for a non-existent image: http://test.test/test.png

I have a second rule that might be effecting images:

 location ~* \.(?:gif|jpe?g|png|ico)$ {
    expires 168h;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
  }

But I'm not sure how that would prevent the rewrite rule from triggering. It's all the extensions in this rule that are not getting forwarded to render.php
When I remove this rule (by commenting it out) all extensions except .png work. This is very strange.

This is my entire config:

server {
  rewrite_log on;

  # IPv4
  listen 80;
  listen 443 ssl;
  # IPv6
  # listen [::]:80 ipv6only=on;
  # listen [::]:443 ssl ipv6only=on;

  server_name test.test;

  # SSL
  ssl_certificate /etc/ssl/private/ssl-bundle.test.test.crt;
  ssl_certificate_key /etc/ssl/private/test.test.key;
  ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers HIGH:!aNULL:!MD5;

  root /var/www/test;

  index index.php index.html index.htm;
  try_files $uri $uri/ $uri/index.php $uri/index.html $uri/index.htm =404;
  disable_symlinks off;

  location / {
    if (!-e $request_filename) {
      expires 168h;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";

      rewrite ^(.*)$ /libraries/render.php;
    }
  }

  location = /config.inc.php {
    internal;
  }

  location /cache/ {
    internal;
  }

  location /libraries/ {
    internal;
  }

  location /images/ {
    internal;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-cgi alone:
    #fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }

  location ~ /\.ht {
    deny all;
  }

  location ~ /\.sh {
    deny all;
  }

  location ~* \.html$ {
    expires -1;
  }

  location ~* \.(?:gif|jpe?g|png|ico)$ {
    expires 168h;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
  }

  # Rewrites

  rewrite ^(?<filename>.+\.(?<type>css|js))$ /compress.php?file=$filename&type=$type;
}
  • When you say "working" for /test but not /test.png are those real files that are not supposed to be served by render.php or the other way around? – Stephen Ostermiller Nov 17 '14 at 17:38
  • These are both files that do not exist and that should be served by render.php. /test is being served by render.php but /test.png isn't. – BrainStone Nov 17 '14 at 17:55
  • Is it just .png image files that are not being forwarded to your php, or are other file types also problematic? – Stephen Ostermiller Nov 17 '14 at 17:58
  • And just to make sure I'm understanding it, if you remove the "exists" condition so that all requests get served by PHP, then the .png URL gets handled correctly? – Stephen Ostermiller Nov 17 '14 at 17:59
  • 1
    That is what I figured. I edited your question quite a bit to incorporate these comments. The title doesn't point the finger at that if statement anymore. – Stephen Ostermiller Nov 17 '14 at 18:37
1

So after some trial and error I came up with a working solution that I'm not quite happy with. But at least it works.

Since all "real" images are located in /includes/ or subdirectories I simply rewrote the rule for the images.

So this

 location ~* \.(?:gif|jpe?g|png|ico)$ {
    expires 168h;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
  }

Has been turned into this

 location ~* ^/includes/.+\.(?:gif|jpe?g|png|ico)$ {
    expires 168h;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.