为什么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

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

更多信息:

  • Mac操作系统X:10.8.3
  • Apache服务器版本:Apache/2.2.22(Unix(
  • Apache服务器构建时间:2012年12月9日18:57:18
  • Firefox:48.0.2
  • image/css/js文件的文件权限都设置为chmod 644

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

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

这看起来有点不可靠。

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

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

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

试错(猜测和检查(终于让我找到了答案。这是配置文件中有问题的一行。我现在没有删除这行,而是注释了它,提醒我再也不要使用这行了!

AddHandler cgi-script .pl .cgi
# ENABLING THE FOLLOWING LINE WILL MAKE _EVERY_ FILE EXECUTE!!!
#      SetHandler cgi-script      

那个SetHandler cgi-script是纯毒药。我从来没有想到——它看起来如此无害、无辜,甚至是必要的。

如果没有这条线,一切都会像预期的那样重新开始。图片、css和js文件的http响应200现在看起来非常漂亮!

最新更新