Location question

I have a request:

http://test.dev/imgheaders/<img_hash>/<img_serialize>

What I want to achieve is, get from location <img_hash>, and check if
file exists, if it exists then serve the file. if file not exists pass
<img_serialize> to php fastcgi script and it will return the image.

Any suggestions on how to achieve this?

On Thu, Sep 03, 2009 at 11:14:17AM +0200, Tomasz P. wrote:

I have a request:

http://test.dev/imgheaders/<img_hash>/<img_serialize>

What I want to achieve is, get from location <img_hash>, and check if
file exists, if it exists then serve the file. if file not exists pass
<img_serialize> to php fastcgi script and it will return the image.

Any suggestions on how to achieve this?

location ~ ^(/imgheaders/[^/]+)/[^/]$ {
root …
try_files $1 @php;
}

location @php {

}

On Thu, Sep 03, 2009 at 01:20:19PM +0400, Igor S. wrote:

Any suggestions on how to achieve this?

location ~ ^(/imgheaders/[^/]+)/[^/]$ {

  • location ~ ^(/imgheaders/[^/]+)/[^/]$ {
  • location ~ ^(/imgheaders/[^/]+)/[^/]+$ {

This worked great, but i had to change the request format to:

http://test.dev/imgheaders/<img_hash>.png?<img_serialize>

and the image if exists is in /vhosts/static/img/headers/<img_hash>.png

Can You please update the location statement, because I can’t get it
right, thanks!


Best regards
Tomasz P.

On Thu, Sep 03, 2009 at 12:33:43PM +0200, Tomasz P. wrote:

This worked great, but i had to change the request format to:

http://test.dev/imgheaders/<img_hash>.png?<img_serialize>

and the image if exists is in /vhosts/static/img/headers/<img_hash>.png

Can You please update the location statement, because I can’t get it right,
thanks!

It’s even simpler:

location /imgheaders/ {
root …
try_files $uri @php;
}

For those who would want to use such a setup:

    location ~ ^/imgheaders/(.*)$ {
            root /vhosts/static/img/headers;
            try_files /$1 @headers;
    }

    location @headers {
            fastcgi_pass unix:/var/run/spawn-fcgi.sock;
            fastcgi_param SCRIPT_FILENAME 

/vhosts/test/ImageText.php;
include fastcgi_params;
}

On Thu, Sep 03, 2009 at 01:59:52PM +0200, Tomasz P. wrote:

For those who would want to use such a setup:

   location ~ ^/imgheaders/(.*)$ {
           root /vhosts/static/img/headers;
           try_files /$1 @headers;
   }
    location /imgheaders/ {
            alias /vhosts/static/img/headers/;
            try_files $uri @headers;
    }
    location /imgheaders/ {

Igor wrote… ^

location ^~ /imgheaders/ {

You wrote… ^

:slight_smile:

  • Jeff
    location /imgheaders/ {
            alias /vhosts/static/img/headers/;
            try_files $uri @headers;
    }

this doesn’t work, maybe because i have one more location, this are all
locations in that server space.

location ^~ /imgheaders/ {
    alias /vhosts/static/img/headers;
    try_files $uri @headers;
}

location @headers {
    fastcgi_pass unix:/var/run/spawn-fcgi.sock;
    fastcgi_param SCRIPT_FILENAME /vhosts/test/ImageText.php;
    include fastcgi_params;
}

location ~* ^.+\.(js|ico|gif|jpg|png|css|swf|xml|xsl|flv|cur|mp3)$ {
    root /vhosts/static;
    access_log off;
    expires 30d;
}

On Thu, Sep 03, 2009 at 03:14:44PM +0200, Tomasz P. wrote:

   alias /vhosts/static/img/headers;
   root /vhosts/static;
   access_log off;
   expires 30d;

}

Yes, you need to disable regex for /imgheaders/ if you have also
“location ~* ^.+.(js|ico|gif|jpg|png|…”.