Apigee - Targetendpoints & TargetServers

When Target Servers are pure IP address (which would be the IP address of the target lb); what must be done to have apigee properly set the host header to the targetserver to be the request.header.host and NOT the IP Address? (and even if I use a DNS entry; I'd want the host to be the request.header.host and not the target.header.host) 

Solved Solved
0 12 405
1 ACCEPTED SOLUTION

The AM policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-host">
    <DisplayName>AM-host</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>target.header.host</Name>
        <Ref>request.header.host</Ref>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

The target flow XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>AM-host</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <HTTPTargetConnection>
        <URL>https://httpbin.org</URL>
    </HTTPTargetConnection>
</TargetEndpoint>

View solution in original post

12 REPLIES 12

Try using an AssignMessage policy in the target preflow.

You can set the target.request.host and try pointing it at the request.header.host eg

<AssignVariable>
<Name>target.header.host</Name>
<Ref>request.header.host</Ref>
</AssignVariable>

yeah; I tried that as well as javascript;  context.setVariable('target.header.host', "somehost.company.com") - using a target of "http://mocktarget.apigee.net/echo" and the above example header.host - the response in the headers (in the request) still have mocktarget and not "somehost".

This is important in my case because I'd rather set the host header and talk directly to the IP rather than take another round of DNS. (other reasons as well but keeping this request as simple as possible)

When I use the IP address; I get the IP address in the host header instead of the request header (or even for testing - a static value).

 

"

Are you sure your policy is in the target preflow and not the proxy preflow? 

correct - I tried all 4 locations, pre/post proxy and target just to ensure I would see which if any would work and use static values <Value> in each just to minimize any potential problems. (I tried all 4 locations for both javascript and the AM).  Although I didn't like using the AM since that won't work for my lower environments on "base"; but I'm willing to try anything to get the host header working and then going from there.  I've read somewhere that this is a protected header; for additional information - if I change the name to target.header.host-2 or target.header.test - it works without a problem and shows up in the /echo.

Using AssignMessage with target.header.host worked for me on a request to httpbin on Apigee X.

so if you changed your target server to point to mockapigee / echo - you'd see that your host header was updated to reflect whatever you set it to (in this case the request header)?  I wonder why this would work for your setup and but ours.  Everything is fairly basic; are you able to share the proxy/policy/flow/target endpoint and target server config?

The AM policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-host">
    <DisplayName>AM-host</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>target.header.host</Name>
        <Ref>request.header.host</Ref>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

The target flow XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>AM-host</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <HTTPTargetConnection>
        <URL>https://httpbin.org</URL>
    </HTTPTargetConnection>
</TargetEndpoint>

I wonder if targetserver is causing my issue; since I use a targetserver so that I can use this for multiple environments - it shouldn't but I'll dig into this more. thank you for the detailed response!

I switched mine to targetserver also, but overriding the host header still works. Good observation though

For my lower environments; I'm using base; do you know of the best way to accomplish this without extensible policies?

I'm not sure what you mean by using base;, and for new questions it would be great to create a new post

There are three types of "environments" - base, intermediate, and comprehensive. All at different price tiers.  so I was referring to the lowest tier which is called "base" - which doesn't allow for extensible policies.