Portents of Porting Ports
May 24, 2009 5:44 AM   Subscribe

ApacheFilter: What's the easiest way to have a "default page" on an alternate port?

I've got Comcast cable internet at home, and I run a web server with several VirtualHosts.
Thinking about switching to FiOS, where port 80 is blocked.
The simple solution is to move my web content over to a different port -- in this case 81.
I've already set up the server to Listen on both ports. That's the easy part.

What I want to do is, before port 80 goes away, put a special note and redirect notice when people access any of my pages, at any of my domains, at port 80, without having 2 different versions of every domain.

What is the easiest way to accomplish this?

It is Apache 2.2.3 on Ubuntu where I have full root privileges.
posted by jozxyqk to Computers & Internet (5 answers total) 1 user marked this as a favorite
 
Set all your virtualhosts to only listen to port 81 - apache will then default to the root of the htdocs folder for everything else; if not, put back in a section, or wherever your server-wide settings live in conf.httpd. Apache will get the incoming requested URI and try to interpret it; not finding the right site in the virtualhosts parts, it will default down to the htdocs folder, or wherever your top directory is in httpd.conf, regardless of the incoming domain name - Apache will always try to serve something to an incoming connection, even if it doesn't make sense, and it will usually send it to htdocs, but it depends on how much your httpd.conf is modified; if you've just added virtualhost sections to the end or in a separate file, this is the default behavior I believe. In the htdocs folder, have just an index file which gives a generic site-change message, or in PHP you can use the request_uri variable to do something fancier. This way, it'll also be 'working right' when port 80 gets closed.
posted by AzraelBrown at 6:53 AM on May 24, 2009


Response by poster: AzraelBrown, I thought it was "as simple as that", but it gave me a warning message from Apache about "using * and non-* ports with a NameVirtualHost directive".
By adding a NameVirtualHosts line with a specific reference to port 81, I've made the problem go away, and I think I'm good from here.

I also thought there might have been some RewriteRule nonsense I could have done instead; other answers still appreciated.
posted by jozxyqk at 8:00 AM on May 24, 2009


The config AzraelBrown describes is definitely the simplest way, because you only need to make that one change in your config file, turning NameVirtualHost on for port 81 and off for all other ports. The only other thing you would need to do is add
UseCanonicalName off
to the config for the default (port 80) host. That will cause Apache to allow the client-specified HTTP_HOST header through, and you can use that in PHP or other server-side scripting to redirect to the appropriate site.

There are other ways to do it, although none as easy as that. If you want to use mod_rewrite, you could use the following in each vhost:
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .* http://%{HTTP_HOST}:81/%{REQUEST_URI}?fromport80=true [QSA]

This says "If the client is using port 80, redirect them to port 81 and add to the query string the variable "fromport80=true". You could then add code to each site to check for fromport80 and warn them they'd better start using port 81.
posted by pocams at 8:56 AM on May 24, 2009


Just always use * syntax and let it pick which vhost to use based on ServerName/ServerAlias. This is a lot easier than naming each virtual host.

NameVirtualHost *:80
NameVirtualHost *:81

<VirtualHost *:80>
    DocumentRoot /path/to/message/docroot
</VirtualHost>

<VirtualHost *:81>
    ServerName foo.com
    ...
</VirtualHost>

<VirtualHost *:81>
   ServerName bar.org
   ...
</VirtualHost>


etc..
posted by cj_ at 1:12 PM on May 24, 2009


Response by poster: I just got an auto-notification that I should resolve this or set a "best answer"...
But it turns out that the question is moot for 2 reasons:
1) As of fairly recently, FiOS no longer blocks port 80 (which I found out by accident with my friend-with-FiOS's help)
2) FiOS isn't available on my street after all.

Still some good suggestions.
posted by jozxyqk at 5:26 PM on June 23, 2009


« Older Google's services don't work in a specific way   |   Rock the Casb-AHHHHGH!! Newer »
This thread is closed to new comments.