多个 IP 对应不同的域 Apache2

多个 IP 对应不同的域 Apache2

我以前曾设法做到这一点,没有任何问题,但现在我陷入困境。

我遇到了以下问题:我的 Ubuntu VPS 连接了两个 IP。111.111.11.111 和 222.222.22.222。111.111.111.111 是服务器的“默认”IP。222.222.22.222 是辅助 IP,我尝试将我的站点 example.com 添加到该 IP。奇怪的是,当我尝试浏览或 ping 我的 example.com 站点时,它会超时,无论是在域上还是在 IP 上,数据包丢失率 100%。

当我浏览 111.111.11.111 时,我到达了我的网站 example.com(显示内容),而该网站应该只显示在 222.222.22.222 上。为什么会发生这种情况?我错过了什么?

系统Ubuntu18.04 Apache2.4.29

  • 站点 000-默认已禁用。
  • 服务器上的所有网站文件都在 /var/www/example.com/public_html/ 中(如果我浏览,111.111.111.111 也会到达这里)

/etc/hosts

127.0.0.1 localhost
127.0.0.1 the-server-name
222.222.22.222 example.com
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

/etc/apache2/sites-enabled/example.com.conf

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/public_html/

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

编辑 1:首先查看 /etc/network/interfaces 得到了这个:

# ifupdown has been replaced by netplan(5) on this system.  See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
#    sudo apt install ifupdown

因此,当转到 /etc/netplan 时,会出现一个链接的 .yaml 文件,我在其中添加了 222.222.22.222 IP,现在它看起来像这样:

network:
    version: 2
    renderer: networkd
    ethernets:
        ens192:
            match:
                macaddress: '02:01:84:66:71:01'
            set-name: ens192
            gateway4: 111.111.11.111
            gateway6: '2a02:0750:0009:0000:0000:0000:0000:0001'
            addresses:
                - 111.111.11.111/23
                - '2a02:750:9::3c9/64'
                - 222.222.22.222/23
            nameservers:
                addresses:
                    - 33.33.3.300
                    - 33.33.3.301
                    - '2a02:751:aaaa::1'
                    - '2a02:751:aaaa::2'

完成此操作后,我可以 ping 和浏览 222.222.22.222。我想我会解决剩下的问题。谢谢你的帮助!

编辑 2:我以为我会解决其余的问题,但这里也遇到了困难。我可以通过所述 IP 访问该网站,但不能通过域名访问。尽管 Apache access.log 记录了每个尝试访问该网站的访问者。

为什么我会收到“连接被拒绝”的消息?

netstat -lntp |复制代码

tcp6       0      0 :::80                   :::*                    LISTEN      724/apache2   

/var/log/apache2/error.log

[Tue Nov 20 21:30:12.537727 2018] [mpm_prefork:notice] [pid 7538] AH00169: caught SIGTERM, shutting down
[Tue Nov 20 21:30:12.627503 2018] [mpm_prefork:notice] [pid 7720] AH00163: Apache/2.4.29 (Ubuntu) configured -- resuming normal operations
[Tue Nov 20 21:30:12.627574 2018] [core:notice] [pid 7720] AH00094: Command line: '/usr/sbin/apache2'
[Tue Nov 20 21:31:01.605035 2018] [php7:warn] [pid 7746] [client 58.182.34.135:35914] PHP Warning:  mysqli_real_connect(): (HY000/1698): Access denied for user 'root'@'localhost' in /var/www/example.com/public_html/wp-includes/wp-db.php on line 1531
[Tue Nov 20 21:34:15.751251 2018] [mpm_prefork:notice] [pid 7720] AH00169: caught SIGTERM, shutting down
[Tue Nov 20 21:34:15.840563 2018] [mpm_prefork:notice] [pid 7883] AH00163: Apache/2.4.29 (Ubuntu) configured -- resuming normal operations
[Tue Nov 20 21:34:15.840667 2018] [core:notice] [pid 7883] AH00094: Command line: '/usr/sbin/apache2'
[Tue Nov 20 21:38:03.325023 2018] [mpm_prefork:notice] [pid 7883] AH00169: caught SIGTERM, shutting down
[Tue Nov 20 21:38:15.988516 2018] [mpm_prefork:notice] [pid 724] AH00163: Apache/2.4.29 (Ubuntu) configured -- resuming normal operations
[Tue Nov 20 21:38:16.003974 2018] [core:notice] [pid 724] AH00094: Command line: '/usr/sbin/apache2'

答案1

您不能使用<VirtualHost *:80>来实现此目的。如http://httpd.apache.org/docs/2.4/vhosts/ip-based.html“对于每个基于 IP 的虚拟主机,服务器必须具有不同的 IP 地址/端口组合”。该文档中还提供了如何正确执行此操作的示例。

相关内容