首页 > 解决方案 > 为什么 apache2 试图执行我的 CSS 和图像文件?

问题描述

我为此苦苦挣扎了两天,在网上搜索线索和答案,发现适用于 Mac 和 CGI​​ 脚本的内容很少。当我收到 500 错误时,可能有很多问题,但我认为它可能就像 apache2 试图执行我的非可执行文件一样简单,即 .css、.js、.png 等。我的配置文件(删除注释行)是这样的:

ServerRoot "/usr"

Listen 80

LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule setenvif_module libexec/apache2/mod_setenvif.so
LoadModule mime_module libexec/apache2/mod_mime.so
LoadModule autoindex_module libexec/apache2/mod_autoindex.so
LoadModule cgi_module libexec/apache2/mod_cgi.so
LoadModule negotiation_module libexec/apache2/mod_negotiation.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule alias_module libexec/apache2/mod_alias.so

User _www
Group _www

ServerAdmin you@example.com
DocumentRoot "/var/www"

<Directory />
        Options FollowSymLinks ExecCGI
        AllowOverride None
  </Directory>

<IfModule alias_module>
   ScriptAlias /cgi/ "/var/www/mywebsite/cgi/"
   AddHandler cgi-script .pl .cgi
      SetHandler cgi-script
</IfModule>

        <Directory /var/www/mywebsite/>
                Options +Indexes +FollowSymLinks -MultiViews 
                AllowOverride None
                Order allow,deny
                AddHandler cgi-script .cgi .pl
                allow from all
        </Directory>

        <Directory /var/www/mywebsite/*>
           AllowOverride AuthConfig
        </Directory>

        <Directory "/var/www/mywebsite/cgi/">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                AddHandler cgi-script .cgi .pl
                Order allow,deny
                Allow from all
        </Directory>
        
<IfModule dir_module>
    DirectoryIndex index.html index.htm index.php
</IfModule>

ErrorLog "/private/var/log/apache2/error_log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "/private/var/log/apache2/access_log" common
</IfModule>

DefaultType text/plain

<IfModule mime_module>
    TypesConfig /private/etc/apache2/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
</IfModule>

TraceEnable off

当我尝试加载我的页面时,Perl 脚本运行,但是嵌入到它提供给浏览器的 HTML 中的链接都出现 http 500 错误(如果我从基本目录的选项中删除了“ExecCGI”,则会出现 403 错误)。

以下是页面加载后日志文件显示的内容:




[Mon Jul 19 17:37:59 2021] [error] [client ::1] (8)Exec format error: exec of '/var/www/css/MW.css' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] Premature end of script headers: MW.css, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/new.gif' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] Premature end of script headers: new.gif, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_LA.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] Premature end of script headers: MW_lang_LA.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_US.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] Premature end of script headers: MW_lang_US.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW.js' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] Premature end of script headers: MW.js, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_TH.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] Premature end of script headers: MW_lang_TH.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_VT.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] Premature end of script headers: MW_lang_VT.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_SP.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:37:59 2021] [error] [client ::1] Premature end of script headers: MW_lang_SP.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] (8)Exec format error: exec of '/var/www/css/MW.css' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] Premature end of script headers: MW.css, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_US.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] Premature end of script headers: MW_lang_US.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_TH.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] Premature end of script headers: MW_lang_TH.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW.js' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] Premature end of script headers: MW.js, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_LA.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] Premature end of script headers: MW_lang_LA.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_SP.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] Premature end of script headers: MW_lang_SP.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/MW_lang_VT.png' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] Premature end of script headers: MW_lang_VT.png, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] (13)Permission denied: exec of '/var/www/css/new.gif' failed, referer: http://localhost/mywebsite/cgi/MW.pl
[Mon Jul 19 17:38:01 2021] [error] [client ::1] Premature end of script headers: new.gif, referer: http://localhost/mywebsite/cgi/MW.pl

如图所示,即使是图像文件也被视为需要执行的脚本!自然,他们会遇到语法错误。

更多信息:

Apache 仅在本地托管网站——这不是互联网虚拟主机。这应该是供离线使用和开发目的。

为什么apache2会这样做?配置有什么问题?

标签: macosapacheperlcgimime-types

解决方案


嗯,这看起来有点狡猾。

<Directory />
    Options FollowSymLinks ExecCGI
    AllowOverride None
</Directory>

首先,这会将您的整个文件系统(/ 下的所有内容)视为 Web 服务器的一部分。其次,您已经ExecCGI为 Apache 服务的每个文件都打开了 - 这会导致您的问题。

您还将实际的 CGI 目录 (/var/www/mywebsite/cgi/) 定义为 CGI 目录两次。


推荐阅读