帮助中心 >  技术知识库 >  网站相关 >  网站运营 >  Ubuntu 12.04使用Apache的ProxyPass配置反向代理

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

2017-03-23 00:49:28 6732

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


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


激活Proxy模块


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

<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
                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>

这将启用代理支持。

下一步,执行下命令:

a2enmod proxy
a2enmod proxy_http
service apache2 restart

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

代理一个域名到Lighttpd

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

/etc/apache2/sites-available/www.landui.com:

<VirtualHost *:80>
     ServerAdmin support@secondsite.org
     ServerName secondsite.org
     ServerAlias www.landui.com
 
     ProxyPass / http://www.landui.com:8080/
 
     # Uncomment the line below if your site uses SSL.
     #SSLProxyEngine On
</VirtualHost>

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

a2ensite www.landui.com
service apache2 reload

代理指定URL到Lighttpd

如果我们想把http://www.landui.com/myapp/的请求转向 lighttpd,我们只需修改其配置文件,如下所示:

/apache2/sites-available/www.landui.com:

<VirtualHost *:80>
     ServerAdmin support@firstsite.org
     ServerName firstsite.org
     ServerAlias www.landui.com
     DocumentRoot /srv/www/firstsite.org/public_html/
     ErrorLog /srv/www/firstsite.org/logs/error.log
     CustomLog /srv/www/firstsite.org/logs/access.log combined
 
     ProxyPass /myapp http://www.landui.com:8080/
</VirtualHost>


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: