【问题标题】:Why is Apache2 mod_cache serving a directory index instead of index.php?为什么 Apache2 mod_cache 提供目录索引而不是 index.php?
【发布时间】:2019-04-18 13:46:14
【问题描述】:

我有一个在 DigitalOcean droplet 上运行的多站点 WordPress 安装。我添加了 Apache 的 mod_cache 模块来缓存我网站上的内容,但我遇到了一个非常奇怪的问题。在我设置缓存后的第一个页面加载时,网站加载正常。但是,在随后的页面加载中,我得到的是目录索引而不是 index.php 被调用:

如果我清除缓存,下一页可以正常工作。 mod_cache 似乎正在缓存目录索引 HTML 页面,而不是 index.php 的渲染输出。

这是我的网站配置:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName jeremydormitzer.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/jeremydormitzer.com

    ErrorLog /var/log/jeremydormitzer.com/error.log
    CustomLog /var/log/jeremydormitzer.com/access.log combined

    <Directory /var/www/html/jeremydormitzer.com/>
        AllowOverride All
    </Directory>

    RewriteEngine on

    LoadModule cache_module modules/mod_cache.so
    <IfModule mod_cache.c>
        LoadModule cache_disk_module modules/mod_cache_disk.so
        <IfModule mod_cache_disk.c>
            CacheEnable disk /
            CacheDisable /wp-admin
        </IfModule>
        CacheLock on
        CacheLockPath "/tmp/mod_cache-lock"
        CacheLockMaxAge 5
        CacheDisable "http://security.update.server/update-list/"
    </IfModule>

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/qa.getpterotype.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/qa.getpterotype.com/privkey.pem
</VirtualHost>
</IfModule>

这是我的cache_disk.conf

<IfModule mod_cache_disk.c>
    CacheRoot /var/cache/apache2/mod_cache_disk
    CacheDirLevels 2
    CacheDirLength 1
</IfModule>

这是我的apache2.conf

Mutex file:${APACHE_LOCK_DIR} default    
PidFile ${APACHE_PID_FILE}
Timeout 300    
KeepAlive On    
MaxKeepAliveRequests 100    
KeepAliveTimeout 5   
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}    
HostnameLookups Off    
ErrorLog ${APACHE_LOG_DIR}/error.log    
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf    
Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/html>
    AllowOverride All
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf    
IncludeOptional sites-enabled/*.conf

ServerName 104.236.87.208    
UseCanonicalName On

有人知道这是怎么回事吗?


更新

根据要求,这是我网站根路径下.htaccess 的内容:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

【问题讨论】:

  • 使用了哪些重写规则?根目录下是否有 .htaccess 文件?
  • 另外,您是否尝试将日志级别设置为调试以查看是否可以为您提供任何额外信息?
  • 我发布了根.htaccess 文件。 This blog post 暗示 RewriteRule 可能导致 mod_cache 在错误的 URL 下缓存页面 - 这听起来准确吗?

标签: php apache apache2 lamp mod-cache


【解决方案1】:

不确定,但是当我看这里时https://httpd.apache.org/docs/2.2/mod/mod_cache.html 该示例有 mod_disk_cache 并且您的代码似乎有 mod_cache_disk?

【讨论】:

  • 他们在 2.3/2.4 中把名字改成了 mod_cache_disk
【解决方案2】:

我发现了问题所在。有两个:

问题 1

mod_cache 在服务器处理管道中运行得太早。这意味着它在调用 PHP 脚本之前缓存了内容,导致它改为缓存目录索引。为了解决这个问题,我在我的配置文件中关闭了CacheQuickHandler

CacheQuickHandler off

问题 2

我的 .htaccess 文件(由 WordPress 自动生成)正在将所有 URI 重写为 /index.php

RewriteRule . /index.php [L]

这导致每个页面都被缓存为index.php,这意味着每当您访问我网站上的页面时,Apache 都会提供最后访问的页面。

将其更改为将 URI 重写为 index.php/&lt;the path&gt; 解决了这个问题:

RewriteRule ^(.*)$ /index.php/$1 [L]

感谢this ServerFault answer 为我指明了正确的方向。

【讨论】:

  • 对后者有所怀疑,但似乎还有其他问题。很高兴你找到了它。
  • 你还在用mod_cache还是换成nginx/varnish反向代理了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 2010-12-22
相关资源
最近更新 更多