【问题标题】:HTML files as PHP in Nginx在 Nginx 中作为 PHP 的 HTML 文件
【发布时间】:2012-01-28 12:13:21
【问题描述】:

对于使用 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 来做到这一点?

【问题讨论】:

  • 你确定这是一个明智的想法吗?这意味着对于 HTML 或 JS 文件的每个请求,都将启动 PHP 解释器。为此使用 URL 重写可能会节省更多资源
  • 你能解释一下想要这样做的原因吗? (这是一个可能的安全风险和服务器上的拖累)。
  • @ikano 这是一个性能问题,但我看不出这是一个安全风险,你能详细说明一下吗? (编辑:啊,我想你的意思是代码可能会在任意上传的文件中执行)
  • 没错,上传文件中的代码执行 :-)
  • 我将在子域上提供它。基本上我正在为客户创建一个广告服务器。他们希望向他们的网站提供广告代码。所以ad.nginx.com/serve.js?client=2343&zone=2&banner=22例子

标签: .htaccess nginx addhandler


【解决方案1】:

传递给 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 文件时。

【讨论】:

    【解决方案2】:

    简单;只是改变

    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;
        }
    

    【讨论】:

    • location ~ \.php|\.html|\.htm$ { ... 也可以。
    • 另一个简单的 +1。
    【解决方案3】:

    .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;
    }
    

    如果需要,只需更改扩展和端口设置

    【讨论】:

    • 能不能限制子域。只有 .JS 文件我想在选择文件夹区域中执行此操作。
    • 嗯,我的所有 js 文件现在都得到 502。
    【解决方案4】:

    多年后回复这个帖子,我花了三个小时试图解决这个问题,我在我的位置块 (php|html|htm) 和 PHP-FPM (security.limit_extensions) 中设置了所有正确的设置,我的问题是我在以前的位置已经有了 .html 扩展名,该扩展名用于一些静态文件,所以如果其他人在使用正确的设置后遇到问题,请确保您没有制作我犯的同样愚蠢的错误:)

    【讨论】:

    • 哇!这样一个新手的错误,我们都犯了这些。很棒的发现!如果不能错过,那将是非常痛苦和令人厌烦的。有趣的是,您不只是尝试使用 PHP 和 HTML 文件来查看它是否可以正常工作:)
    【解决方案5】:

    Tom 通过链接回答:

    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
    

    【讨论】:

      【解决方案6】:

      在 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
      

      我的 .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;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-01-08
        • 2021-03-16
        • 2016-08-16
        • 2012-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-06
        相关资源
        最近更新 更多