
我在 Ubuntu 18.04 下運行 Apache 2.4
我遇到了這個問題,我在瀏覽器欄上輸入的所有位址都被重定向到一我配置的兩個網站中的一個(在這種情況下,每當我鍵入應重定向到的地址時other-site
,我只會顯示下面的網站jekyll
。)
例如:
- 如預期的那樣,在瀏覽器上輸入內容
jekyll
會顯示 jekyll 網站 other-site
在瀏覽器上輸入也向我展示了 jekyll 網站,即使有另一個 VirtualHost 正在偵聽該名稱
我有這些兩個獨立的站點配置為VHosts /etc/apache2/sites-available
(兩者都已啟用通過a2ensite
)
jekyll.conf
<VirtualHost *:80> DirectoryIndex index.html DocumentRoot /var/www/jekyll/_site LimitRequestFieldSize 48000 <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/jekyll/_site> AllowOverride All Order allow,deny allow from all </Directory> <Directory /var/www/jekyll/_site/> Order allow,deny allow from all </Directory> ServerName jekyll ServerName http://jekyll ServerName http://localhost/jekyll ServerAdmin webmaster@localhost </VirtualHost>
其他站點.conf
<VirtualHost *:80> DirectoryIndex index.html DocumentRoot /var/www/other-site/_site LimitRequestFieldSize 48000 <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/other-site/_site> AllowOverride All Order allow,deny allow from all </Directory> <Directory /var/www/other-site/_site/> Order allow,deny allow from all </Directory> ServerName other-site ServerName http://other-site ServerName http://localhost/other-site ServerAdmin webmaster@localhost </VirtualHost>
我還添加了這些條目,/etc/hosts
以便 localhost 被重定向到每個網站:
127.0.0.1 localhost
127.0.0.1 localhost/jekyll
127.0.0.1 localhost/other-site
127.0.0.1 jekyll
127.0.0.1 other-site
127.0.1.1 felipe-Inspiron-7559
答案1
Apache 設定檔中的屬性ServerName
不需要重複,因為後續的每一行都會取代前面的行。
相反,您可以ServerName
像ServerAlias
這樣使用:
ServerName jekyll
ServerAlias jekyll.local *.jekyll *.jekyll.local
請注意,這是不合邏輯的:
127.0.0.1 localhost/jekyll
127.0.0.1 localhost/other-site
這些不是域(或子域),而是localhost
.結果,只會localhost
被觀察到。這就是為什麼我沒有將其包含在 Apache 配置中,如上所述。
因此,考慮到這一點,您可以三阿帕契設定檔:
⇢000-jekyll.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName jekyll
ServerAlias jekyll.local *.jekyll *.jekyll.local
DirectoryIndex index.html
DocumentRoot /var/www/jekyll/_site
LimitRequestFieldSize 48000
<Directory /var/www/jekyll/_site>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/jekyll-error.log
CustomLog ${APACHE_LOG_DIR}/jekyll-access.log combined
</VirtualHost>
⇢001-other.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName other-site
ServerAlias other-site.local *.other-site *.other-site.local
DirectoryIndex index.html
DocumentRoot /var/www/other-site/_site
LimitRequestFieldSize 48000
<Directory /var/www/other-site/_site>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/other-error.log
CustomLog ${APACHE_LOG_DIR}/other-access.log combined
</VirtualHost>
⇢999-default.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName localhost
ServerAlias *.localhost * *.*
DirectoryIndex index.html
DocumentRoot /var/www
LimitRequestFieldSize 48000
<Directory /var/www>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/local-error.log
CustomLog ${APACHE_LOG_DIR}/local-access.log combined
</VirtualHost>
Apache 會依照設定檔的順序處理流量。因此,任何與指定的域匹配的域都000-jekyll.conf
將由該檔案處理。如果沒有找到匹配項,001-other.conf
則將進行檢查。如果沒有找到匹配項,999-default.conf
則將使用。請注意文件ServerAlias
中的999-default.conf
以及它如何依賴廣泛開放的通配符。這意味著它將被視為與定義的設定檔不匹配的流量的包羅萬象。
筆記:Apache 設定檔經過簡化,以消除不相關的Directory
區塊並讓每個主機使用自己的錯誤日誌。