“SSC API key is required” on Sitecore K8s

When trying to connect to Sitecore (hosted in AKS) from my Next.JS head (hosted externally), I got following error:

SSC API key is required. Pass with 'sc_apikey' query string or HTTP header.

The code that caused the issue was a textbook GraphQL request:

const graphQLClient = new GraphQLRequestClient(config.graphQLEndpoint, {
    apiKey: config.sitecoreApiKey,
});

Turns out nginx (that connects incoming AKS traffic to the actual Sitecore containers) does not allow underscores in header keys by default:

http://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers

This case (different use case though) is documented in SitecoreXP1020ProductionDeploymentWithKubernetes-en.pdf, see

4.6.3. When I request SSC, problems occur if there are underscores in header names

but since that document is not indexed by Google, finding it was not that easy.

Luckily somebody had a similar issue when running Sitecore on Amazon: https://github.com/Sitecore/jss/issues/1060.

The solution for k8s is straightforward. Simply add

enable-underscores-in-headers: "true"

to ingress-nginx\configuration.yaml (the configmap used by nginx).

Note that if you install this with kubectl -k ingress-nginx this requires nginx and Sitecore to run in the same namespace. If not, the config map is created in the Sitecore namespace, but won’t be picked up by nginx.