Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

First off, this is my first go at apache, so please forgive my beginingingismness :)

My basic setup is as such: mysub.domain.com gets sent to my static IP via a CNAME entry at godaddy's DNS manager. It hits my Ubuntu 10 LTS server running Apache2.

I have a virtual host entry that directs that request to the proper /var/www/mysub folder. I don't have any content in there, but I added a line to the "It Works" page so I'd know if I got there successfully. I also have a Mac Mini running a wiki server on the same local network as the Ubuntu server.

I'd like mysub.domain.com to hit my Mini server instead of the /var/www/mysub folder.

After much reading on this site and others, I've managed to do it... kind of.

I have the following in my /var/www/mysub/.htacess, which I found in another SF question (forgot to copy the link).

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysub.domain.com/*
RewriteRule .* http://192.168.x.x/ [P,L]

This works insomuch as it does redirect mysub.domain.com to the Mini's front page. But of course, so does every subsequent link click on the Mini page. I think I understand why it's doing it (anything that starts with mysub.domain.com gets directed to what is essentially the front page of the wiki server, and since subsequent links on the wiki server also include mysub.domain.com, it always ends up in the same place)

I just don't know what to do different. To be perfectly honest, I don't actually understand the syntax of those Rewrite lines.


I've seen countless examples of config entries and tried some of them, but without really understanding the syntax, it's kind of shooting in the dark.

This was a useful post, and after reading this question, I tried adding this to my /apache2/httpd.conf file

<Location />
   ProxyPass http://192.168.x.x
   ProxyPassReverse http://192.168.x.x
</Location>

No luck.

Clearly, I have some learning to do, but it would seem to me that what I want to do is probably quite simple. What am I missing?


EDIT PER COMMENTS

My /etc/apache2/httpd.conf file

ServerName localhost

<VirtualHost *:80>
   ServerName domain.com
   ServerAlias www.domain.com
   DocumentRoot /var/www/domain
</VirtualHost>

<VirtualHost *:80>
   ServerName mysub.domain.com
   DocumentRoot /var/www/mysub
   <Location />
      ProxyPass http://192.168.x.x/
      ProxyPassReverse http://192.168.x.x/
   </Location>
</VirtualHost>

... and my sites-available/mysub file...

<VirtualHost *:80>
        ServerAdmin me@domain.com
        ServerName mysub.domain.com
        DocumentRoot /var/www/mysub

        #ProxyRequests Off
        <Location />
                ProxyPass http://192.168.1.50/
                ProxyPassReverse http://192.168.1.50/
        </Location>
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/mysub>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/www/mysub/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

Output of apache2ctl -S

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server 66-152-109-110.tvc-ip.com (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost 66-152-109-110.tvc-ip.com (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost domain.com (/etc/apache2/sites-enabled/domain:1)
         port 80 namevhost mysub.domain.com (/etc/apache2/sites-enabled/mysub:1)
Syntax OK
share|improve this question
    
Nice first question :-). –  Tom Marthenal Aug 1 '12 at 3:31
    
@TomMarthenal Cheers! –  JoshP Aug 1 '12 at 4:46

2 Answers 2

up vote 1 down vote accepted

You're very close!

A couple notes:

  • RewriteCond %{HTTP_HOST} ^mysub.domain.com/* - The HTTP_HOST variable only contains mysub.domain.com, not the rest of the path.

    This rule actually matches, but accidentally - there's no / character there, but the * modifier applies to the / character, meaning "repeat the / 0 to infinite times".

    Apache uses perl-compatible regex - to match the exact host, it should look like this:

    RewriteCond %{HTTP_HOST} ^mysub\.domain\.com$
    
  • RewriteRule .* http://192.168.x.x/ [P,L] - This is only loading the home page since it's not including the rest of the passed path - this must be manually done when using the [P] flag of RewriteRule.

    This should work:

    RewriteRule (.*) http://192.168.x.x/$1 [P,L]
    
  • The ProxyPass setup is almost right, except it's being overridden by the setup in the .htaccess file, so it's not being used. Using .htaccess is bad for performance and potentially problematic for security - see the recommendation in the Apache documentation here.

    Probably the best approach is to delete the .htaccess file outright, and just use ProxyPass. Change your config a small bit...

    <Location />
       ProxyPass http://192.168.x.x/
       ProxyPassReverse http://192.168.x.x/
    </Location>
    

    ...and move it from your httpd.conf over to within the <VirtualHost> block that's serving the subdomain.

    With the matching trailing slashes and no more .htaccess, this should do the trick!

share|improve this answer
    
Thanks so much for the answer! I've axed the .htaccess file. I've altered the <Location> block with the trailing slashes. The URL still just directs me to the /var/www/mysub "It Works" page. One thing I may be doing wrong here... My <VirtualHost> block is IN my httpd.conf file. When you say to move it, is my <VirtualHost> block in the wrong place? –  JoshP Aug 1 '12 at 2:29
    
@Josh The "typical" way to create a virtual host on an Ubuntu system is to create a file like /etc/apache2/sites-available/site-name then run a2ensite site-name to enable it (which creates a symlink in /etc/apache2/sites-enabled). It still works just fine in httpd.conf, though. Go ahead and move the <Location> block into the <VirtualHost> block and restart Apache. If that doesn't do the trick, can you provide the <VirtualHost> block, as well as the output from apache2ctl -S? (edit the extra info into your question as opposed to putting it in a comment, for formatting) –  Shane Madden Aug 1 '12 at 2:35
    
I actually do have it created in sites-available and linked to -enabled. I'll post both in the question. Is it wrong to have the site defined in both places? –  JoshP Aug 1 '12 at 2:47
    
@Josh You'll only want it defined in one location - that may be part of the problem. –  Shane Madden Aug 1 '12 at 2:51
    
Ok, is it "typical" then, to have it all defined in the sites-available? Shall I remove the <VirtualHost> blocks in the httpd.conf file? Would that then leave only the ServerName localhost entry? –  JoshP Aug 1 '12 at 3:05

This was the final piece of the puzzle. Found it as the answer over at this question.

The solution is to have this in /etc/apache2/mods-enabled/proxy.conf:

<IfModule mod_proxy.c>
ProxyRequests Off
<Proxy *>
  AddDefaultCharset off 
  Order deny,allow
  Allow from all 
</Proxy>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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