Do I need to duplicate Virtualhost directives for ports 80 and 443?

I have a long and complex list of <VirtualHost> directives, and I have to duplicate them into separate <VirtualHost> groups for ports 80 and 443, because I use SSL. Whenever I update my mod_rewrite rules, I have to remember to do this in both places, otherwise I will break my application ... this duplication requires trouble. Is there a way to combine or list them - the only difference between them is that the version of port 443 contains SSLEngine, SSLCertificateFile, etc.

My <Virtualhost> contains many mod_rewrite rules, LocationMatch rules, CGI directives, etc.

Also, I cannot use .htaccess files.

+52
apache virtualhost
Mar 24 '09 at 21:44
source share
5 answers

You cannot use the include directive to include general rules. here

article

eg:.

<VirtualHost _default_:80> ... include conf/common_rule.conf </VirtualHost> <VirtualHost _default_:*> ... include conf/common_rule.conf </VirtualHost> <VirtualHost _default_:443> ... #SSL rules include conf/common_rule.conf </VirtualHost> 
+41
Mar 24 '09 at 22:02
source share

You can use any number of hosts and ports in one Virtualhost directive.

 <VirtualHost addr[:port] [addr[:port]] ...> ... </VirtualHost> 

In my case, I used.

 <VirtualHost *:80 *:443> ServerName loop.lk .... SSLEngine on SSLCertificateFile /etc/apache2/ssl/local.crt </VirtualHost> 
+31
Jun 13 '13 at 10:24
source share

Sorry to encounter such an old post, but in order to help other googlers, I wanted to share how I dealt with it:

I have a couple of vhosts on my localhost, say: localhost, foo.com, bar.com

This is a local site on my laptop (macosx), I could leave with self-signed certificates, and so the ssl part will be the same for all vhosts ...

I have done the following:

I created the directory /etc/apache2/extra/vhosts/ .

I created /etc/apache2/extra/vhosts/localhost.conf :

 ServerName localhost DocumentRoot "/www/localhost" <Directory /www/localhost> Require all granted </Directory> ErrorLog "/var/log/apache2/localhost.error_log" CustomLog "/var/log/apache2/localhost.access_log" common 

A /etc/apache2/extra/vhosts/foo.conf :

 ServerName foo.com DocumentRoot "/www/foo.com" <Directory /www/foo.com> Require all granted </Directory> ErrorLog "/var/log/apache2/foo.com.error_log" CustomLog "/var/log/apache2/foo.com.access_log" common 

A /etc/apache2/extra/vhosts/bar.conf :

 ServerName bar.com DocumentRoot "/www/bar.com" <Directory /www/bar.com> Require all granted </Directory> ErrorLog "/var/log/apache2/bar.com.error_log" CustomLog "/var/log/apache2/bar.com.access_log" common 

And finally, /etc/apache2/extra/vhosts/ssl.conf :

 SSLEngine on SSLCertificateFile "/etc/apache2/ssl/server.crt" SSLCertificateKeyFile "/etc/apache2/ssl/server.key" 

And in my /etc/apache2/extra/httpd-vhosts.conf :

 <VirtualHost *:80> Include /etc/apache2/extra/vhosts/localhost.conf </VirtualHost> <VirtualHost *:443> Include /etc/apache2/extra/vhosts/localhost.conf Include /etc/apache2/extra/vhosts/ssl.conf </VirtualHost> <VirtualHost *:80> Include /etc/apache2/extra/vhosts/foo.conf </VirtualHost> <VirtualHost *:443> Include /etc/apache2/extra/vhosts/foo.conf Include /etc/apache2/extra/vhosts/ssl.conf </VirtualHost> <VirtualHost *:80> Include /etc/apache2/extra/vhosts/bar.conf </VirtualHost> <VirtualHost *:443> Include /etc/apache2/extra/vhosts/bar.conf Include /etc/apache2/extra/vhosts/ssl.conf </VirtualHost> 
+7
Nov 05 '15 at 13:17
source share

You can put the general configuration in a separate file and include it in both VirtualHost segments. For example:

 <VirtualHost 192.168.1.2:80> Include conf/common.conf </VirtualHost> <VirtualHost 192.168.1.2:443> Include conf/common.conf (put your ssl specific cofiguration stuff here ...) </VirtualHost> 
+2
Mar 24 '09 at 22:12
source share

You can also specify general directives inside the container, and not inside yourself. This is what I do, mainly because I prefer mod_rewrite rules at the directory level rather than at the server level, but it should work equally well for you as well.

0
Mar 24 '09 at 23:11
source share



All Articles