DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Apache2 Proxy To Local Port

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

<VirtualHost *>
        ProxyPass / http://localhost:3000/
        ProxyPassReverse / http://localhost:3000/
        ProxyPreserveHost On

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.    


Snippets Manager replied on Sat, 2011/08/13 - 10:30pm

Works perfectly.. but, at least on Ubuntu with apache2 you need to sudo a2enmod proxy_http (and maybe a2enmod proxy too!)

Snippets Manager replied on Thu, 2007/05/31 - 9:42am

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]

Yuri Soldak replied on Thu, 2007/03/01 - 4:58am

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://:80/folder1 -> http://: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