如何路由.html和.js文件作为PHP在Nginx FastCGI服务器


How to route .html and .js files as PHP on Nginx FastCGI server?

对于使用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扩展位置用于一些静态文件,如果任何人有问题尽管使用正确的设置,请确保你没有犯同样愚蠢的错误我做:)

Tom的回答:

http://ffct.cc/solving-nginx-php-fpm-access-denied-issue/

真的很有用。然而,我使用php与php-fpm安装在mac os yosemite w/homebrew。对php-fpm.conf文件的更改直到我在.bash_profile中添加以下内容才生效:

 # for homebrew php55
 export PATH="/usr/local/sbin:$PATH"

详细信息请参见:

brew info php55

在macOS 11.5和PHP 8.0中,当我浏览http://localhost/index.html时,它以403 Forbidden结束

我必须更改文件/usr/local/etc/php/8.0/php-fpm.d/www.conf来设置

security.limit_extensions = .php .html
PHP代码块在我的。html文件正确执行,以下是我的服务器块在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;
  }
}