Main reason for writing this is, I've basically used Apache2 on windows and configuration is bit different then how it's on Debian. In windows typically you will find all configuration under httpd.conf file where you can load any particular module using LoadModule. This is not how Apache2 works exactly with Debian. With a typical install, you will find httpd.conf totally empty. Also loading modules dynamically does not work by having LoadModule declaration.
Without going much into details of these differences, I'll just write down how to setup reverse proxy...
- Load required modules - proxy, proxy_http, proxy_connect
sudo a2enmode proxy_connectAbove will automatically enable proxy module.
sudo a2enmode proxy_http
- Configure Site - Change your directory to the site for which you want to setup reverser proxy. For simplicity, I'll refer to the default site located at /etc/apache2/sites-enabled. You need to have root privilege to modify the site configuration. Below is the typical content for the default site i.e. 000-default
<virtualhost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www <directory /> Options FollowSymLinks AllowOverride None </Directory> <directory /var/www/> 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/apache2/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> </VirtualHost>
To setup a reverse proxy for URL http://localhost:8080/myapp/ please add following lines at the end of the VirtualHost tag as displayed above...
ProxyPass /myapp/ http://localhost:8080/myapp/
ProxyPassReverse /myapp/ http://localhost:8080/myapp/
- Enable access - To allow access for all requests to this proxy, edit /etc/apache2/mods-available/proxy.conf. By default access to all is denied. To enable the access, put line Allow from all. Typical proxy.conf after this change will look something like...
<IfModule mod_proxy.c> #turning ProxyRequests on and allowing proxying from all may allow #spammers to use your proxy to send email. ProxyRequests Off <Proxy *> AddDefaultCharset off Order deny,allow Deny from all Allow from all </Proxy> # Enable/disable the handling of HTTP/1.1 "Via:" headers. # ("Full" adds the server version; "Block" removes all outgoing Via: headers) # Set to one of: Off | On | Full | Block ProxyVia On </IfModule>
Note the line ProxyRequests Off. This is important in order to enable reverse proxy
- Restart Apache2 - To get all of above configuration changes in effect, have a clean restart. Recommended way is...
sudo apache2ctl graceful
- Done !!! Happy reverse proxying