Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running php code inside an image file on a certain directory from nginx

Tags:

php

nginx

We recently migrated to nginx and we need to transfer as well our htaccess config the like below from a certain directory/path(www.domain.com/images/test.jpg) in which that image file contains a php code that we want to run.

AddHandler application/x-httpd-ea-php56 .jpg .png .gif

Did some research and found examples below, but since I am not really that familiar with how to configure nginx, I am not sure why it isn't working.

first:

location ~ \.(php|jpg)$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

second:

location ~ .*\.php$ {
   root   /var/www/html/www.domain.com;
    if (!-f $request_filename) {
       rewrite ^/(.*)$ /index.php?q=$1;
       break;
    }
  include        fastcgi_params;
 #root           html;
 fastcgi_pass   127.0.0.1:9000;
 fastcgi_intercept_errors off;
 fastcgi_index  index.php;
 fastcgi_param  SCRIPT_FILENAME  /var/www/html/www.domain.com$fastcgi_script$
}

I hope some can help me.

UPDATE 08/09/2019 -- ANSWER

location ~ ^/pathname/images/(.*)\.(jpg|png|gif)$
{
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
like image 355
Roljhon Avatar asked Oct 27 '22 11:10

Roljhon


1 Answers

Can you try this code:

location ~* \.(jpg|jpeg|gif|png|bmp)$ {
    try_files $uri $uri/ /index.php$is_args$args;

    add_header        Cache-Control public;
    add_header        Cache-Control must-revalidate;
    expires           7d;
}

You might need to modify /index.php?$is_args$args part on the try_files line so that you get the correct parameters for your script, as your initial question didn't show clearly what the parameters are you want.

Credits goes to nginx serve image via php script

like image 146
Ray A Avatar answered Oct 31 '22 09:10

Ray A