Paul Deen
Paul Deen

Reputation: 455

Apigee Proxy passing same host header to target

I have a simple apigee proxy, but I can see in the trace an issue where the Host header going to the target contains the host of the proxy itself.

i.e. the target gets

Host: xx.apigeename.com

rather than:

Host: my.awsservername.com

The target is on a different domain to the proxy, so it means that the target server is improperly handling the request (404 in this case).

Why is it that Apigee might be sending the exact same host header and not transforming it?

I have tried to explicitly set it, by setting the TargetEndpoint:

<TargetEndpoint name="xyzzy">
    <Description/>
    <FaultRules/>
    <PreFlow name="PreFlow">
        <Request>
            <Headers>
                <Header name="Host">{target.host}</Header>
            </Headers>
        </Request>
        <Response>
        </Response>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows/>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://{targetBackend}/xyzzy</URL>
        <SSLInfo>
            <Enabled>true</Enabled>
            <Protocols>
                <Protocol>TLSv1.2</Protocol>
            </Protocols>
        </SSLInfo>
    </HTTPTargetConnection>
</TargetEndpoint>

The documentation Apigee has on this seems very vague.

It's getting super frustrating. We have other proxies that are working fine without doing anything special.

Upvotes: 2

Views: 2448

Answers (2)

hey
hey

Reputation: 3272

For Apigee X, the AssignMessage -> Set option does not work. But two other options are effective in Target endpoints -> PreFlow:

  1. Javascript: context.setVariable('target.header.host',"my.host.name")

  2. AssignMessage Policy:

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

This makes ensures that the http(s) request Apigee X sends to the backend has the same host header as the incoming one. It is important to use the variable request.header.host in the <Ref> tag and not in the <Value> tag, as Apigee does not resolve variables in the <Value> tag. If the <Ref> tag has content, the content of the <Value> tag is used in case the <Ref> tag content does not resolve.

It is also possible to set the host header statically; the "ref" tag remains empty then:

    <Name>target.header.host</Name>
    <Value>demo.example.org</Value>
    <Ref/>

Upvotes: 0

Mr.Turtle
Mr.Turtle

Reputation: 3090

This seems odd. Apigee should not do that by default. Are you sure that other flows are setup correctly? Anyhow.. you can try to create a AssignMessage policy that adds the correct host. Take a look at this: https://docs.apigee.com/api-platform/reference/policies/assign-message-policy#Samples

./policies/hostPolicy.xml:

<AssignMessage name="hostPolicy" continueOnError="false">
  <AssignTo createNew="false" type="request"></AssignTo>
  <Set>
    <Headers>
     <Header name="Host">{target.host}</Header>
    </Headers>
  </Set>
</AssignMessage>

./targets/xyzzy.xml

  <PreFlow name="PreFlow">
    <Request>
      <Step>
        <Name>hostPolicy</Name>
      </Step>
    </Request>
  </PreFlow>

NB: I haven't tested this. Read the vague apigee docs on policies

Upvotes: 1

Related Questions