Score:0

Change the host header based on cookie

cn flag

I want to replace www with beta in the $http_host if a cookie SHOW_BETA is set to true in the request.

I've put this in my host block but not sure how to do the actual replacement.

map $http_host $cookie_SHOW_BETA {
    // replace www with beta here if true.   <---- Question: what to put in here
    default $http_host;
}

also if I want to use if where I can easily split $http_host I cannot use and to check the cookie's value.

Score:0
us flag

Your usage of map has issues:

  • The "from" value for the map should be first, and the variable to set should be second
  • Using $http_host as a variable to set might not work

I don't think replace is even possible in map.

The best you could try is to add a prefix:

map $cookie_show_beta $http_host {
    true beta.$http_host;
    default $http_host;
}
mohsen kamrani avatar
cn flag
Thanks for your help especially the map part, but the part beta.$http_host wouldn't give me what I want. I'm starting to think perhaps map is not what I'm looking for, I just thought map is the way to do it that's why I used it.
us flag
I think nginx does not have a feature that would do what you want.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.