-1

Working on using nginx to serve images.

static.foobar.com/UUID.jpg url will actually serve as a pointer to a PHP script which determines if you have permission to view that image (and if so, serve it via PHP/X-Accel-Redirect).

DNS/nginx are setup correctly on the subdomain, but I'm getting a 404. Here's my current server block:

server {
    listen x.x.x.x:80;

    server_name static.foobar.com;

    location ~ \.(gif|jpg|png) {
        internal;
        rewrite "^/([a-f0-9]{8})-([a-f0-9]{4})-4([a-f0-9]{3})-([a-f0-9]{4})-([a-f0-9]{12}).(gif|jpg|png)" /dir/photo.php?uuid=$1-$2-4$3-$4-$5 last;
    }
}

I'm wondering if the presence of gif|jpg|png is forcing nginx to set the mime-type as an image, which is causing a conflict when I send the session to a PHP file. Could that be the issue? What else can I do to help debug this or does anyone have any pointers please?

Thanks kindly


Update: in the end, the problem was that on my server, nginx php-fpm is not supported with cpanel, so my nginx config wasn't able to run php at all. I was hoping to offload serving of these files via nginx but will unfortunately have to go back to using apache. Thank you all for your time and help.

4

1 Answer 1

1

One issue here is at least that you are using the internal keyword inside the location. This means that it will match only requests coming from inside nginx, for example from other location blocks. You should remove that keyword.

Your regexes need also other fixes. Try this location block:

location ~ \.(gif|jpg|png)$ {
    rewrite "^/([a-f0-9]{8})-([a-f0-9]{4})-4([a-f0-9]{3})-([a-f0-9]{4})-([a-f0-9]{12})\.(gif|jpg|png)$" /dir/photo.php?uuid=$1-$2-4$3-$4-$5 last;
}
2
  • 1
    Not an error/issue but that rewrite rule can be simplified a little - since there's no need for a capturing group for each bit of the uuid, to then rebuild the uuid. i.e. it can be written with only one capturing group: rewrite "^/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12})\.(?:gif|jpg|png)$" /dir/photo.php?$1;
    – AD7six
    Mar 29, 2015 at 11:31
  • Thank you Tero. I was going by the x-accel-redirect docs on nginx and it suggested the use of internal. I was not aware of this restriction. Thank you also, AD7six, that is far more palatable. :)
    – appl3s
    Mar 30, 2015 at 2:37

Not the answer you're looking for? Browse other questions tagged .