Apache Rewrite or Proxy to internal server
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 [email protected]
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
You're very close!
A couple notes:
-
RewriteCond %{HTTP_HOST} ^mysub.domain.com/*
- TheHTTP_HOST
variable only containsmysub.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 ofRewriteRule
.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 useProxyPass
. 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!