配置 apache 不靜態提供 php 文件,除非配置了 php-fpm

配置 apache 不靜態提供 php 文件,除非配置了 php-fpm

我有一個 apache 2.4 配置部分,用於處理罕見的情況,其中配置錯誤的 apache 框可能將 php 檔案作為靜態純文字提供,並可能放棄憑證等。

<IfModule !mod_php5.c>
  <IfModule !mod_php7.c>
    <Files "*.php">
        Require all denied
    </Files>
  </IfModule>
</IfModule>

這些指令檢查 php 模組是否存在,如果沒有找到其中任何一個,則不會提供頁面服務。

然而,在 Centos-8 的 httpd 2.4 目前版本中,php-fpm 是配置 php 處理程序的預設方法,而且它不會載入任何 php 模組:

<IfModule !mod_php5.c>
  <IfModule !mod_php7.c>
    # Enable http authorization headers
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch \.(php|phar)$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
  </IfModule>
</IfModule>

不幸的是, proxy_fcgi 模組是由 httpd 自動載入的,因此它不能作為 php-fpm 已配置(或未配置)的指示:

[root@web httpd]# rpm -q --whatprovides /usr/lib64/httpd/modules/mod_proxy_fcgi.so
httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64

根據醫生有一個HANDLER變數:

HANDLER 建立回應的處理程序的名稱

……應該會回到其中之一在內建處理程序中

default-handler: Send the file using the default_handler(), which is the handler used by default to handle static content. (core)
send-as-is: Send file with HTTP headers as is. (mod_asis)
cgi-script: Treat the file as a CGI script. (mod_cgi)
imap-file: Parse as an imagemap rule file. (mod_imagemap)
server-info: Get the server's configuration information. (mod_info)
server-status: Get the server's status report. (mod_status)
type-map: Parse as a type map file for content negotiation. (mod_negotiation)

但是,我嘗試匹配它沒有成功,例如這不起作用:

<FilesMatch \.(php|phar)$>
  <If "%{HANDLER} == 'default-handler'">
    Require all denied
  </If>
</FilesMatch>

任何建議表示讚賞。

編輯

我在安裝了 php-fpm 的情況下轉儲了變數 %{HANDLER} 的值,而不是像這樣:

<IfModule headers_module>
    Header  always set X-HANDLER "expr=%{HANDLER}"
</IfModule>

它會傳回以下標頭:

X-HANDLER: text/plain     # no php-fpm

X-HANDLER: proxy:unix:/run/php-fpm/www.sock|fcgi://localhost     # with php-fpm

所以我將測試更新為:

<IfModule !mod_php5.c>
  <IfModule !mod_php7.c>
    <If "%{HANDLER} == 'text/plain'">
      <Files "*.php">
          Require all denied
      </Files>
    </If>
  </IfModule>
</IfModule>

然而,這也不起作用

相關內容