Ubuntu 12.04使用Apache的ProxyPass配置反向代理

在某些情况下,虽然Apache已经能满足其大多数通用Web服务需求,但其他Web或应用程序服务器更适合某些任务。 幸运的是,很容易配置Apache将某些请求传递到其他Web服务器进程。 这些辅助(或第三)web服务器可以在相同的服务器或单独的节点(可能通过专用网络)上运行。 我们的示例使用lighttpd作为辅助Web服务器,但它们也可以代理HTTP请求到任何Web服务器或应用程序。

激活Proxy模块



按如下内容编辑/etc/apache2/mods-available/proxy.conf:

  1. <IfModule mod_proxy.c>
  2.         #turning ProxyRequests on and allowing proxying from all may allow
  3.         #spammers to use your proxy to send email.
  4.  
  5.         ProxyRequests Off
  6.  
  7.         <Proxy *>
  8.                 AddDefaultCharset off
  9.                 Order deny,allow
  10.                 Allow from all
  11.         </Proxy>
  12.  
  13.         # Enable/disable the handling of HTTP/1.1 "Via:" headers.
  14.         # ("Full" adds the server version; "Block" removes all outgoing Via: headers)
  15.         # Set to one of: Off | On | Full | Block
  16.  
  17.         ProxyVia On
  18. </IfModule>

这将启用代理支持。
下一步,执行如下命令:

  1. a2enmod proxy
  2. a2enmod proxy_http
  3. service apache2 restart

Apache应该能正常重新启动。如果您遇到任何问题,您可以检查/var/log/apache2/下的日志以获取更多信息。

代理一个域名到Lighttpd



我们已经有一个名为“www.firstsite.org”的网站作为正常的虚拟主机在Apache下运行。 我们将使用Apache将对“www.secondsite.org”网站的请求发送到lighttpd,lighttpd运行在端口8080上。 这里是“www.secondsite.org”的配置文件:
/etc/apache2/sites-available/www.secondsite.org:

  1. <VirtualHost *:80>
  2.      ServerAdmin support@secondsite.org
  3.      ServerName secondsite.org
  4.      ServerAlias www.secondsite.org
  5.  
  6.      ProxyPass / http://localhost:8080/
  7.  
  8.      # Uncomment the line below if your site uses SSL.
  9.      #SSLProxyEngine On
  10. </VirtualHost>

ProxyPass指令告诉Apache将此域的所有请求转发到在端口8080上运行的Web服务器。如果我们的目标服务器在另一个服务器上运行,我们可以指定地址。 我们将使用以下命令启用该网站:

  1. a2ensite www.secondsite.org
  2. service apache2 reload

代理指定URL到Lighttpd



如果我们想把http://www.firstsite.org/myapp/的请求转向 lighttpd,我们只需修改其配置文件,如下所示:
/apache2/sites-available/www.firstsite.org:

  1. <VirtualHost *:80>
  2.      ServerAdmin support@firstsite.org
  3.      ServerName firstsite.org
  4.      ServerAlias www.firstsite.org
  5.      DocumentRoot /srv/www/firstsite.org/public_html/
  6.      ErrorLog /srv/www/firstsite.org/logs/error.log
  7.      CustomLog /srv/www/firstsite.org/logs/access.log combined
  8.  
  9.      ProxyPass /myapp http://localhost:8080/
  10. </VirtualHost>
标签:UbuntuApache 发布于:2019-11-21 11:58:50