How can i configure Environment based Hostname for my proxy.

Not applicable

Lets say we have Dev/Qa environment based API and both environment has separate host name for target end point and there are bunch of host names.

is there any way lets say if i deploy the proxy on 'test' environment it should take Dev hostname and when i deploy the proxy on 'prod' environment it should take Qa hostname?

Solved Solved
0 13 3,460
1 ACCEPTED SOLUTION

adas
Participant V

@Vinay Soni There are a few options here. The simplest would be to create targets servers with the same name in both test and prod, each referring to the corresponding target hostname that you want. You can refer to the apigee documentation for how to create and use target servers in your proxies: http://apigee.com/docs/management/apis/post/organi...

The other option is to do that in your proxies through either a javascript or conditional route rules because EDGE populates the environment.name flow variable for all runtime requests.

For the javascript option,

var env = context.getVariable("environment.name"))

would give you the environment that the proxy request hit and then based on that you can populate the corresponding target.url in the javascript.

For the conditional route rules, it would be something very similar:

<RouteRule name="test">
        <Condition>(environment.name = "test")</Condition>
        <HTTPTargetConnection>
            <URL>http://test.myapis.com</URL>
        </HTTPTargetConnection>
</RouteRule>
<RouteRule name="prod">
        <Condition>(environment.name = "prod")</Condition>
        <HTTPTargetConnection>
            <URL>http://prod.myapis.com</URL>
        </HTTPTargetConnection>
</RouteRule>
<RouteRule name="prod">
        <HTTPTargetConnection>
            <URL>http://default.myapis.com</URL>
        </HTTPTargetConnection>
</RouteRule>

Either of these approaches would work, but we recommend going by the target servers approach because then your proxy doesn't need all this logic or hardcoded URLs, and you can easily update the target server URLs using the management apis as and when you need.

View solution in original post

13 REPLIES 13

Dear @Vinay Soni

I think Target Servers should answer your queries. Did you get a chance to look at our documentation here & here ?

Thanks anil, i didn't look at it yet.. we try to look it now

Dear @Vinay Soni ,

Yes, Apigee Supports it out of the box using Target Servers concept. You can manage same using Edge UI Environment Configuration Link under APIs.

1177-screen-shot-2015-09-16-at-50134-pm.png

Find more about same here and here.

Cheers,

Anil Sagar

@Anil Sagar I am using javascript policy in my target endpoint there i am modifying target.url.

So how can i retrieve the hostname mentioned in my target server in javascript policy?

you don't need to overwrite target.url, Just need to use Targetservers in your TargetEndpoint configuration. Since TargetServer is created for a env, based on the env -- call will be routed to the appropriate Target,

@Mukundha Madhavan yes, but i have some requirement where i have to modify target url path, that's why i need javascript policy

Hi @Vinay Soni,

By now you may have figured this already out, but there's a way to leverage Mukunda's approach and still provide the path. This code should be self-explanatory:

    <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="TARGET_SERVER_PROD"/>
        </LoadBalancer>        
        <Path>{flow.customer.targetServerPath}</Path>
    </HTTPTargetConnection>

Please let me know if you have any questions.

Regards,

Diego

adas
Participant V

@Vinay Soni There are a few options here. The simplest would be to create targets servers with the same name in both test and prod, each referring to the corresponding target hostname that you want. You can refer to the apigee documentation for how to create and use target servers in your proxies: http://apigee.com/docs/management/apis/post/organi...

The other option is to do that in your proxies through either a javascript or conditional route rules because EDGE populates the environment.name flow variable for all runtime requests.

For the javascript option,

var env = context.getVariable("environment.name"))

would give you the environment that the proxy request hit and then based on that you can populate the corresponding target.url in the javascript.

For the conditional route rules, it would be something very similar:

<RouteRule name="test">
        <Condition>(environment.name = "test")</Condition>
        <HTTPTargetConnection>
            <URL>http://test.myapis.com</URL>
        </HTTPTargetConnection>
</RouteRule>
<RouteRule name="prod">
        <Condition>(environment.name = "prod")</Condition>
        <HTTPTargetConnection>
            <URL>http://prod.myapis.com</URL>
        </HTTPTargetConnection>
</RouteRule>
<RouteRule name="prod">
        <HTTPTargetConnection>
            <URL>http://default.myapis.com</URL>
        </HTTPTargetConnection>
</RouteRule>

Either of these approaches would work, but we recommend going by the target servers approach because then your proxy doesn't need all this logic or hardcoded URLs, and you can easily update the target server URLs using the management apis as and when you need.

thanks @arghya das..

i have some requirement where i need host name of my target server in java script.

Can you help me out ?

@Vinay Soni just to maintain the community standards and make content easily searchable, can you post this requirement as a new thread instead of combining with the current post. I will answer that once you have made a new post.

Any help ???

is there a thread on the community with your follow up question?

Hi I've tried to use the targetserver solution but i couldn't make it work. I keep receiving a 502 reply from the proxy