1

I had my LFCE exam and i stacked on a question. Question requested to have apache2 installed with 2 virtual host. site1.example.com and site2.example.com. Both virtual hosts share the Document Root /var/www/html/. . also requested to have 2 different files in document root. test_page1.html and test_page2.html and request the file test_page1.html is accessible only from the site1.example.com virtual host and test_page2.html is accessible only from site2.example.com.

My problem was how to secure the test_page1.html will be not accessible from virtual host 2 and vice versa if they share the same Document root.

Virtual host 1

<VirtualHost *:80>
ServerName site1.example.com
DocumentRoot /var/www/html/
<Directory /var/www/html/> 
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Virtual host 2

<VirtualHost *:80>
ServerName site2.example.com
DocumentRoot /var/www/html/
<Directory /var/www/html/> 
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
  • 1
    What are the configurations on your virtualhosts? What are the errors that you are seeing? – BANJOSA Dec 11 '17 at 8:09
  • Hello, I have no errors, i had a very specific question. how to secure the test_page1.html will be not accessible from virtual host 2 and vice versa if they share the same Document root. – Spyros Savvanis Dec 11 '17 at 8:10
  • @Vlastimil i added virtual hosts – Spyros Savvanis Dec 11 '17 at 8:19
  • 1
    Check the fine docs which directives can be used in a VirtualHost context, in particular Location, Files, and Directory – Ulrich Schwarz Dec 11 '17 at 8:19
0

I will answer my question for future referral. It can be done with a combination of Location block, regular expressions and server variables.

Create a new config file

nano /etc/apache2/sites-available/secure.conf

Add:

<Location /test_page1.html>
    <If "!(%{SERVER_NAME} -strmatch 'site1.example.com*')">
    Require all denied
    </If>
</Location>
<Location /test_page2.html>
    <If "!(%{SERVER_NAME} -strmatch 'site2.example.com*')">
    Require all denied
    </If>
</Location>

enable the config and restart apache:

 a2ensite secure
 systemctl restart apache2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.