首页 > 解决方案 > Apache 不能在 localhost 以外的网站上运行

问题描述

VirtualHost 文件仅适用于 localhost 用户,而不适用于任何 ip,无论是公共的、私有的还是自己的。这是放在sites-enabled文件夹中的 VirtualHost 文件内容:

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin root@localhost

    DocumentRoot /home/matecat/cattool
    DirectoryIndex index.php index.php3 index.html index.htm index.shtml
    <Directory /home/matecat/cattool/>
        Options All
        AllowOverride All

       <IfVersion < 2.4>
               Order allow,deny
               Allow from all
       </IfVersion>
       <IfVersion >= 2.4>
               Require all granted
       </IfVersion>

        ExpiresActive On
        ExpiresByType text/html "access plus 1 hour"
        ExpiresByType text/css "access plus 1 week"
        ExpiresByType text/javascript "access plus 1 week"
        ExpiresByType image/gif "access plus 1 week"
        ExpiresByType image/jpeg "access plus 1 week"
        ExpiresByType image/jpg "access plus 1 week"
        ExpiresByType image/png "access plus 1 week"
        ExpiresByType image/vnd.microsoft.icon "access plus 1 week"
        ExpiresByType image/ico "access plus 1 week"
        ExpiresByType application/x-shockwave-flash "access plus 1 week"
    </Directory>
    php_flag register_globals off
    php_flag magic_quotes_gpc off
    php_admin_flag short_open_tag on
    php_value memory_limit 1024M
    php_value upload_max_filesize 200M
    php_value post_max_size 200M
    LogLevel error
    ErrorLog /var/log/apache2/matecat.error.log
    CustomLog /var/log/apache2/matecat.log combined
    ServerSignature Off

    # Configuration for Nodejs proxy
    ProxyRequests off

    <Proxy *>
      Order deny,allow
      Allow from all
    </Proxy>

    <Location /sse/ >
      ProxyPass http://0.0.0.0:7788/
      ProxyPassReverse http://0.0.0.0:7788/
    </Location>

</VirtualHost>

我注释掉了以下几行:

Servername localhost
ServerAdmin root@localhost

但是在重新启动apache服务器后,使用系统的“IPv4”仍然无法正常工作。

标签: apachevirtualhost

解决方案


如果您没有指定域,并通过 IP 访问它,Apache 将使用它找到的第一个匹配的 VirtualHost,自上而下。

这里,任何 IP (*),端口 80。所以如果默认配置 VirtualHost 没有被注释掉,Apache 将使用这个默认配置。所以注释掉默认的,或者把你的移到默认的。但是无论如何都没有人需要该默认配置:-)


推荐阅读