Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Apache2 proxy to local port (See related posts)

Apache as the receptionist, forwarding requests to and from an internal server (e.g. webrick or lighttpd).

<VirtualHost *>
        ServerName www.example.com
        ProxyPass / http://localhost:3000/
        ProxyPassReverse / http://localhost:3000/
        ProxyPreserveHost On
</VirtualHost>


Change 3000 to whatever port you need and make sure the internal server is set up to answer requests on that port (not port 80). The ProxyPreserveHost line is critical to keep all your URLs working correctly.

Comments on this post

ysoldak posts on Mar 01, 2007 at 05:27
I see some problems with this. For example in case you want proxy for particular paths only... or simply change a port. I'd forced to change VirtualHost element each time.
In other words you forced to change apache's config files (/etc/apache2 in most *nix)

I have a solution I use at my server to proxy (not redirect!)
http://<servername>:80/folder1 -> http://<servername>:8080/folder2
and still be able to access other paths as usual. Please note both port and path are changed, while path in user's browser stays untouched (that I mean "not redirect").

Req:
apache2, mod_rewrite, mod_proxy, mod_proxy_http

Create following .htaccess file in /var/www/folder1
RewriteEngine   on
RewriteRule     (.*) http://localhost:8080/folder2/$1 [P]


No additional changes are required (except enabling .htaccess in apache2.conf if disabled) in /etc/apache2
comechao posts on May 31, 2007 at 10:48
If you want your app play nice with other files and some php app:

RewriteEngine   on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule     (.*) http://localhost:3000/$1 [P]

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts