几个月之前的记录,结果现在想在ubuntu17.10上重现一遍,竟然失败。有必要对apache反向代理进一步总结,深入学习。
1 第一次实验
1.1 试验环境
系统版本:
root@ubuntu-14-dev:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.4 LTS
Release: 14.04
Codename: trusty
apache版本:
root@ubuntu-14-dev:~# apachectl -v
Server version: Apache/2.4.7 (Ubuntu)
Server built: Sep 18 2017 16:37:54
用于当做反向代理的主机IP:192.168.80.156
用于提供应用的主机IP:192.168.80.157
(另一个apache服务)
安装方式
源码安装
二进制安装
sudo apt-get install apache2
(参考Ubuntu 14.04安装Apache)
1.2 反向代理
找到apache的配置目录,默认位于/etc/apache2/(注意这个是对照我的环境,不同的版本以下的配置目录不同)
.
|__apache2.conf
|__conf-available
| |__*.conf
|__conf-enable
| |__*.conf
|__mods-available
| |__*.conf
|__mods-enable
| |__*.conf
|__ports.conf
|__sites-available
| |__*.conf
|__sites-enable
|__*.conf
在mods-available/proxy.conf文件内修改
<IfModule mod_proxy.c>
# If you want to use apache2 as a forward proxy, uncomment the
# 'ProxyRequests On' line and the <Proxy *> block below.
# WARNING: Be careful to restrict access inside the <Proxy *> block.
# Open proxy servers are dangerous both to your network and to the
# Internet at large.
#
# If you only want to use apache2 as a reverse proxy/gateway in
# front of some web application server, you DON'T need
# 'ProxyRequests On'.
#ProxyRequests On
<Proxy *>
AddDefaultCharset off
Require all granted
#Require local
</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
ProxyPass "/test" "http://192.168.80.157:56785/"
ProxyPassReverse "/test" "http://192.168.80.157:56785/"
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
根据注释的提示,如果你想把apache2用作一个反向代理网关,用来代理某些web应用服务,把<proxy *>块的代码注释,不要注释掉ProxyRequests On
这里发现了一个以前的错误,反向代理不需要
ProxyRequest
需要注意的一点是,你还需要将Require all deny
修改为Require all granted
。查阅apache文档发现:
Require all granted
Access is allowed unconditionally.访问被无条件接受
Require all denied
Access is denied unconditionally.访问被无条件拒绝
然后利用ProxyPass和ProxyPassReverse来设置后端应用。
在192.168.80.155的主机浏览器中输入http://192.168.80.156/test
,出现的界面和输入http://192.168.80.157
相同。
最简反向代理配置
在试玩上述的改动之后,我又将配置精简为:
<IfModule mod_proxy.c>
ProxyPass "/test" "http://192.168.80.157:56785/"
ProxyPassReverse "/test" "http://192.168.80.157:56785/"
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
并使用隐身窗口登录,反向代理仍然成功。
1.3 正向代理
<IfModule mod_proxy.c>
# If you want to use apache2 as a forward proxy, uncomment the
# 'ProxyRequests On' line and the <Proxy *> block below.
# WARNING: Be careful to restrict access inside the <Proxy *> block.
# Open proxy servers are dangerous both to your network and to the
# Internet at large.
#
# If you only want to use apache2 as a reverse proxy/gateway in
# front of some web application server, you DON'T need
# 'ProxyRequests On'.
ProxyRequests On
# <Proxy *>
# AddDefaultCharset off
# Require all granted
# #Require local
# </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
ProxyPass "/" "http://192.168.80.157:56785/"
ProxyPassReverse "/" "http://192.168.80.157:56785/"
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
在IE设置
里点击连接
->局域网设置
- [x] 自动检测设置(A)
代理服务器
- [x] 为LAN使用代理服务器
地址 [gateway-ip]端口[gateway-port]
奇怪的是该设置对chrome同样有效
以上便是几个月前使用Ubuntu14进行反向代理的尝试。下面将对apache反向代理的一些指令和流程的说明。
...
表明未完成
首先解决问题吧。google了一篇博客总结的精炼——在Ubuntu17.04/17.10为Nginx设置Apache2反向代理
试了一下原来是Apache2的代理模块未启用:
a2enmod proxy
a2enmod proxy_http
然后重启:
systemctl restart apache2
我的问题解决了,就不乐意继续总结了。
先留个坑以后慢慢填
...