Puppetlabs-Apache: enable both 80 and 443 for virtualhost

1.4k Views Asked by At

I'm relatively new to puppet and currently working on 'puppetlabs-apache' module. I'm missing something while setting both ssl and non-ssl on a virtual-host.

Manifest applied:

include apache
include apache::mod::rewrite

#apache::vhost { 'site.mydomain.com':
# port         => '80',
# docroot      => '/var/www/site',
# rewrite_rule => '(.*) https://site.mydomain.com [R,L]',
#}

apache::vhost { 'site.mydomain.com':
  port          => '443',
  ssl           => true,
  docroot       => '/var/www/site',
  docroot_owner => 'www-data',
  docroot_group => 'www-data',
#  rewrite_rule  => '(.*) https://site.mydomain.com [R,L]',
}

The thing is I don't need the non-ssl (80 port), but all requests should redirect to 443.

If I comment out the first vhost definition of site.mydomain.com for port 80, it throws an error:

Error 400 on SERVER: Duplicate declaration: Apache::Vhost[site2.mydomain.com] is already declared in file..

Not sure what I'm missing here. What should I do to make this permanent redirect happen?

http://site2.mydomain.com/ => https://site2.mydomain.com/
2

There are 2 best solutions below

5
Matthew Schuchard On BEST ANSWER

To configure a virtual host to redirect unencrypted connections to SSL, declare them with separate apache::vhost defined types and redirect unencrypted requests to the virtual host with SSL enabled:

apache::vhost { 'site.mydomain.com:80':
  servername      => 'site.mydomain.com',
  port            => '80',
  docroot         => '/var/www/site',
  rewrite_rule    => '(.*) https://site.mydomain.com [R,L]',
  redirect_status => 'permanent',
  redirect_dest   => 'https://site.mydomain.com'
}

apache::vhost { 'site.mydomain.com:443':
  servername    => 'site.mydomain.com',
  port          => '443',
  ssl           => true,
  docroot       => '/var/www/site',
  docroot_owner => 'www-data',
  docroot_group => 'www-data',
  rewrite_rule  => '(.*) https://site.mydomain.com [R,L]',
}

You also needed those additional redirect attributes for the non-ssl virtualhost resource. Since apache::vhost is a defined resource type with no namevar, you can circumvent the multiple resource declaration issue by using two unique and purely cosmetic resource titles.

0
Vineeth Vijayan On

Working out Matt's answer and error while running it made me come at following answer.

apache::vhost { 'site.mydomain.com:80' ... } 

apache::vhost { 'site.mydomain.com:443' : ...}

Thanks,