Virtual Hosts का परिचय
In this page:
Virtual Hosts क्यों Exist करते हैं
Virtual Hosts के बिना, एक server असल में हर IP address और port पर सिर्फ एक site serve कर सकता है। Virtual Hosts एक Apache instance को एक साथ site-a.com, site-b.com, और blog.site-a.com के लिए answer करने देते हैं, हर एक की अपनी DocumentRoot, logs, और settings के साथ -- बिल्कुल वही जिस पर shared hosting और ज़्यादातर multi-site servers depend करते हैं।
Name-Based Virtual Hosts
आज सबसे common setup name-based virtual hosting है: हर site same IP और port share करती है, और Apache decide करता है कि कौन सी serve करनी है browser द्वारा भेजे गए Host header (जो बस वह domain name है जो आपने type किया) को पढ़कर। एक <VirtualHost *:80> block, जो इसके ServerName से match किया जाता है, हर site define करता है।
एक VirtualHost Block की Anatomy
एक typical block ServerName (primary domain जिसे यह answer करता है) set करता है, optionally ServerAlias (दूसरे domains/subdomains जिन्हें same block से match होना चाहिए, जैसे www.example.com), DocumentRoot (जहाँ इसकी files रहती हैं), और अपने खुद के ErrorLog/CustomLog paths ताकि हर site के logs अलग रहें।
Apache Match कैसे चुनता है
Apache आने वाली requests को हर <VirtualHost> block के against उसी order में check करता है जिस order में वे load होते हैं, और पहला वही इस्तेमाल करता है जिसका ServerName/ServerAlias request के Host header से match करे। अगर कुछ भी match न हो, तो यह उस IP/port के लिए define किए गए बिल्कुल पहले VirtualHost block पर fall back करता है -- यही कारण है कि जानबूझकर एक "default" या catch-all site पहले रखना आम practice है।
एक नया Virtual Host Enable करना
Ubuntu पर, एक नई site का config sites-available/ में एक file है, जिसे a2ensite yoursite.conf से enable किया जाता है, फिर systemctl reload apache2 से activate किया जाता है। RHEL-family systems पर, file को बस included conf.d/ directory में होना चाहिए और Apache इसे अगले reload पर pick up कर लेता है -- कोई अलग enable step नहीं है।
उदाहरण: A basic name-based VirtualHost
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
{# Flagged by hand after confirming a runner can't handle this example
(a shell command / go.mod file stored as a TopicExample, a language
feature the configured runner version doesn't support, or output
that blows a runner's sandbox limit) -- see TopicExample.norun.
Never render the run button for these, regardless of language,
since it would just fail at execute_code (or worse, hang the
Judge0 queue on a submission that can never finish cleanly). #}
- एक VirtualHost block पर
ServerNameभूल जाना, जो Apache को guess करने पर छोड़ देता है और एक startup warning log कर सकता है या requests को unpredictably match कर सकता है। - बिल्कुल same ServerName वाले दो VirtualHost blocks define करना, ताकि दूसरा असल में कभी इस्तेमाल ही न हो।
- VirtualHost file edit करना लेकिन Apache reload करना भूल जाना (या, Ubuntu पर, पहले
a2ensiteभूल जाना) -- नई site बस अभी तक reachable नहीं होगी।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: