kubernetes/ingress-nginx

HTTP header key formatted problem

66li opened this issue · 19 comments

66li commented

Expecting to convert all HTTP keys to lowercase, but in reality, this does not work,
Is there an existing solution for reference

This issue is currently awaiting triage.

If Ingress contributors determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

66li commented

i need help

  • Look at the template of a new bug report
  • Edit this issue description and answer those questions here in this issue description
  • Ensure to use markdown format
  • Give some examples
  • Search github for other users having attempted it
  • One good example is geop2 related configs injected into the nginx.conf for variables

This having data, context, links and elaborate descriptions can help draw attention as there are hardly any resources here.

You can even get more people to look at your messages on kubernetes.slack.com in the ingress-nginx-users channel (register at slack.k8s.io if needed)

/remove-kind bug
/kind support

66li commented

When I use curl https://example.com -H "CC: bb"
I hope that the request header received by the backend service after forwarding through nginx ingress is
cc: bb
In fact, nginx did not do any processing, and the HTTP header received by the backend service is CC: bb

66li commented

I want to format the key in nginx ingress
curl https://example.com -H "CC: bb"
Backend service received
cc: bb

66li commented

This background is my team's migration from Istio provided by cloud service providers to nginx
The default configuration of Istio is to lowercase all http header key

66li commented

It seems that,
I tried using Lua script but it didn't work

66li commented
 k get cm headers-lower -o yaml
apiVersion: v1
data:
  headers_lower.lua: |
    local headers = ngx.req.get_headers()
    local lower_headers = {}

    for key, value in pairs(headers) do
        local lower_key = string.lower(key)
        lower_headers[lower_key] = value
    end

    ngx.req.clear_header()
    for key, value in pairs(lower_headers) do
        ngx.req.set_header(key, value)
    end
    ngx.req.set_header("X-Custom-Header", "Hello, Lua Plugin!")
    ngx.req.set_header("AAX-Custom-Header", headers)
    ngx.log(ngx.DEBUG, headers)
kind: ConfigMap


k get cm nonprod-1-11-2-ingress-nginx-controller -o yaml
apiVersion: v1
data:
  allow-snippet-annotations: "true"
  server-snippet: |
    access_by_lua_file /etc/nginx/lua/headers_lower.lua;
kind: ConfigMap

66li commented
 k get deploy  nonprod-1-11-2-ingress-nginx-controller -o yaml
         volumeMounts:
        - mountPath: /etc/nginx/lua/headers_lower.lua
          name: lua-scripts
          readOnly: true
          subPath: headers_lower.lua
       volumes:
      - configMap:
          defaultMode: 420
          items:
          - key: headers_lower.lua
            path: headers_lower.lua
          name: headers-lower
        name: lua-scripts
66li commented

I think I'm not the only one who has encountered similar problems,
The default configuration of Istio is to format all HTTP header keys
I don't know if there is an elegant solution on nginx ingress

66li commented

ok
Can I use Lua script to implement this feature? I am a newcomer to Lua, so I need help

66li commented

ok
Is there any other way to achieve similar functionality,
Lowercase all the keys in the HTTP header

66li commented

ok
Thank you for your time

66li commented

It has been resolved. Below are the key code snippets

chart-version: ingress-nginx-4.11.2  

helm get values -nnginx-ingress example-nginx-ingress

...
USER-SUPPLIED VALUES:
commonLabels: {}
controller:
  config:
    server-snippet: |
      access_by_lua_file /etc/nginx/lua/headers_lower.lua;
  extraVolumeMounts:
  - mountPath: /etc/nginx/lua/headers_lower.lua
    name: lua-scripts
    readOnly: true
    subPath: headers_lower.lua
  extraVolumes:
  - configMap:
      defaultMode: 420
      items:
      - key: headers_lower.lua
        path: headers_lower.lua
      name: headers-lower
...



cat /opt/nginx-ingress/ingress-nginx/templates/lua-cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: headers-lower
  namespace: {{ include "ingress-nginx.namespace" . }}
data:
  headers_lower.lua: |
    local headers = ngx.req.get_headers()


    for key, value in pairs(headers) do
        local lower_key = string.lower(key)
        ngx.req.clear_header(key)       
        ngx.req.set_header(lower_key, value) 
    end