在 apache 中设置基本的 Web 代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1997001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:33:44  来源:igfitidea点击:

Setting up a basic web proxy in apache

apacheproxy

提问by mark

I'm looking to run Apache as a proxy for web development. I'm running Mac OS X 10.5.4, which already has Apache 2.2.8 installed and running.

我希望运行 Apache 作为 Web 开发的代理。我正在运行 Mac OS X 10.5.4,它已经安装并运行了 Apache 2.2.8。

I'd like to point my JavaScript files (which are running locally on my machine) to:

我想将我的 JavaScript 文件(在我的机器上本地运行)指向:

http://localhost/test.php

which would hit the local apache server, then have that apache instance forward to my real remote server:

这将访问本地 apache 服务器,然后将该 apache 实例转发到我真正的远程服务器:

http://www.mysite.com/test.php

I've looked at a few walkthroughs but they seem to be out of date. I'm wondering if there's a recent how-to on setting this up - the doc here:

我看过一些演练,但它们似乎已经过时。我想知道是否有最近的设置方法 - 此处的文档:

http://httpd.apache.org/docs/2.0/mod/mod_proxy.html

http://httpd.apache.org/docs/2.0/mod/mod_proxy.html

gives a basic example, but I'm not sure where that text should be added - to http.conf? Do I simply add it there, then restart the server?

给出了一个基本示例,但我不确定应该在哪里添加该文本 - 到 http.conf?我是否只需将其添加到那里,然后重新启动服务器?

Thanks

谢谢

回答by Daniel Vassallo

The proxy setup that you describe is called a Reverse Proxy.

您描述的代理设置称为反向代理

This is very easy to set up in Apache, by using the mod_proxymodule.

通过使用mod_proxy模块,这在 Apache 中非常容易设置。

The fundamental mod_proxy directive to set up a reverse proxy is the ProxyPass. You would typically add the following line to your local Apache configuration file (usually httpd.confor apache2.conf):

设置反向代理的基本 mod_proxy 指令是ProxyPass. 您通常会将以下行添加到本地 Apache 配置文件(通常为httpd.confapache2.conf):

ProxyPass     /remote/     http://www.mysite.com/

In this case, the browser would be requesting http://localhost/remote/test.phpbut your local Apache server would serve this by acting as a proxy to http://www.mysite.com/test.php.

在这种情况下,浏览器将http://localhost/remote/test.php发出请求,但您的本地 Apache 服务器将通过充当http://www.mysite.com/test.php.

You also need to make sure to have the following configuration lines uncommented in your Apache config file:

您还需要确保在 Apache 配置文件中取消注释以下配置行:

LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so

Make sure to restart your local Apache service after you do any changes to the config file.

对配置文件进行任何更改后,请确保重新启动本地 Apache 服务。