Httpd Virtual Host Configuration: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(40 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* http://httpd.apache.org/docs/2.4/vhosts/name-based.html
=Internal=
=Internal=


* [[httpd Concepts#Virtual_Host_Configuration]]
* [[httpd Concepts#Virtual_Host_Configuration|httpd Concepts]]
* [[httpd Configuration]]
* [[httpd VirtualHost]]
 
=Name-Based Virtual Host Configuration=
 
Virtual hosts can be declared using the <tt><VirtualHost></tt> configuration element. <tt><VirtualHost></tt> encloses a group of directives that apply to only a particular virtual host. Each  <tt><VirtualHost></tt> is associated with an IP:port pair.
 
<pre>
<VirtualHost ip:port>
</VirtualHost>
</pre>
 
More details about <tt>VirtualHost</tt> element syntax can be found here <tt>[[httpd VirtualHost|VirtualHost]]</tt>.
 
For each virtual host you want to serve, create a <tt><VirtualHost></tt> section.
 
The <tt><VirtualHost></tt> section must contain at minimum a <tt>[[httpd ServerName|ServerName]]</tt> directive to designate what host to serve and a <tt>[[httpd DocumentRoot |DocumentRoot]]</tt> directive to indicate the file system directory that contains the content to server.
 
<tt>NameVirtualHost</tt>: the directive was necessary in older versions but it is now (Apache 2.4.6) not necessary and it will be deprecated.
 
=Name-Based Virtual Host Resolution=
 
When a request arrives, the server must find the best (most specific) matching <tt><VirtualHost></tt> configuration based on the IP address and the port the request came on. If just one <tt><VirtualHost></tt> is identified, that's the one that is used. If there is more than one virtual host that matches the IP:port, httpd will further compare the <tt>ServerName</tt> and <tt>ServerAlias</tt> directives to the value of the "Host" header arrived with the request. If no <tt>ServerName</tt>/<tt>ServerAlias</tt> match is found, then the first listed virtual host is used.
 
==An IP:port Match Must Occur==
 
Note that an IP:port match ''must'' occur, otherwise the main server will be used to serve the request, even if there are declared virtual servers with matching ServerNames.
 
<font color=purple>
==The Default Virtual Host==
It is usually wise to create a default virtual host with a ServerName matching that of the base server. New domains on the same interface and port, but requiring separate configurations, can then be added as subsequent (non-default) virtual hosts.
</font>
 
==In-Depth Discussion of Virtual Host Matching==
 
https://httpd.apache.org/docs/2.4/vhosts/details.html
 
=Configuration Inheritance=
 
All directives present in the httpd configuration file above the <tt><VirtualHost></tt> section set up the values used by the "main" server, which responds to any requests that are not handled by a  <tt><VirtualHost></tt> definition. These values also provide defaults for any  <tt><VirtualHost></tt> containers that defined later in the file.
 
If any of these directives appear inside a  <tt><VirtualHost></tt> container, their values override the "main" defaults.
 
Inherited and overridable directives:
 
* [[httpd ServerAdmin|ServerAdmin]]
* [[httpd ServerName|ServerName]]
* [[httpd DocumentRoot|DocumentRoot]]
* [[httpd ErrorLog|ErrorLog]]
* [[httpd LogLevel|LogLevel]]
 
=Example=
 
<pre>


=Overview=
#
# ... the "main" section
#


Virtual hosts can be declared using the <tt><VirtualHost></tt> configuration element. <tt><VirtualHost></tt> encloses a group of directives that apply to only a particular virtual host.
IncludeOptional conf.d/*.conf


=Name-Based Virtual Host Configuration=
NameVirtualHost 1.2.3.4:80
 
<VirtualHost 1.2.3.4:80>
    # This first-listed virtual host is also the default for 108.68.48.75:80
    ServerName varangian.novaordis.com
    DocumentRoot "/var/www/varangian"
</VirtualHost>
 
<VirtualHost 1.2.3.4:80>
    ServerName praetorian.novaordis.com
    DocumentRoot "/var/www/praetorian"
</VirtualHost>
 
</pre>

Latest revision as of 01:04, 30 December 2023

External

Internal

Name-Based Virtual Host Configuration

Virtual hosts can be declared using the <VirtualHost> configuration element. <VirtualHost> encloses a group of directives that apply to only a particular virtual host. Each <VirtualHost> is associated with an IP:port pair.

<VirtualHost ip:port>
</VirtualHost>

More details about VirtualHost element syntax can be found here VirtualHost.

For each virtual host you want to serve, create a <VirtualHost> section.

The <VirtualHost> section must contain at minimum a ServerName directive to designate what host to serve and a DocumentRoot directive to indicate the file system directory that contains the content to server.

NameVirtualHost: the directive was necessary in older versions but it is now (Apache 2.4.6) not necessary and it will be deprecated.

Name-Based Virtual Host Resolution

When a request arrives, the server must find the best (most specific) matching <VirtualHost> configuration based on the IP address and the port the request came on. If just one <VirtualHost> is identified, that's the one that is used. If there is more than one virtual host that matches the IP:port, httpd will further compare the ServerName and ServerAlias directives to the value of the "Host" header arrived with the request. If no ServerName/ServerAlias match is found, then the first listed virtual host is used.

An IP:port Match Must Occur

Note that an IP:port match must occur, otherwise the main server will be used to serve the request, even if there are declared virtual servers with matching ServerNames.

The Default Virtual Host

It is usually wise to create a default virtual host with a ServerName matching that of the base server. New domains on the same interface and port, but requiring separate configurations, can then be added as subsequent (non-default) virtual hosts.

In-Depth Discussion of Virtual Host Matching

https://httpd.apache.org/docs/2.4/vhosts/details.html

Configuration Inheritance

All directives present in the httpd configuration file above the <VirtualHost> section set up the values used by the "main" server, which responds to any requests that are not handled by a <VirtualHost> definition. These values also provide defaults for any <VirtualHost> containers that defined later in the file.

If any of these directives appear inside a <VirtualHost> container, their values override the "main" defaults.

Inherited and overridable directives:

Example


#
# ... the "main" section
#

IncludeOptional conf.d/*.conf

NameVirtualHost 1.2.3.4:80

<VirtualHost 1.2.3.4:80>
    # This first-listed virtual host is also the default for 108.68.48.75:80 
    ServerName varangian.novaordis.com
    DocumentRoot "/var/www/varangian"
</VirtualHost>

<VirtualHost 1.2.3.4:80>
    ServerName praetorian.novaordis.com
    DocumentRoot "/var/www/praetorian"
</VirtualHost>