Httpd Virtual Host Configuration: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(18 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 Configuration]]
* [[httpd VirtualHost]]


=Name-Based Virtual Host Configuration=
=Name-Based Virtual Host Configuration=
Line 13: Line 18:
</pre>
</pre>


<font color=purple><tt>ip</tt> could be "*"</font>
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.
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.
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=
=Name-Based Virtual Host Resolution=
Line 31: Line 38:
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.
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>
</font>
==In-Depth Discussion of Virtual Host Matching==
https://httpd.apache.org/docs/2.4/vhosts/details.html


=Configuration Inheritance=
=Configuration Inheritance=
Line 38: Line 49:
If any of these directives appear inside a  <tt><VirtualHost></tt> container, their values override the "main" defaults.
If any of these directives appear inside a  <tt><VirtualHost></tt> container, their values override the "main" defaults.


Overridable directives:
Inherited and overridable directives:


* [[httpd ServerAdmin|ServerAdmin]]
* [[httpd ServerAdmin|ServerAdmin]]
Line 56: Line 67:
IncludeOptional conf.d/*.conf
IncludeOptional conf.d/*.conf


<VirtualHost>
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>


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


</pre>
</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>