大网数据广告活动
服务器运维问答

Dynamic DocumentRoot Based on SSL (Client Auth.)

· 52CE 我爱测

I currently use Client SSL authentication to protect sensitive content. Users are required to import a certificate into their browsers that we generated using OpenSSL. When a user visits the website, we redirect them to a custom …

问题原文

原帖链接 · Ralph · CC BY-SA 4.0

I currently use Client SSL authentication to protect sensitive content. Users are required to import a certificate into their browsers that we generated using OpenSSL. When a user visits the website, we redirect them to a custom 403 page if they don't have the certificate in their browser or operating system's keychain. This is achieved using the following Apache configuration.

<VirtualHost *:443>
    ServerName host.tld
    ServerAlias www.host.tld

    ServerAdmin [email omitted]
    DocumentRoot /var/www/path

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on
    SSLProtocol             all -SSLv2 -SSLv3

    SSLCertificateFile /etc/ssl/host/host.crt
    SSLCertificateKeyFile /etc/ssl/host.key
    SSLCACertificateFile /etc/ssl/cacert.pem
    SSLVerifyClient optional
    SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
    SSLVerifyDepth 10
#   SSLCARevocationPath /etc/ssl/crl
    SSLCARevocationFile /etc/ssl/ca.crl
    SSLCARevocationCheck chain

    RewriteEngine        on
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond     %{SSL:SSL_CLIENT_VERIFY} !=SUCCESS
    RewriteRule     .? - [F]
    ErrorDocument 403 /403.html
    
</VirtualHost>

I would like to take this a step further and introduce some conditionals to load a specific DocumentRoot if they have the certificate present, or another DocumentRoot if they don't have the client certificate.

In summary: website.com

  • User A visits with client SSL, serve website.com from /var/www/has_ssl
  • User B visits without client SSL, serve website.com from /var/www/no_ssl

I can use %{SSL:SSL_CLIENT_S_DN_CN} ="" to redirect to a path if empty, but I'm not sure how to rewrite the DocumentRoot based on the presence of the certificate.

I've been struggling with this for a few days. Any assistance or pointers is appreciated.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

AFAIK you can't conditionally set the DocumentRoot. However, you could internally rewrite the request using mod_rewrite, in much the same way you are returning a 403 response when the client SSL is not set.

For example, if you set:

DocumentRoot /var/www

Then internally rewrite all requests to /var/www/has_ssl on success or /var/www/no_ssl otherwise.

RewriteEngine on

RewriteCond %{SSL:SSL_CLIENT_VERIFY} !=SUCCESS
RewriteRule ^ /no_ssl%{REQUEST_URI} [L]

RewriteCond %{SSL:SSL_CLIENT_VERIFY} =SUCCESS
RewriteRule ^ /has_ssl%{REQUEST_URI} [L]

To prevent direct access to either the /no_ssl or /has_ssl subdirectories you could implement an external redirect before the above rewrites that removes the subdirectory from the request. For example:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^/(no_ssl|has_ssl)(.*) /$1 [R=301,L]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-09-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。