Name-based Virtual Host Support
This document describes when and how to use name-based virtual hosts.
Name-based vs. IP-based Virtual Hosts
IP-based virtual hosts use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host.
With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.
Name-based virtual hosting is usually simpler, since you need only configure your DNS server to map each hostname to the correct IP address and then configure the Apache HTTP Server to recognize the different hostnames. Name-based virtual hosting also eases the demand for scarce IP addresses. Therefore you should use name-based virtual hosting unless you are using equipment that explicitly demands IP-based hosting. Historical reasons for IP-based virtual hosting based on client support are no longer applicable to a general-purpose web server.
Name-based virtual hosting builds off of the IP-based virtual host selection algorithm, meaning that searches for the proper server name occur only between virtual hosts that have the best IP-based address.
How the server selects the proper name-based virtual host
It is important to recognize that the first step in name-based virtual host resolution is IP-based resolution. Name-based virtual host resolution only chooses the most appropriate name-based virtual host after narrowing down the candidates to the best IP-based match. Using a wildcard (*) for the IP address in all of the VirtualHost directives makes this IP-based mapping irrelevant.
When a request arrives, the server will find the best (most specific) matching
<VirtualHost> argument based on
the IP address and port used by the request. If there is more than one virtual host
containing this best-match address and port combination, Apache will further
compare the ServerName and ServerAlias directives to the server name
present in the request.
If you omit the ServerName
directive from any name-based virtual host, the server will default
to a fully qualified domain name (FQDN) derived from the system hostname.
This implicitly set server name can lead to counter-intuitive virtual host
matching and is discouraged.
The default name-based vhost for an IP and port combination
If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.
Using Name-based Virtual Hosts
| Related Modules | Related Directives |
|---|---|
The first step is to create a <VirtualHost> block for
each different host that you would like to serve. Inside each <VirtualHost> block, you will need at minimum a
ServerName directive to designate
which host is served and a DocumentRoot
directive to show where in the filesystem the content for that host
lives.
Main host goes away
Any request that doesn't match an existing <VirtualHost> is handled by the global
server configuration, regardless of the hostname or ServerName.
When you add a name-based virtual host to an existing server, and
the virtual host arguments match preexisting IP and port combinations,
requests will now be handled by an explicit virtual host. In this case,
it's 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.
ServerName inheritance
It is best to always explicitly list a ServerName in every name-based virtual host.
If a VirtualHost doesn't specify
a ServerName, a server name will be
inherited from the base server configuration. If no server name was
specified globally, one is detected at startup through reverse DNS resolution
of the first listening address. In either case, this inherited server name
will influence name-based virtual host resolution, so it is best to always
explicitly list a ServerName in every
name-based virtual host.
For example, suppose that you are serving the domain
www.example.com and you wish to add the virtual host
other.example.com, which points at the same IP address.
Then you simply add the following to httpd.conf:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/www/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot "/www/otherdomain"
</VirtualHost>
You can alternatively specify an explicit IP address in place of the
* in <VirtualHost> directives. For example, you might want to do this
in order to run some name-based virtual hosts on one IP address, and either
IP-based, or another set of name-based virtual hosts on another address.
Many servers want to be accessible by more than one name. This is
possible with the ServerAlias
directive, placed inside the <VirtualHost> section. For example in the first <VirtualHost> block above, the
ServerAlias directive indicates that
the listed names are other names which people can use to see that same
web site:
ServerAlias example.com *.example.com
then requests for all hosts in the example.com domain will
be served by the www.example.com virtual host. The wildcard
characters * and ? can be used to match names.
Of course, you can't just make up names and place them in ServerName or ServerAlias. You must
first have your DNS server properly configured to map those names to an IP
address associated with your server.
Name-based virtual hosts for the best-matching set of <virtualhost>s are processed
in the order they appear in the configuration. The first matching ServerName or ServerAlias is used, with no different precedence for wildcards
(nor for ServerName vs. ServerAlias).
The complete list of names in the VirtualHost
directive are treated just like a (non wildcard)
ServerAlias.
Finally, you can fine-tune the configuration of the virtual hosts
by placing other directives inside the <VirtualHost> containers. Most directives can be
placed in these containers and will then change the configuration only of
the relevant virtual host. To find out if a particular directive is allowed,
check the Context of the
directive. Configuration directives set in the main server context
(outside any <VirtualHost>
container) will be used only if they are not overridden by the virtual host
settings.
دعم المضيف الافتراضي المستند إلى الاسم
يصف هذا المستند متى وكيفية استخدام المضيفات الافتراضية المستندة إلى الاسم.
المضيفات الافتراضية المستندة إلى الاسم مقابل المستندة إلى عنوان IP
المضيفات الافتراضية المستندة إلى عنوان IP تستخدم عنوان IP الخاص بالاتصال لتحديد المضيف الافتراضي الصحيح لتقديمه. لذلك فأنت بحاجة إلى عنوان IP منفصل لكل مضيف.
مع الاستضافة الافتراضية المستندة إلى الاسم، يعتمد الخادم على العميل للإبلاغ عن اسم المضيف كجزء من ترويسات HTTP. باستخدام هذه التقنية، يمكن للعديد من المضيفات المختلفة مشاركة عنوان IP نفسه.
عادةً ما تكون الاستضافة الافتراضية المستندة إلى الاسم أبسط، حيث تحتاج فقط إلى تكوين خادم DNS الخاص بك لربط كل اسم مضيف بعنوان IP الصحيح ثم تكوين خادم Apache HTTP للتعرف على أسماء المضيفين المختلفة. تخفف الاستضافة الافتراضية المستندة إلى الاسم أيضًا من الطلب على عناوين IP الشحيحة. لذلك يجب عليك استخدام الاستضافة الافتراضية المستندة إلى الاسم ما لم تكن تستخدم معدات تتطلب صراحةً استضافة مستندة إلى عنوان IP. الأسباب التاريخية للاستضافة الافتراضية المستندة إلى عنوان IP والمبنية على دعم العميل لم تعد تنطبق على خادم ويب عام الغرض.
تعتمد الاستضافة الافتراضية المستندة إلى الاسم على خوارزمية تحديد المضيف الافتراضي المستندة إلى عنوان IP، مما يعني أن عمليات البحث عن اسم الخادم المناسب تتم فقط بين المضيفات الافتراضية التي تمتلك أفضل عنوان مستند إلى IP.
كيف يختار الخادم المضيف الافتراضي المناسب المستند إلى الاسم
من المهم إدراك أن الخطوة الأولى في حل المضيف الافتراضي المستند إلى الاسم هي الحل المستند إلى عنوان IP. ولا يختار حل المضيف الافتراضي المستند إلى الاسم المضيف الافتراضي الأنسب المستند إلى الاسم إلا بعد تضييق نطاق المرشحين إلى أفضل تطابق مستند إلى عنوان IP. إن استخدام محرف البدل (*) لعنوان IP في جميع توجيهات VirtualHost يجعل هذا التعيين المستند إلى IP غير ذي صلة.
عندما يصل طلب، سيجد الخادم أفضل تطابق (الأكثر تحديدًا) <VirtualHost> للمُعامل بناءً على عنوان IP والمنفذ المستخدمين بواسطة الطلب. إذا كان هناك أكثر من مضيف افتراضي واحد يحتوي على تركيبة العنوان والمنفذ الأفضل تطابقًا هذه، فسوف يقوم Apache بمقارنة ServerName و ServerAliasالتوجيهات لاسم الخادم الموجود في الطلب.
إذا أغفلت ServerName التوجيه من أي مضيف افتراضي قائم على الاسم، فسيعتمد الخادم افتراضيًا اسم نطاق مؤهل بالكامل (FQDN) مشتقًا من اسم مضيف النظام. قد يؤدي اسم الخادم المحدد ضمنيًا هذا إلى مطابقة غير متوقعة للمضيف الافتراضي، وهو أمر غير موصى به.
المضيف الافتراضي القياسي القائم على الاسم لتركيبة عنوان IP والمنفذ
إذا لم يتم العثور على ServerName أو ServerAlias مطابق في مجموعة المضيفين الافتراضيين التي تحتوي على تركيبة عنوان IP والمنفذ الأكثر تخصيصًا والمطابقة، فإن أول مضيف افتراضي مدرج يطابق ذلك هو ما سيتم استخدامه.
استخدام المضيفات الافتراضية القائمة على الاسم
| الوحدات النمطية ذات الصلة | التوجيهات ذات الصلة |
|---|---|
الخطوة الأولى هي إنشاء <VirtualHost> كتلة لكل مستضيف مختلف ترغب في تقديم الخدمة له. وداخل كل <VirtualHost> كتلة، ستلاحظ حاجتك كحد أدنى إلى ServerName توجيه لتحديد المستضيف الذي تتم خدمته و DocumentRoot توجيه لإظهار مكان وجود محتوى ذلك المستضيف في نظام الملفات.
يزول المستضيف الرئيسي
أي طلب لا يطابق <VirtualHost> يتم التعامل معه بواسطة تكوين الخادم العام، بغض النظر عن اسم المستضيف أو ServerName.
عند إضافة مستضيف افتراضي قائم على الاسم إلى خادم حالي، وتطابق وسائط المستضيف الافتراضي تجميعات عنوان IP والمنفذ الموجودة مسبقًا، فإن الطلبات سيتم التعامل معها الآن بواسطة مستضيف افتراضي صريح. وفي هذه الحالة، يكون من الحكمة عادةً إنشاء مستضيف افتراضي افتراضي مع ServerName يطابق الخاص بالخادم الأساسي. كما يمكن لاحقًا إضافة نطاقات جديدة على الواجهة والمنفذ نفسهما، والتي تتطلب تكوينات منفصلة، كمستضيفات افتراضية لاحقة (غير افتراضية).
وراثة ServerName
من الأفضل دائمًا تحديد ServerName صراحةً في كل مضيف افتراضي مستند إلى الاسم.
إذا كان VirtualHost لا يحدد
ServerName، فسوف يتم توريث اسم الخادم من التكوين الأساسي للخادم. وإذا لم يتم تحديد اسم خادم على المستوى العام، فسيتم اكتشاف اسم عند بدء التشغيل من خلال التحليل العكسي لـ DNS لعنوان الاستماع الأول. وفي أي من الحالتين، سيؤثر اسم الخادم الموروث هذا على حل المضيف الافتراضي المستند إلى الاسم، لذا من الأفضل دائمًا تحديد ServerName صراحةً في كل
مضيف افتراضي مستند إلى الاسم.
على سبيل المثال، افترض أنك تخدم النطاق
www.example.com وترغب في إضافة المضيف الافتراضي
other.example.com، الذي يشير إلى نفس عنوان IP.
عندئذٍ يمكنك ببساطة إضافة ما يلي إلى httpd.conf:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/www/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot "/www/otherdomain"
</VirtualHost>
يمكنك بدلاً من ذلك تحديد عنوان IP صريح بدلاً من
* في <VirtualHost> التوجيهات. على سبيل المثال، قد ترغب في القيام بذلك لتشغيل بعض المضيفين الافتراضيين المستندين إلى الاسم على عنوان IP واحد، وإما مضيفين مستندين إلى IP، أو مجموعة أخرى من المضيفين الافتراضيين المستندين إلى الاسم على عنوان آخر.
ترغب العديد من الخوادم في أن تكون متاحة بأكثر من اسم واحد. وهذا ممكن باستخدام توجيه ServerAlias، الموضوع داخل قسم <VirtualHost>. على سبيل المثال، في كتلة <VirtualHost> الأولى أعلاه، يشير التوجيه ServerAlias إلى أن الأسماء المدرجة هي أسماء أخرى يمكن للناس استخدامها لرؤية موقع الويب نفسه:
ServerAlias example.com *.example.com
فعندئذٍ، فإن الطلبات الموجهة لجميع المضيفين في النطاق example.com ستتم خدمتها بواسطة المضيف الافتراضي www.example.com. يمكن استخدام أحرف البدل * و ? لمطابقة الأسماء. بالطبع، لا يمكنك مجرد اختلاق أسماء ووضعها في ServerName أو ServerAlias. يجب عليك أولاً تهيئة خادم DNS الخاص بك بشكل صحيح لتعيين تلك الأسماء إلى عنوان IP مرتبط بخادمك.
المضيفون الافتراضيون القائمون على الاسم لمجموعة الأكثر تطابقاً من <virtualhost>تُعالج
حسب ترتيب ظهورها في التكوين. أول مطابقة ServerName أو ServerAlias يُستخدم، دون أي أولوية مختلفة لأحرف البدل (ولا لـ ServerName مقابل ServerAlias).
القائمة الكاملة للأسماء في VirtualHostتُعامل التوجيهات تمامًا مثل توجيه (غير بديل)ServerAlias.
أخيرًا، يمكنك ضبط تكوين المضيفين الافتراضيين بدقة عن طريق وضع توجيهات أخرى داخل<VirtualHost> الحاويات. يمكن وضع معظم التوجيهات في هذه الحاويات وستغير حينئذٍ تكوين المضيف الافتراضي المعني فقط. لمعرفة ما إذا كان مسموحًا بتوجيه معين، تحقق منالسياق الخاص بالتوجيه. توجيهات التكوين المحددة فيسياق الخادم الرئيسي (خارج أي <VirtualHost>حاوية) لن يُستخدم إلا إذا لم يتم تجاوزه بواسطة إعدادات المضيف الافتراضي.
Frequently asked questions · الأسئلة الشائعة
What is a name-based virtual host and how does it differ from an IP-based host?ما هو المضيف الافتراضي المستند إلى الاسم وكيف يختلف عن المضيف المستند إلى عنوان IP؟
A name-based virtual host relies on the HTTP client header to report the target hostname, allowing multiple hostnames to share a single IP address. In contrast, an IP-based virtual host determines the site to serve based on the connection's IP address, requiring a distinct IP address for every host.
يعتمد المضيف الافتراضي المستند إلى الاسم على ترويسة HTTP المستلمة من العميل للإبلاغ عن اسم المضيف، مما يسمح لعدة أسماء مضيفين بالمشاركة في عنوان IP واحد. في المقابل، يحدد المضيف الافتراضي المستند إلى عنوان IP الموقع الذي سيتم خدمته بناءً على عنوان IP الخاص بالاتصال، مما يتطلب عنوان IP منفصلاً لكل مضيف.
When should an organization choose name-based virtual hosting over IP-based hosting?متى يجب على المنشأة اختيار الاستضافة الافتراضية المستندة إلى الاسم بدلاً من الاستضافة المستندة إلى عنوان IP؟
An organization should use name-based virtual hosting by default for general-purpose web serving because it simplifies DNS configuration and reduces demand for scarce IP addresses. IP-based virtual hosting should only be used if network equipment explicitly demands it.
يجب على المنشأة استخدام الاستضافة الافتراضية المستندة إلى الاسم كخيار افتراضي لتقديم خدمات الويب العامة لأنها تبسط تهيئة DNS وتقلل الطلب على عناوين IP الشحيحة. ولا يُلجأ إلى الاستضافة الافتراضية المستندة إلى عنوان IP إلا إذا كانت المعدات تتطلب ذلك صراحة.
What prerequisite DNS configuration must be completed before enabling name-based virtual hosts?ما هي متطلبات تهيئة نظام أسماء النطاقات (DNS) الأساسية قبل تفعيل المضيفات الافتراضية المستندة إلى الاسم؟
Before configuring name-based virtual hosts in Apache, you must properly configure your DNS server to map all intended domain names and aliases to the IP address associated with your web server. You cannot simply define unmapped names in ServerName or ServerAlias.
قبل تهيئة المضيفات الافتراضية المستندة إلى الاسم في أباتشي، يجب تهيئة خادم DNS لديك بشكل صحيح لربط جميع أسماء النطاقات والأسماء المستعارة المحددة بعنون IP المرتبط بخادم الويب. لا يمكنك مجرد وضع أسماء غير معرفة في توجيهات ServerName أو ServerAlias.
What operational risks occur if the ServerName directive is omitted from a virtual host configuration?ما هي المخاطر التشغيلية الناتجة عن إغفال توجيه ServerName في تهيئة المضيف الافتراضي؟
If ServerName is omitted, the server inherits a name from the base server configuration or derives an FQDN from system hostname or reverse DNS resolution. This implicit naming causes counter-intuitive virtual host matching and unexpected routing of web requests.
إذا تم إغفال توجيه ServerName، يرث الخادم اسماً من تهيئة الخادم الأساسية أو يستخرج اسم نطاق مؤهل بالكامل (FQDN) من اسم مضيف النظام أو تحليل DNS العكسي. يؤدي هذا التسمية الضمنية إلى مطابقة غير متوقعة للمضيفات الافتراضية وتوجيه غير صحيح للطلبات.
How do you implement multiple name-based virtual hosts sharing the same IP interface and port?كيف يتم تطبيق عدة مضيفات افتراضية مستندة إلى الاسم تشترك في نفس واجهة IP والمنفذ؟
Create a <VirtualHost> block for each host using matching IP and port definitions (such as *:80). Include at minimum an explicit ServerName directive designating the host and a DocumentRoot directive pointing to the respective file system location for that host's content.
قم بإنشاء كتلة <VirtualHost> لكل مضيف باستخدام تعريفات متطابقة لعنوان IP والمنفذ (مثل *:80). يجب تضمين توجيه ServerName صريح على الأقل لتحديد المضيف، وتوجيه DocumentRoot يشير إلى مسار نظام الملفات الخاص بمحتوى ذلك المضيف.
How does Apache validate and match incoming requests to the correct name-based virtual host?كيف يتحقق خادم أباتشي ويطابق الطلبات الواردة مع المضيف الافتراضي المستند إلى الاسم الصحيح؟
Apache first performs IP-based resolution to find the best matching IP address and port block. If multiple virtual hosts share that match, Apache evaluates the request's HTTP host header against ServerName and ServerAlias directives in the order they appear in the configuration file.
يقوم أباتشي أولاً بالتحليل المستند إلى عنوان IP للعثور على أفضل كتلة مطابقة لعنوان IP والمنفذ. وإذا شاركت عدة مضيفات افتراضية في تلك المطابقة، يقيم أباتشي ترويسة المضيف في طلب HTTP مقابل توجيهات ServerName وServerAlias حسب ترتيب ظهورها في ملف التهيئة.
Why do incoming web requests resolve to the first-listed virtual host instead of the expected domain host?لماذا تتجه طلبات الويب الواردة إلى المضيف الافتراضي المذكور أولاً بدلاً من المضيف المطلوب؟
Incoming requests fall back to the first-listed matching <VirtualHost> block if the request header does not match any configured ServerName or ServerAlias directive under the target IP and port combination, as the first block acts as the default host for that combination.
تعود الطلبات الواردة إلى أول كتلة <VirtualHost> مطابقة إذا كانت ترويسة الطلب لا تطابق أي توجيه ServerName أو ServerAlias معرف تحت تركيب عنوان IP والمنفذ المستهدف، حيث تعمل الكتلة الأولى كمضيف افتراضي لتلك التركيبة.
What action should be taken if global main server settings conflict with virtual host configurations?ما الإجراء الواجب اتخاذه إذا تعارضت إعدادات الخادم الرئيسية العامة مع تهيئات المضيف الافتراضي؟
Review the context of the conflicting directives. Global settings outside <VirtualHost> containers apply only when not overridden within a specific container. If issues persist, verify that explicit ServerName directives are defined across all blocks to prevent global inheritance conflicts.
قم بمراجعة سياق التوجيهات المتعارضة. تنطبق الإعدادات العامة الموجودة خارج كتل <VirtualHost> فقط عندما لا يتم تجاوزها داخل كتلة محددة. إذا استمرت المشكلة، تحقق من تعريف توجيهات ServerName صريحة في جميع الكتل لمنع تعارضات الوراثة العامة.
How does name-based virtual hosting optimize infrastructure efficiency for enterprise regional deployment?كيف تحسن الاستضافة الافتراضية المستندة إلى الاسم كفاءة البنية التحتية للتطبيقات المؤسسية إقليمياً؟
Name-based virtual hosting allows regional enterprises to host multiple domains and web services on a single shared IP address and interface. This eliminates the necessity of allocating dedicated IP addresses for each site, easing pressure on scarce IP resource pools across regional datacenters.
تتيح الاستضافة الافتراضية المستندة إلى الاسم للمؤسسات الإقليمية استضافة نطاقات وخدمات ويب متعددة على عنوان IP وواجهة مشتركة واحدة. وهذا يلغي الحاجة لتخصيص عناوين IP مستقلة لكل موقع، مما يخفف الضغط على موارد عناوين IP الشحيحة في مراكز البيانات الإقليمية.
What next step should an administrator take to customize and fine-tune individual name-based virtual hosts?ما الخطوة التالية التي يجب على المسؤول اتخاذها لتخصيص وضبط المضيفات الافتراضية المستندة إلى الاسم بشكل فردي؟
Administrators should check the permitted Context of desired configuration directives in the documentation, then add host-specific directives inside individual <VirtualHost> containers to safely adjust settings like document roots, aliases, and access controls for each host.
يجب على المسؤولين التحقق من سياق التوجيهات (Context) المسموح به في التوثيق، ثم إضافة التوجيهات الخاصة بكل مضيف داخل كتل <VirtualHost> الفردية لضبط الإعدادات مثل جذر المستند والأسماء المستعارة وضوابط الوصول لكل مضيف بأمان.
Related reading
Knowledge base
Log FilesKnowledge base
Getting StartedService
Sahab™ Cloud Servers سحاب™: Vision 2030 Cloud Computing Power: Secure, Scalable Cloud Hosting for Saudi & GCCHigh-performance cloud hosting with SSD/NVMe, 24/7 Saudi support, strong security and autoscaling — Fast Cloud ideal for business, apps, and websites.
Knowledge base
How to Register a New Domain NameArticle
VPS Hosting in KSA, GCC & MENA: The Practical Guide to Speed, Isolation, and Scaling Without OverpayingVPS hosting for KSA/GCC/MENA businesses: how to choose CPU/RAM/NVMe, improve speed, secure your server, and scale to dedicated or cloud VPS Riyadh KSA