Apache's method for configuring virtual directories and multi host headers

  • 2020-05-13 04:27:58
  • OfStack

Learn 1 about the relationship between the root directory and the virtual directory:
When you open the httpd.conf file after installing apache, you will find that the default setting for DocumentRoot is the htdocs folder in the apache installation directory. Then you can enlarge the htdocs folder test, case you put in it a 1. PHP, can input http: / / 127.0.0.1/1. php test. The path following DocumentRoot is the root directory of apache. Sometimes we don't want to put their own websites in here, for example, I want to put F: / MyPHP to run, you can modify a DocumentRoot F: / MyPHP, then will the 1. php into F: / MyPHP folder, use the same http: / / 127.0.0.1/1. php can normal visit.

What is a virtual directory? As the name implies, it is only a virtual directory, and the real directory is different. Let's look at using real directory access, we are just F: / MyPHP Test 1 folder has been established, and then into the inside 2. php, this time can pass http: / / 127.0.0.1 Test / 2. php access. But sometimes you might create a folder that has a mapping to the access address you want to enter, rather than just typing the Test folder name. There are a number of reasons to do this, one of which is security, because then others will know your root folder. Specific points, you placed 2. In the Test folder php, but want to http: / / 127.0.0.1 cmj / 2. php access 2. php rather than through http: / / 127.0.0.1 Test / 2. php to access? At this point we need a virtual directory, which obviously does not have the cmj folder, but can be accessed as if it were a formal directory 1, requiring a mapping relationship.

Multiple virtual directories

First to install Apache in D: \ Program Files \ Apache2 2 directory, the port number is set to 8080, the default site after the installation is complete the root directory for D: \ Program Files \ Apache2 2 \ htdocs, usually we can build a folder under the htdocs MySite, then the browser input: http: / / localhost: 8080 / MySite so you can see our own site. Sometimes, however, we want to put our site under another directory, so we need to configure the virtual directory
For example, we set up the following folders in D set D: \ Code \ WebSite, then through http: / / localhost: 8080 / DemoSite to visit this site

Open the httpd.conf file and search < IfModule alias_module > Node, and then enter the following content inside the node:


# Below is the virtual directory declaration format 
#Alias Is used to define a virtual directory and a virtual directory path, where the virtual directory name is used for URL Access path alias, can be different from the virtual directory name 
#<Directory/> Nodes are used to define access rights to directories, and so on 
#
#Alias  Virtual directory name   Virtual directory path 
#<Directory  Virtual directory path >
#  Options Indexes FollowSymLinks
#  AllowOverride All
#  Order allow,deny
#  Allow from all
#</Directory>

# Here's a concrete example, /DemoSite Is a directory alias  "D:/Code/WebSite" Is the actual path to the virtual directory 
Alias /DemoSite "D:/Code/WebSite"

<Directory "D:/Code/WebSite">
  Options Indexes FollowSymLinks
  AllowOverride All
  Order allow,deny
  Allow from all
</Directory>

After service in a browser restart Apache input http: / / localhost: 8080 / DemoSite can normal visit
Note here that you should always use "/" instead of "\" in the directory. The reason is that "\" stands for escape character, which in some cases can cause inexplicable errors < IfModule alias_module > Nodes for reference


<IfModule alias_module>
  #
  # Redirect: Allows you to tell clients about documents that used to 
  # exist in your server's namespace, but do not anymore. The client 
  # will make a new request for the document at its new location.
  # Example:
  # Redirect permanent /foo http://localhost/bar

  #
  # Alias: Maps web paths into filesystem paths and is used to
  # access content that does not live under the DocumentRoot.
  # Example:
  # Alias /webpath /full/filesystem/path
  #
  # If you include a trailing / on /webpath then the server will
  # require it to be present in the URL. You will also likely
  # need to provide a <Directory> section to allow access to
  # the filesystem path.

  #
  # ScriptAlias: This controls which directories contain server scripts. 
  # ScriptAliases are essentially the same as Aliases, except that
  # documents in the target directory are treated as applications and
  # run by the server when requested rather than as documents sent to the
  # client. The same rules about trailing "/" apply to ScriptAlias
  # directives as to Alias.
  #
  ScriptAlias /cgi-bin/ "D:/Program Files/Apache2.2/cgi-bin/"
  
  Alias /DemoSite "D:/Code/WebSite"

  <Directory "D:/Code/WebSite">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</IfModule>

Multi-host header binding
(it is to bind multiple domain names on one port, and then each domain name can be accessed to a different directory. The host header is IIS.) open the httpd.conf file, and add the following content at the end of the file


# The multi-host header configuration does not need to be placed under a specific node, 1 It can be added directly at the bottom of the configuration file 
#NameVirtualHost addr[:port]  for 1 The domain name based virtual host is specified 1 a IP address ( And port )
# Declare that this command must be added to the header, otherwise the header configuration will not take effect 
#VirtualHost Under the node ServerName That's the domain name to bind to, DocumentRoot Represents the directory to which this domain name points 
# For native testing, please refer to the hosts Domain name binding such as  127.0.0.1 www.mysite1.com

NameVirtualHost *:8080
<VirtualHost *:8080>
  ServerName www.mysite1.com
  DocumentRoot "D:\Program Files\Apache2.2\htdocs"
</VirtualHost>

<VirtualHost *:8080>
  ServerName www.mysite2.com
  DocumentRoot "D:\Code\MySite"
</VirtualHost>

Configured, restart apache service, browser input www. mysite1. com: 8080, will automatically redirect to D: \ Program Files \ Apache2 2 \ htdocs site

Input www. mysite2. com: 8080 will automatically redirect to D: \ Code \ MySite site, so can be achieved on a server running multiple sites at the same time


Related articles: