Apache2, NameVirtualHosts, SSL and SERVER_PORT

I was experiencing odd things integrating to google maps - although I have both an API key for http and https - only https was working.

It turns out that the setting of the apache environment variable SERVER_PORT was always 443 irrespective of whether I was using http/80 or https/443 from the client.

I have several NameVirtualHosts on different IPs.

In apache2.conf:


NameVirtualHost 78.47.168.122:80
NameVirtualHost 78.47.168.123:80
NameVirtualHost 78.47.168.124:80
NameVirtualHost 78.47.168.124:443
NameVirtualHost 78.47.168.125:80
NameVirtualHost 78.47.168.125:443
NameVirtualHost 78.47.168.126:80
NameVirtualHost 78.47.168.126:443

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/

And in one of my more complex virtual files I had the following:


<VirtualHost 78.47.168.125:80 78.47.168.125:443>

This was making the site available under both http and https - and for https only functions I was using the SSLRequireSSL statement.

But - by using the following test code (thanks once again to Steve Kemp of www.debian-administration.org):


#!/usr/bin/perl
print "Content-Type: text/plain\n\n";

foreach my $key ( sort keys %ENV )
{
    print "Key $key " . $ENV{$key} . "\n";
} 

I could show that SERVER_PORT was not correct when accessing via HTTP on port 80.

By changing that single virtual host config to have two <VirtualHost> sections - one for each port - this issue goes away. However - I do end up with duplicated config:


<VirtualHost 78.47.168.125:80>
  HTTP only site config here
</VirtualHost>
<VirtualHost 78.47.168.125:443>
  Complete site config here (all the ssl stuff plus a duplicate of the http stuff above)
</VirtualHost>

Reply

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <p> <pre> <hr>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Insert Flickr images: [flickr-photo:id=230452326,size=s] or [flickr-photoset:id=72157594262419167,size=m].
  • Replace [debbug:xxxxx] with a link to the relevant debian bug.
  • Images can be added to this post.
  • You can link nodes to other nodes using the following syntax:
    [node:node_id,title="val2"]
  • You may link to Gallery2 items on this site using a special syntax.
  • Insert Google Map macro.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

worried