Apache-2.2

Apache VirtualHost 設置… 502 Bad Gateway

  • February 16, 2014

我正在嘗試在 Ubuntu 12.04 lts 上設置 apache 虛擬主機。我在這裡遵循了本指南http://www.debian-administration.org/articles/412 ,但我使用的是 /var/www 而不是 /home/www。我已將文件根文件夾設置為 755 權限。這是我家的伺服器,所以我認為這可能是我的路由器有問題嗎?我沒有使用代理。是否需要設置某種方式的埠轉發來執行虛擬主機?(是的,它在虛擬主機之前執行良好。我收到 502 響應錯誤(網關錯誤)。這是我啟用的兩個站點

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.functioncreate.com
   ServerAlias functioncreate.com

   # Indexes + Directory Root.
   DirectoryIndex index.html
   DocumentRoot /var/www/functioncreate/htdocs/

   # CGI Directory
   ScriptAlias /cgi-bin/ /var/www/functioncreate/cgi-bin/
   <Location /cgi-bin>
           Options +ExecCGI
   </Location>

   # Logfiles
   ErrorLog  /var/www/fucntioncreate/logs/error.log
   CustomLog /var/www/functioncreate/logs/access.log combined
</VirtualHost>

第二個:

#
#  Example.com (/etc/apache2/sites-available/www.example.com)
#
<VirtualHost *:80>
   ServerName www.labelleviemodesto.com
   ServerAlias labelleviemodesto.com

   # Indexes + Directory Root.
   DirectoryIndex index.html
   DocumentRoot /var/www/labellevie/htdocs/

   # CGI Directory
   ScriptAlias /cgi-bin/ /var/www/labellevie/cgi-bin/
   <Location /cgi-bin>
           Options +ExecCGI
   </Location>


   # Logfiles
   ErrorLog  /var/www/labellevie/logs/error.log
   CustomLog /var/www/labellevie/logs/access.log combined
</VirtualHost>

我的 httpd.conf 裡面什麼都沒有。我嘗試過啟用和禁用“預設”站點。我在 apache 中唯一接觸過的東西是我在 sites-available 中創建的新站點,然後通過 a2ensite 符號連結到啟用的站點(然後是 apache 重新載入),然後在 conf.d 中創建 virtual.conf ,其中包含以下內容:

#
#  We're running multiple virtual hosts.
#
 NameVirtualHost *

這是我的錯誤日誌

Warning: DocumentRoot [/var/www/functioncreate] does not exist
Warning: DocumentRoot [/var/www/labellevie] does not exist
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Wed Feb 12 23:40:40 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
[Wed Feb 12 23:40:40 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Wed Feb 12 23:44:55 2014] [notice] Graceful restart requested, doing restart
Warning: DocumentRoot [/var/www/functioncreate] does not exist
Warning: DocumentRoot [/var/www/labellevie] does not exist
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Wed Feb 12 23:44:55 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
[Wed Feb 12 23:44:55 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Wed Feb 12 23:46:06 2014] [notice] Graceful restart requested, doing restart
Warning: DocumentRoot [/var/www/labellevie] does not exist
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Wed Feb 12 23:46:06 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
[Wed Feb 12 23:46:06 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Wed Feb 12 23:46:29 2014] [error] [client 10.0.0.14] File does not exist: /var/www/functionCreate/favicon.ico
[Thu Feb 13 00:22:45 2014] [notice] Graceful restart requested, doing restart
Warning: DocumentRoot [/var/www/labellevie] does not exist
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Thu Feb 13 00:22:45 2014] [warn] _default_ VirtualHost overlap on port 80, the first has precedence
[Thu Feb 13 00:22:45 2014] [warn] _default_ VirtualHost overlap on port 80, the first has precedence
[Thu Feb 13 00:22:45 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
[Thu Feb 13 00:22:45 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
(2)No such file or directory: apache2: could not open error log file /var/www/fucntionCreate/logs/error.log.
Unable to open logs
[Thu Feb 13 12:03:37 2014] [warn] pid file /var/run/apache2.pid overwritten -- Unclean shutdown of previous Apache run?
[Thu Feb 13 12:03:37 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Thu Feb 13 12:03:57 2014] [notice] caught SIGTERM, shutting down
[Thu Feb 13 12:03:58 2014] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 with Suhosin-Patch configured -- resuming normal operations
[Thu Feb 13 12:04:23 2014] [error] [client 166.137.191.15] File does not exist: /var/www/favicon.ico
[Thu Feb 13 12:04:57 2014] [error] [client 166.137.191.15] File does not exist: /var/www/favicon.ico
[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php
[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php5
[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php-cgi

[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php5
[Thu Feb 13 12:05:40 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php-cgi
[Thu Feb 13 12:05:41 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php.cgi
[Thu Feb 13 12:05:41 2014] [error] [client 186.251.208.20] script not found or unable to stat: /usr/lib/cgi-bin/php4
[Thu Feb 13 12:09:18 2014] [notice] Graceful restart requested, doing restart
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Thu Feb 13 12:09:19 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
(2)No such file or directory: apache2: could not open error log file /var/www/fucntionCreate/logs/error.log.
Unable to open logs

我有幾個提示:

檢查路由器兩側的行為是否存在差異。假設您的伺服器的內部 IP 是 192.168.0.10。編輯您的測試機器上的主機文件(Mac 上的 /etc/hosts 和 Windows 上的 c:\windows\system32\driver\etc\hosts - 記住在 Mac 上使用 sudo 或在 Windows 上“以管理員身份執行”,否則你不會無法編輯文件)並向其中添加以下行(將 192.168.0.10 替換為您的實際內部/家庭網路伺服器 IP):

192.168.0.10    www.labelleviemodesto.com labelleviemodesto.com
192.168.0.10    www.functioncreate.com functioncreate.com

ping 所有這些域名以確保您訪問正確的 IP。nslookup 不起作用,因為它只會查詢 DNS,這將為您提供伺服器的正確(外部)IP 並忽略主機文件覆蓋。Ping 和所有其他程序將尊重主機文件覆蓋。顯然,您需要在您的私人(家庭)網路內部執行此操作,例如在 wi-fi 上。如果 ping 以所有 4 個域的內部 IP (192.168.0.10) 為目標(這很重要,因為例如 functioncreate.com 可能會重定向到 www.functioncreate.com,反之亦然,因此您要確保無論什麼,您直接與內部 IP 通話,中間沒有路由器)。

無論如何,底線:看看你是否仍然得到 502 錯誤。如果你這樣做了,那麼它很可能來自 Apache,並且你的配置文件有問題。如果通過私有 IP 可以正常工作,但是當您從外部通過路由器訪問此伺服器時,您會收到 502 錯誤,那麼問題出在您的路由器配置上。

我的猜測:這是路由器。您的 Apache 配置中似乎沒有任何類似反向代理的東西。

最後說明:確保在所有 .conf 文件中只有一個且只有一個NameVirtualHost行。如果您有多個,或者如果您的VirtualHost參數與您的****NameVirtualHost參數不完全對應,您將得到奇怪的、意想不到的結果。例如,由於您已經定義了所有虛擬主機,如下所示:

VirtualHost *:80

確保所有 Apache 配置文件中唯一的 NameVirtualHost 行如下所示:

NameVirtualHost *:80

您不應該在日誌中看到這樣的行!

[Wed Feb 12 23:46:06 2014] [warn] NameVirtualHost *:80 has no VirtualHosts

當然,這不是一個直接的答案,但我希望它能讓你更接近答案。:)

引用自:https://serverfault.com/questions/575541