如何为 HTTP 基本身份验证指定白名单(除了某些路径需要密码)?

How to specify a whitelist for HTTP basic authentication (to except certain paths from requiring a password)?

这是我当前配置的重要部分,我在其中使用 HTTP 基本身份验证保护我的整个网站:

<VirtualHost *:443>
  <Location "/">
    AuthType Basic
    AuthName "Protected Area"

    AuthBasicProvider file
    AuthUserFile /path/to/passwords_file

    Require valid-user
  </Location>
</VirtualHost>

但是,我想排除某些路径,以便它们公开可用,特别是 robots.txtfavicon.icomanifest.json。如何做到这一点?

在现有的 Location 之后添加以下配置块将不需要密码的指定路径除外:

<Location ~ "^/favicon\.ico$|^/manifest\.json$|^/robots\.txt$">
    Require all granted
</Location>

这对所有文件使用单个正则表达式匹配字符串,但您也可以单独指定每个文件,一次一个,而不使用正则表达式:

<Location "/favicon.ico">
    Require all granted
</Location>

<Location "/manifest.json">
    Require all granted
</Location>

<Location "/robots.txt">
    Require all granted
</Location>