对于使用 PHP 作为 apache 模块的 Web 服务器:
AddType application/x-httpd-php .html .htm
对于将 PHP 作为 CGI 运行的 Web 服务器:
AddHandler application/x-httpd-php .html .htm
我有一个 Nginx 服务器,我想将 .js 文件和 .htm 文件作为 PHP 运行,所以我将在其中包含完整的 PHP 代码。任何人都知道如何配置 Nginx 来做到这一点?
对于使用 PHP 作为 apache 模块的 Web 服务器:
AddType application/x-httpd-php .html .htm
对于将 PHP 作为 CGI 运行的 Web 服务器:
AddHandler application/x-httpd-php .html .htm
我有一个 Nginx 服务器,我想将 .js 文件和 .htm 文件作为 PHP 运行,所以我将在其中包含完整的 PHP 代码。任何人都知道如何配置 Nginx 来做到这一点?
传递给 fastcgi 对我不起作用。经过几个小时的搜索,我在这里找到了解决方案:http: //ffct.cc/solving-nginx-php-fpm-access-denied-issue/
简而言之:
由于 PHP 版本 > 5.3.8,要使其正常工作,您应该在 php-fpm.conf 中添加指令:
security.limit_extensions = .php .html .js
识别标志是“拒绝访问”。(请注意,它与 HTTP 错误 403 不同)在访问.html
或.js
文件时。
简单的; 只是改变
location ~ \.php$ {
root html;
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|html|htm)$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
.htm、.html 文件的示例
location ~ \.htm$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.htm;
include fastcgi.conf;
}
.js 文件的示例
location ~ \.js$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
如果需要,只需更改扩展名和端口设置
多年后回复这个帖子,我花了三个小时试图解决这个问题,我在我的位置块(php|html|htm
)和 PHP-FPM(security.limit_extensions
)中都有所有正确的设置,我的问题是我已经有了 .html 扩展名以前用于某些静态文件的位置,因此,如果其他人在使用正确的设置时遇到问题,请确保您没有犯与我一样的愚蠢错误 :)
汤姆通过链接回答:
http://ffct.cc/solving-nginx-php-fpm-access-denied-issue/
真的很有帮助。但是,我使用 php 和 php-fpm 安装在 mac os yosemite w/ homebrew 上。在我将以下内容添加到我的 .bash_profile 之前,对 php-fpm.conf 文件的更改不会生效:
# for homebrew php55
export PATH="/usr/local/sbin:$PATH"
详情见:
brew info php55
在带有 PHP 8.0 的 macOS 11.5 中,当我浏览 http://localhost/index.html 时,它以403
Forbidden结尾
我不得不更改文件/usr/local/etc/php/8.0/php-fpm.d/www.conf
来设置
security.limit_extensions = .php .html
我的 .html 文件中的 PHP 代码块已正确执行,以下是我在 nginx.conf
文件中的服务器块
server {
listen 80;
server_name example.local;
root "/var/www/html";
index index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.html$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}