Apache用户指导手册翻译(1)--设置监听IP地址和端口

Apache默认监听所有IP的Web端口,但可通过`Listen`指令指定监听特定IP地址和端口。本文详细介绍了如何配置Apache,包括监听多个端口、特定IP和IPv6相关注意事项,以及在虚拟主机环境中使用这些设置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

绑定(Binding)
                                                                       --设置Apache监听特定的IP地址和端口 


一、概要
  当Apache起动后,它默认监听本机所有Ip地址的与Web相关的端口,然而我们可以告诉Apache去监听特定的端口或者只监听特定的IP上的的特定的端口.这个设置经常与需拟主机配合使用,通过配置告诉需拟主机来响应不同的IP地址、域名和端口上的Http请求。
  Linsten指令告诉Apache服务器接受特定IP地址的特定端口的Http请求,或所有Ip地址上的特定端口的Http请求,如果Listen后指定了一个特定的端口号,那么apache服务器则监听所有网卡的这个端口.如果Listen指令后也同时设置了IP地址和端口号,那么服务器则监听这个指定的网卡上的端口。可以设置多个Listen指令来指定监听多个IP和端口, 服务器将根据Listen指令设置的Ip地址和端口来响应不同的请求
   例如:
     如果让服务器接受80和8000端口的请求,使用:
     Listen 80
     Listen 8000
     为了让服务器接受特定网卡上的指定的端口上的请求,使用:
     Listen 192.170.2.1:80
     Listen 192.170.2.5:8000
     IPv6地址需要使用方括号括起来,如:
     Listen [2001:db8::a00:fea7:ccea]:80

二、IPv6需要注意的事项
  越来越多的平台实现了对IPv6的支持,Apache中APR模块也支持大部分已经实现了IPv6的平台,Apache通过APR指定IPv6的套接字来处理通过IPv6发送来Http请求。
  对于Apache管理员来说,使用IPv6也带来了一个问题:IPv6的sccket在处理ipv6的请求时,是否也能同时处理IPv4的请求。大部分平台是允许通过IPv6 socket的IPv4映射Ipv6地址来处理IPv4的请求的,但在FreeBSD,NetBSD和OpenBSD平台上,由于受系统策略的影响,这种IPv4映射Ipv6地址的方式是不允许的。 即使系统默认不允许使用IPv4映射Ipv6地址的方式,我们可以通过设置特定的配置参数来让Apache实现这种技术。
  另一方面,在部分平台上比如Linux和Tru64上,只能使用IPv6映射IPv4地址的方式来同时处理IPv6和IPv4的请求,果你想让Apache使用最少的Socket连接来同时处理IPv6和IPv4的请求,需要使用--enabel-v4-mapped 的配置参数。
  Apache在构建时--enabel-v4-mapped在所有的平台上默认是on,除了FreeBSD,NetBSD和OpenBSD之外。如果你只想让Apache处理IPv4的连接,不用管APR是否支持你合用的平台,直接在Listen指令后设置IP地址就可以了,例如:
     Listen:0.0.0.0:80
     Listen 192.170.2.1:80
     如果你使用的平台支持APR并且你想让Apache通过各自的Socket处理IPv4和IPv6连接(如:禁用IPv4-mapped address),设置 --disabel-v4-mapetv4-mapped 在FreeBSD、NetBSD和OpenBSD上默认的设置是on

三、在需拟主机里面如何使用绑定
  Listen指令没有实现对需拟主机的支持,它只能告诉Apache服务器监听哪一个IP地址和端口,如果没有使用<VitrualHost>指令,那么服务器将使用相同的动作接受Listen指令设置的IP和端口上的请求。然而,<VirtualHost>指令能够指定对于一个或多个IP或者端口上的请求做出特定的动作。为了实现一个需拟主机,首先要告诉服务器监听的所有IP和端口,然后使用<VirtualHost>片断来设置该需拟主机对其中特定的地端口上的请求做出响应。注意:如果<VirtualHost>中设置了没有通过Listen指定的IP地址和端口,那么Apache是不会接受该请求的。

韩永涛
      2007-11-26


原文如下:

Binding
  Configuring Apache to listen on specific addresses and ports

Overview

  When Apache starts, it binds to some port and address on the local machine and waits for incoming requests. By default, it listens to all addresses on the machine. However, it needs to be told to listen on specific ports, or to listen on only selected addresses, or a combination. This is often combined with the Virtual Host feature which determines how Apache responds to different IP addresses, hostnames and ports.
The Listen directive tells the server to accept incoming requests only on the specified port or address-and-port combinations. If only a port number is specified in the Listen directive, the server listens to the given port on all interfaces. If an IP address is given as well as a port, the server will listen on the given port and interface. Multiple Listen directives may be used to specify a number of addresses and ports to listen on. The server will respond to requests from any of the listed addresses and ports.
  For example, to make the server accept connections on both port 80 and port 8000, use:
  Listen 80
  Listen 8000
  To make the server accept connections on two specified interfaces and port numbers, use
  Listen 192.170.2.1:80
  Listen 192.170.2.5:8000
  IPv6 addresses must be surrounded in square brackets, as in the following example:
  Listen [2001:db8::a00:20ff:fea7:ccea]:80

Special IPv6 Considerations
  A growing number of platforms implement IPv6, and APR supports IPv6 on most of these platforms, allowing Apache to allocate IPv6 sockets and handle requests which were sent over IPv6.One complicating factor for Apache administrators is whether or not an IPv6 socket can handle both IPv4 connections and IPv6 connections. Handling IPv4 connections with an IPv6 socket uses IPv4-mapped IPv6 addresses, which are allowed by default on most platforms but are disallowed by default on FreeBSD, NetBSD, and OpenBSD in order to match the system-wide policy on those platforms. But even on systems where it is disallowed by default, a special configure parameter can change this behavior for Apache.On the other hand, on some platforms such as Linux and Tru64 the only way to handle both IPv6 and IPv4 is to use mapped addresses. If you want Apache to handle IPv4 and IPv6 connections with a minimum of sockets, which requires using IPv4-mapped IPv6 addresses, specify the --enable-v4-mapped configure option.--enable-v4-mapped is the default on all platforms but FreeBSD, NetBSD, and OpenBSD, so this is probably how your Apache was built.

  If you want Apache to handle IPv4 connections only, regardless of what your platform and APR will support, specify an IPv4 address on all Listen directives, as in the following examples:
  Listen 0.0.0.0:80
  Listen 192.170.2.1:80

  If your platform supports it and you want Apache to handle IPv4 and IPv6 connections on separate sockets (i.e., to disable IPv4-mapped addresses), specify the --disable-v4-mapped configure option. --disable-v4-mapped is the default on FreeBSD, NetBSD, and OpenBSD.

  How This Works With Virtual Hosts
  Listen does not implement Virtual Hosts. It only tells the main server what addresses and ports to listen to. If no <VirtualHost> directives are used, the server will behave the same for all accepted requests. However, <VirtualHost> can be used to specify a different behavior for one or more of the addresses and ports. To implement a VirtualHost, the server must first be told to listen to the address and port to be used. Then a <VirtualHost> section should be created for a specified address and port to set the behavior of this virtual host. Note that if the <VirtualHost> is set for an address and port that the server is not listening to, it cannot be accessed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值