我在 RHEL7.5 上使用 apache 2.4.6 设置反向代理时遇到了问题。
我有以下虚拟主机,它将请求发送到 3 个后端:
<VirtualHost *:80>
ServerName www.example.com
RewriteEngine On
ProxyPreserveHost On
<Proxy balancer://backend8093>
BalancerMember http://backend01:8093 route=1
BalancerMember http://backend02:8093 route=2
BalancerMember http://backend03:8093 route=3
ProxySet lbmethod=bybusyness
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
ProxySet stickysession=ROUTEID
</Proxy>
# v1, works
#RewriteRule ^/scheduler/(.*) proxy:balancer://backend8093/scheduler/$1 [L]
# v2, doesn't work
ProxyPass "/scheduler" "balancer://backend8093/scheduler"
ProxyPassReverse "/scheduler" "balancer://backend8093/scheduler"
RewriteRule .* http://failure [R,L]
</VirtualHost>
然而,当我尝试卷曲时,我遇到了最后一条规则:
# curl -vvv -H "host: www.example.com" localhost/scheduler/xxx
* About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /scheduler/xxx HTTP/1.1
> User-Agent: curl/7.29.0
> Accept: */*
> host: www.example.com
>
< HTTP/1.1 302 Found
< Date: Thu, 07 Jun 2018 16:04:46 GMT
< Server: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.2k-fips
< Location: http://failure
< Content-Length: 198
< Content-Type: text/html; charset=iso-8859-1
这意味着该ProxyPass
指令与我的请求不匹配。我也尝试过将ProxyPass*
指令包装在<Location>
指令中,但也没有用。
尝试等效规则(v1),RewriteRule
效果与预期一致,在 3 个服务器之间平衡请求:
# curl -vvv -H "host: www.example.com" localhost/scheduler/xxx
* About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /scheduler/xxx HTTP/1.1
> User-Agent: curl/7.29.0
> Accept: */*
> host: www.example.com
>
< HTTP/1.1 200 OK
< Date: Thu, 07 Jun 2018 16:06:34 GMT
< Server: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.2k-fips
< Last-Modified: Thu, 07 Jun 2018 13:20:51 GMT
< ETag: "11-56e0d26f87639"
< Accept-Ranges: bytes
< Content-Length: 17
< Set-Cookie: ROUTEID=.1; path=/
<
backend01 hello
我可以看到请求与正确的匹配VirtualHost
,因为我看到了重定向,但我无法理解为什么ProxyPass
似乎被忽略了。
我已经检查过并且 apache 正在加载模块(处理server-info
程序也确认了):
# httpd -M | grep proxy
proxy_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)
我还有其他类似设置的服务器,在 RHEL7.4 上运行 apache v2.4.6,在 RHEL6.5 上运行 apache 2.2.15,都可以运行。我找不到 RHEL7 服务器之间加载的模块有什么区别,但仍然有一个可以运行,而另一个不能。
可能配置上有一些我没注意到的差异。配置大部分是默认的,只对文件进行了更改/etc/httpd/conf.d
(上面的虚拟主机包含在其自己的文件中)。
这里出了什么问题?为什么指令不起作用ProxyPass
?
答案1
RewriteRule 在运行时先于 ProxyPass 进行处理。通用 RewriteRule 将匹配所有内容。删除该规则并改用 ErrorPage 指令。