[英]Why is apache2 trying to execute my CSS and image files?
我為此苦苦掙扎了兩天,在網上搜索線索和答案,發現很少有適用於 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 會這樣做? 配置有什么問題?
嗯,這看起來有點狡猾。
<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 現在看起來很漂亮!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.