Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在nginx中特定目录上的图像文件中运行php代码_Php_Nginx - Fatal编程技术网

在nginx中特定目录上的图像文件中运行php代码

在nginx中特定目录上的图像文件中运行php代码,php,nginx,Php,Nginx,我们最近迁移到了nginx,我们还需要从某个目录/路径(www.domain.com/images/test.jpg)传输下面类似的htaccess配置,其中图像文件包含我们想要运行的php代码 AddHandler application/x-httpd-ea-php56 .jpg .png .gif 我做了一些研究并在下面找到了一些例子,但由于我对如何配置nginx并不太熟悉,我不确定它为什么不起作用 第一: location ~ \.(php|jpg)$ { try_files

我们最近迁移到了nginx,我们还需要从某个目录/路径(www.domain.com/images/test.jpg)传输下面类似的htaccess配置,其中图像文件包含我们想要运行的php代码

AddHandler application/x-httpd-ea-php56 .jpg .png .gif
我做了一些研究并在下面找到了一些例子,但由于我对如何配置nginx并不太熟悉,我不确定它为什么不起作用

第一:

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;
}
第二:

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$
}
我希望有人能帮助我

更新日期:2019年9月8日——回答

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;
}
您可以尝试以下代码:

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;
}
您可能需要修改try_files行中的
/index.php?$is_args$args
部分,以便为脚本获取正确的参数,因为您最初的问题没有清楚地显示您需要哪些参数


如果您想限制到某个域和文件夹,您可以使用以下方法将积分转到

location ~ .*\.php|(/folder/images/.*\.(jpg|jpeg|gif|png|bmp))$ {
   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$
}

你犯了什么错误?是404还是脚本未运行您从未指定当前配置有何问题?你需要先详细说明你是谁getting@TarunLalwani我提到据我所知它不起作用?我不知道nginx,我搜索了这么多,但没有找到答案,这就是我来这里的原因?我不知道到底发生了什么,我用一个正常的破损图像页面来表示,但我希望一个PHP代码运行并返回给我一些东西,但它没有,这就是当前显示的内容。你能做一个
curl-vhttp://
并运行一个你希望工作的url吗?并将日志输出添加到question@TarunLalwanieditedI会试试这个我的朋友,谢谢你的回答这会适用于域中的所有图像吗?或者我们如何使它特定于一个目录?除了try_文件之外的所有图像都将确保如果该文件已经存在,它将从disk@TarunLalwani如果我只想将配置应用于域/目录_first/images/,配置会是什么样子?此目录中的所有图像都可以作为普通php代码运行?@Roljhon幸运吗?这仍然不起作用,嗯,根代表什么?我应该用web文件所在的实际路径替换它吗?那么域名呢?对不起,我不太懂nginx。你的Php文件能用吗?您还可以将旧的apache配置添加到问题中吗?可以,所有这些都在工作,我在上面提到,这是我们之前从apache在htaccess
AddHandler应用程序/x-httpd-ea-php56.jpg.png.gif中进行的唯一设置。您能否确认图像本身是否是实际的php文件?图像包含在浏览器中加载图像时运行的php代码。