Apache2 reverse proxies

来自Ubuntu中文
跳到导航跳到搜索

This solution solves the problem of having multiple servers on a LAN which has a single router connected to the Internet. The router forwards all port 80 traffic to a single primary server. That server will then be required to act as a proxy for the other servers on the LAN, redirecting incoming traffic addressed to the URLs of those other servers to their respective LAN IP addresses.

This increases the amount of traffic passing through the primary server, so is not a recommended solution for high volume situations unless the primary server is a dedicated gateway/proxy server.

This method uses Apache2 virtual host configuration files on the primary server (to which the router sends port 80 traffic).

  • On the primary server (which will act as the proxy), create a symbolic link to enable the proxy modules in Apache2, then restart Apache2:
sudo ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled
sudo ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled
sudo /etc/init.d/apache2 restart
  • Edit a virtual host file for all secondary servers:
sudo gedit /etc/apache2/sites-enabled/proxiedhosts
and edit the file so that it resembles:
<VirtualHost *:80>
#
ServerName internalserver2.mydomain.org
#
    ProxyPreserveHost On
    ProxyRequests off
    ProxyPass / http://192.168.1.192/
    ProxyPassReverse / http://192.168.1.192/
#
</VirtualHost>
#
#<VirtualHost *:80>
#
#ServerName internalserver3.mydomain.org'
#
#     ProxyPreserveHost On
#     ProxyRequests off
#     ProxyPass / http://192.168.1.193/
#     ProxyPassReverse / http://192.168.1.193/
#
#</VirtualHost>
#
#<VirtualHost *:80>
#
#ServerName internalserver4.mydomain.org'
#
#     ProxyPreserveHost On
#     ProxyRequests off
#     ProxyPass / http://192.168.1.194/
#     ProxyPassReverse / http://192.168.1.194/
#
#</VirtualHost>
Makes each URL for each server has an entry (and obviously remove the hashmarks for each one that is active).
  • Activate the virtual host file by making a symbolic link to the Apache2 sites-enabled folder then restarting Apache2:
sudo ln -s /etc/apache2/sites-enabled/proxiedhosts /etc/apache2/sites-enabled
sudo /etc/init.d/apache2 restart

Other resources

The information for this page was synthesized from these sources: