Apache 根据 http 方法限制 proxypass

Apache 根据 http 方法限制 proxypass

我对 Apache 还很陌生,我有两个服务,一个用于读取,一个用于写入,并且两个服务都支持相同的位置。但这种路由似乎不起作用。我能否根据 Http 方法限制这些服务

 <VirtualHost *:1000>
     ServerAdmin [email protected]
     DocumentRoot "C:/Apache24/htdocs"
     ServerName localhost:1000
     ErrorLog "logs/example.com-error.log"
     CustomLog "logs/example.com-access.log" common
     ForensicLog "logs/forensic-log.log"
     ProxyRequests Off
     ProxyPreserveHost On
  LogLevel debug
     <Proxy *>
        Require all granted
         
     </Proxy>

     #ReadService only supports GET
     <Location /api/users>
         ProxyPass        http://localhost:5000/api/users connectiontimeout=5 timeout=300
         ProxyPassReverse hhttp://localhost:5000/api/calls
     </Location>
     
     #WriteService anything other than GET
 
     <Location /api/users>
         ProxyPass        http://localhost:7000/api/users connectiontimeout=5 timeout=300
         ProxyPassReverse http://localhost:7000/api/users
     </Location>
     
     
 </VirtualHost>

答案1

RewriteCond %{REQUEST_METHOD} ^(GET)$
RewriteCond %{REQUEST_URI} ^/api/users(.*)$
RewriteRule ^/api/users(.*)$  http://localhost:5000/api/users$1  [P]

RewriteCond %{REQUEST_METHOD} !^(GET)$
RewriteCond %{REQUEST_URI} ^/api/users(.*)$
RewriteRule ^/api/users(.*)$  http://localhost:7000/api/users$1  [P]

答案2

我还没有测试过,但你可以尝试下面的方法。你需要调整 ROUTEID 的条件设置。如果标头不起作用,你将不得不尝试各种示例中概述的 cookie。

<IfModule mod_headers.c>
    <If "%{REQUEST_METHOD} =GET">
        Header set ROUTEID: "1"
    </If>
    <Else>
        Header set ROUTEID: "2"
    </Else>
</IfModule>

ProxyPass        "/api/users" "balancer://mycluster"
ProxyPassReverse "/api/users" "balancer://mycluster"

<Proxy "balancer://mycluster">
    BalancerMember "http://localhost:5000/api/users" route=1
    BalancerMember "http://localhost:7000/api/users" route=2
    ProxySet stickysession=ROUTEID
</Proxy>

让我们知道怎么回事。

相关内容