设为首页 收藏本站
查看: 689|回复: 0

[经验分享] apache 虚拟主机 virtual host ,reverse proxy

[复制链接]

尚未签到

发表于 2015-8-3 09:19:16 | 显示全部楼层 |阅读模式
  http://httpd.apache.org/docs/2.2/vhosts/
  http://www.apacheweek.com/features/vhost
  http://www.neoease.com/apache-virtual-host/
  http://www.webopedia.com/TERM/V/virtual_host.html
  http://www.lccnet.com.tw/commercial/e-paper/200803/new-5.html
  http://www.apachetutor.org/admin/reverseproxies
  http://www.zhangling.org/blog/20070809/reverse-proxy%E9%85%8D%E7%BD%AE%E4%B8%AD%E7%9A%84%E8%B7%AF%E5%BE%84%E8%BD%AC%E6%8D%A2%E9%97%AE%E9%A2%98.html

Feature: Using Virtual Hosts
  One of the most important facilities in Apache is its ability to run 'Virtual Hosts'. This is now the essential way to run multiple web services - each with different host names and URLs - which appear to be completely separate sites. This is widely used by ISPs, hosting sites and content providers who need to manage multiple sites but do not want to buy a new machine for each one. In this issues we explain how to go about setting up a virtual host on your machine, what you need to do to get the hostname working, and how to configure Apache.
  

Picking an IP address
  There are two types of virtual hosts: IP-based and non-IP-based. The former is where each virtual host has its own IP address. You will need a new IP address for each virtual host you want to set up, either from your existing allocation or by obtaining more from your service provider. Once you have extra IP addresses, you tell your machine to handle them. On some operating systems, you can give a single ethernet interface multiple addresses (typically with anifconfig aliascommand). On other systems you will have to have a different physical interface for each IP address (typically by buying extra ethernet cards).
  IP addresses are a resource that costs money and are increasingly difficult to get hold of, so modern browsers can now also use 'non-IP' virtual hosts. This lets you use the same IP address for multiple host names. When the server receives an incoming Web connection it does not know the hostname what was used in the URL, however the new HTTP/1.1specification adds a facility where the browser must tell the server the hostname it is using, on theHost:header. If an older browser connects to a non-IP virtual host, it will not send the Host: header, so the server will have to respond with a list of possible virtual hosts. Apache provides some help for configuring a site for both old and new browsers.
  

Picking a Hostname and Updating the DNS
  Having selected an IP address, the next stage is to update the DNS so that browsers can convert the hostname into the right address. The DNS is the system that every machine connected to the Internet uses to find the IP address of host names. If your hostname is not in the DNS, no-one will be able to connect to your server (except by the unfriendly IP address).
  If the virtual host name you are going to use is under your existing domain, you can just add the record into your own DNS server. If the virtual host name is in someone else's domain, you will need to get them to add it to their DNS server files. In some cases, you will want to use a domain not yet used on the internet, in which case you will have to apply for the domain name from the InterNIC and set up the primary and secondary DNS servers for it, before adding the entry for your virtual host.
  In any of these cases, the entry you need to add to the DNS is an address record (an A record) pointing at the appropriate IP address. For example, say you want the domain www.my-dom.com to access your host with IP address 10.1.2.3: you will need to add the following line to the DNS zone file for my-dom.com:

  www         A     10.1.2.3

  Now users can enterhttp://www.my-dom.com/as a URL in their browsers and get to your web server. However it will return the same information as if the machine's original hostname had been used. So the final stage is to tell Apache how to respond differently to the different addresses.
  

How Apache Handles Virtual Hosts
  Configuring Apache for virtual hosts is a two stage process. Firstly, it needs to be told which IP addresses (and ports) to listen to for incoming web connections. By default Apache listens to port 80 on all IP addresses of the local machine, and this is often sufficient. If you have a more complex requirement, such as listening on various port numbers, or only to specific IP addresses, theBindAddressorListendirectives can be used.
  Secondly, having accepted an incoming web connection, the server needs to be configured to handle the request differently depending on what virtual host it was addressed to. This usually involves configuring Apache to use a different DocumentRoot.
  

Telling Apache Which Addresses to Listen To
  If you are happy for Apache to listen to all local IP addresses on the port specified by thePortdirective, you can skip this section. However there are some cases where you will want to use the directives explained here:


  • If you have many IP addresses on the machine but only want to run a web server on some of them
  • If one or more of your virtual hosts is on a different port
  • If you want to run multiple copies of the Apache server serving different virtual hosts
  There are two ways of telling Apache what addresses and ports to listen two: either you use theBindAddressdirective to specify a single address or port, or you use theListendirective to any number of specific addresses or ports.
  For example, if you run your main server on IP address 10.1.2.3 port 80, and a virtual host on IP 10.1.2.4 port 8000, you would use:

  Listen 10.1.2.3:80
Listen 10.1.2.4:8000

  Listen and BindAddress are documented on the Apache site.
  

Configuring the Virtual Hosts
  Having got Apache to listen to the appropriate IP addresses and ports, the final stage is to configure the server to behave differently for requests on each of the different addresses. This is done usingsections in the configuration files, normally in httpd.conf.
  A typical (but minimal) virtual host configuration looks like this:

  
DocumentRoot /www/vhost1
ServerName  www.my-dom.com


  This should be placed in the httpd.conf file. You would replace the text '10.1.2.3' with one of your virtual host IP addresses. If you want to specify a port as well, follow the IP address with a colon and the port number (eg '10.1.2.4:8000'). If omitted, the port defaults to 80.
  If nosections are given in the configuration files, Apache will treat requests from the different addresses and ports identically. In terms of setting up virtual hosts, we call the default behaviour the 'main server' configuration. Unless overridden bysections, the main server behaviour will be inherited by all the virtual hosts. When configuring virtual hosts, you need to decide what changes need to be made in each of the virtual host configurations.
  Any directives inside asection apply to justthat virtual host. The directives either override the configuration give in the main server, or supplement it, depending on the directive. For example, theDocumentRootdirective in asection overrides the main server'sDocumentRoot, whileAddTypesupplements the main server's mime types.
  Now, when a request arrives, Apache uses the IP address and port it arrived on to find a matching virtual host configuration. If no virtual host matches the address and port, it is handled by the main server configuration. If it does match a virtual host address, Apache will use the configuration of that virtual server to handle the request.
  For the example above, the server configuration used will be the same as the main server, except that theDocumentRootwill be /www/vhost1, and theServerNamewill by www.my-dom.com. Directives commonly set insections areDocumentRoot,ServerName,ErrorLogandTransferLog. Directives that deal with handling requests and resources are valid insidesections. However some directives are not valid insidesections, includingBindAddress,StartSevers,Listen,GroupandUser.
  You can have as manysections as you want. You can choose to leave one or more of your virtual hosts being handled by the main server, or have afor every available address and port, and leave the main server with no requests to handle.
  

VirtualHost sections for non-IP Virtual Hosts
  Non-IP virtual hosts are configured in a very similar way. The IP address that the requests will arrive on is given in thedirective, and the host name is put in the ServerName directive. The difference is that there will (usually) be more than onesection handling the same IP address. In order for Apache to know whether a request arriving on a particular IP address is supposed to be a name-based requests, theNameVirtualHostdirective is used to tell Apache the IP addresses for name-based requests. A virtual host can handle more than one non-IP hostname by using theServerAliasdirective, in addition to theServerName.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-93530-1-1.html 上篇帖子: Apache-访问控制 下篇帖子: apache ci 的404设置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表