将Apache配置为正向代理

时间:2020-02-23 14:29:42  来源:igfitidea点击:

Apache可以配置为正向和反向代理。
普通代理(也称为前向代理)是位于客户端和原始服务器之间的中间服务器。
客户端配置为使用前向代理访问其他站点。
当客户端想要从原点服务器获取内容时,它将请求发送到代理命名原始服务器作为目标。
然后,代理请求来自原点服务器的内容并将其返回给客户端。

以下是我们如何将Apache配置为前向代理:

首先,我们需要启用代理,proxy_http和proxy_connect模块。
我们可以使用A2ENMOD命令执行此操作:

sudo a2enmod proxy proxy_http proxy_connect

接下来,转到"/etc/apache2/mods-enabled目录"并打开文件Proxy.conf。
取消注释 #ProxyRequests On 这行和 <Proxy *>这配置块。

现在,在/etc/apache2/sites-available目录中创建一个新文件。
我们将调用我们的文件forward_proxy.conf。
这是文件的配置:

<VirtualHost *:8080>
 # The ServerName directive sets the request scheme, hostname and port that
 # the server uses to identify itself. This is used when creating
 # redirection URLs. In the context of virtual hosts, the ServerName
 # specifies what hostname must appear in the request's Host: header to
 # match this virtual host. For the default virtual host (this file) this
 # value is not decisive as it is used as a last resort host regardless.
 # However, you must set it for any further virtual host explicitly.
 #ServerName www.example.com
ProxyRequests On
 ProxyVia On
<Proxy "*">
 Require ip 192.168
 </Proxy>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
 # error, crit, alert, emerg.
 # It is also possible to configure the loglevel for particular
 # modules, e.g.
 #LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error_forward_proxy.log
 CustomLog ${APACHE_LOG_DIR}/access_forward_proxy.log combined
# For most configuration files from conf-available/, which are
 # enabled or disabled at a global level, it is possible to
 # include a line for only one particular virtual host. For example the
 # following line enables the CGI configuration for this host only
 # after it has been globally disabled with "a2disconf".
 #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

以下是文件中的行的描述:

<VirtualHost *:8080>指定将用于此虚拟主机的端口。

ProxyRequests On,Proxyvia On 开启了代理。

<proxy"*">Require IP 192.168 </proxy>确定将允许使用代理的IP地址范围。
在我们的情况下,允许的主机的范围是192.168.0.0 192.168.255.255.

Errowlog ${apache_log_dir} /error_forward_proxy.log,customlog ${apache_log_dir} /access_forward_proxy.log combined 指定日志文件位置。

接下来,打开/etc/apache2/ports.conf文件并添加侦听8080行:

cat /etc/apache2/ports.conf

使用a2ensite命令启用站点:

sudo a2ensite forward_proxy.conf

重新启动Apache,以便更改生效。
Web客户端需要配置为使用Proxy进行外部连接。