强制Apache2 Web服务器侦听单个外部IP

我试图强制Apache2 Web服务器侦听属于HAProxy( 192.168.50.30 )的单个外部IP,因此用户必须通过HAProxy来使用Apache2 Web服务器。 目前(不幸的是)用户可以通过http://192.168.50.10http://192.168.50.30访问Apache2 web服务器(只有这样才能被允许)。

  • HAProxy IP: 192.168.50.30
  • Apache2 IP: 192.168.50.10

最常见的post说,更改Listen *:80/etc/apache2/ports.confListen IP-ADDRESS:80会诀窍,但是当我尝试重新启动apache2时出现错误。

 (99)Cannot assign requested address: AH00072: make_sock: could not bind to address 192.168.50.30:80 no listening sockets available, shutting down 

我目前的设置

/etc/apache2/ports.conf

 Listen 192.168.50.30:80 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule> 

/etc/apache2/sites-available/000-default.conf

 <VirtualHost *:80> DocumentRoot /var/www/html </VirtualHost> 

/etc/apache2/apache2.conf中

 <Directory /> Options FollowSymLinks AllowOverride None Require all denied </Directory> <Directory /usr/share> AllowOverride None Require all granted </Directory> <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> 

/ etc / hosts文件

 127.0.0.1 webserver1 webserver1 127.0.0.1 localhost ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts 

$ ifconfig -a

 eth0 Link encap:Ethernet HWaddr 08:00:27:b5:33:dd inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:feb5:33dd/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:63695 errors:0 dropped:0 overruns:0 frame:0 TX packets:13588 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:75453727 (75.4 MB) TX bytes:983784 (983.7 KB) eth1 Link encap:Ethernet HWaddr 08:00:27:9a:05:45 inet addr:192.168.50.10 Bcast:192.168.50.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe9a:545/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8721 errors:0 dropped:0 overruns:0 frame:0 TX packets:8392 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:656568 (656.5 KB) TX bytes:872702 (872.7 KB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) 

我会使用访问控制 。 首先确保您的Listen语句全部列出了1个IP地址(但这可能并不重要)。 然后将访问控制configuration为只允许来自HA代理地址的访问

 Order deny,allow Deny from all Allow from 192.168.50.30 

或类似的东西只会允许从haproxy地址访问。

你只能绑定到在ifconfig中显示的IP(即本地IP)。 这听起来好像你想限制Apache只响应HAProxy连接到它。

您可以使用Apache主机上的IPTables执行此操作:

 iptables -A INPUT -s 192.168.1.30 -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 80 -j REJECT 

这表示允许从192.168.1.30连接到端口80,并拒绝所有其他连接。

事先做'iptables -L'来查看是否有其他规则,这可能会改变你添加这些规则的细节。

另一个select是使用Apache访问控制来做同样的事情: https : //httpd.apache.org/docs/2.4/howto/access.html#host

 <RequireAll> Require all granted Require ip 192.168.1.30 </RequireAll> 

你可以像下面这样修改“/etc/apache2/sites-available/000-default.conf”configuration文件。

 <VirtualHost *:80> to <VirtualHost 192.168.50.30:80> 

它会解决你的问题。