【问题标题】:How to pre-interpret php in .html files on NGINX?如何在 NGINX 上预解释 .html 文件中的 php?
【发布时间】:2021-03-16 03:17:38
【问题描述】:

我在这方面找到了一些线索,但我找到的解决方案不起作用。我试图让我的 NGINX 服务器在将 html 文件发送到浏览器进行处理之前解释 html 文件中的 php。这是我的服务器括号内的一些代码 - 在我唯一启用站点的目录文件中,“默认:”

location ~ \.(php|html|htm)$ {
                include snippets/fastcgi-php.conf;
                root            html;
                fastcgi_index   index.php;
                # With php-fpm (or other unix sockets):
                fastcgi_param HTTP_PROXY "";
                fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                include fastcgi_params;
                # With php-cgi (or other tcp sockets):
                #fastcgi_pass 127.0.0.1:9000;
        }

具体来说,我希望 (php|hmtl|htm) 的部分成为解决方案。但是,我的 .html 文件中的 php 仍然被注释掉并且没有被处理。

不确定 security.limit_extensions 参数在哪里?这是我在 sn-ps 文件夹中的 fastcgi-php.conf:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;

感谢您的任何想法。

【问题讨论】:

  • html文件的扩展名是php。
  • 显然是最简单的解决方案。环境阻止我这样做。因此问题。
  • 您可以将您的正则表达式缩短为location ~ \.(php|html?)$ { ... }

标签: php html nginx


【解决方案1】:

我不知道你的snippets/fastcgi-php.conf 里面有什么,以及它是否还以某种方式限制了通过 PHP-FPM 处理 PHP 代码(我个人更喜欢自己编写 PHP 处理程序位置),但至少你需要调整 PHP-FPM 池配置文件中的 security.limit_extensions 参数(默认为 .php .phar)。如果没有帮助,请将snippets/fastcgi-php.conf 文件内容附加到您的问题中。

【讨论】:

  • @rickster26ter1 在snippets/fastcgi-php.confinclude fastcgi_params; 服务器配置中同时看到include fastcgi.conf; 是很奇怪的。哪一个是实际存在/使用的?此外,您可以尝试fastcgi_split_path_info ^(.+?\.(?:php|html?))(/.*)$; 而不是默认的fastcgi_split_path_info ^(.+?\.php)(/.*)$;(尽管我根本不会使用那个 sn-p 文件)。
猜你喜欢
  • 2021-06-06
  • 2017-11-10
  • 2019-06-09
  • 2019-01-11
  • 2012-01-28
  • 2021-07-08
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多