I am trying to make a system where we route to different backend based on claims in jwt of incoming request.
I have working secondary server that spits out the backend given the request url and jwt in the request but nginx is ignoring my backend value that is set from lua block.
This is the yaml of ingress that I have:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: docs-service
annotations:
nginx.ingress.kubernetes.io/configuration-snippet: |
access_by_lua_block {
local cjson = require("cjson")
local http = require "resty.http"
local default_backend="docs1"
local httpc = http.new()
local res, err = httpc:request_uri("http://authserver.ingress-nginx-helpers.svc.cluster.local:5000/find-backend", {
method = "POST",
body = cjson.encode({ real_url = ngx.var.request_uri }),
headers = {
["Content-Type"] = "application/json",
["Token"] = ngx.req.get_headers()['token'],
["cookie"] = ngx.req.get_headers()['cookie']
}
})
local response, decode_err = cjson.decode(res.body)
if not response then
ngx.log(ngx.ERR, "Failed to decode response: ", decode_err)
-- ngx.var.proxy_upstream_name = ngx.var.namespace .. "-" .. default_backend .. "-5000"
ngx.var.service_name = default_backend
else
-- ngx.var.proxy_upstream_name = ngx.var.namespace .. "-" .. response.backend .. "-5000"
ngx.var.service_name = response.backend
end
}
spec:
ingressClassName: nginx
rules:
- host: hello.ankit.com
http:
paths:
- backend:
service:
name: docs1
port:
number: 5000
path: /api/v1/docs1/docs/custom/
pathType: Exact
The request is reaching “authserver” with it returning right value but the value defined in .spec.rules.[].http.paths.[].backend.service is used.
I looked a bit on openresty’s docs and found out rewrite_by_lua block runs first and thus tried to make it that but it errored out while applying the manifest. I looked at the nginx.conf generated and saw generated rewrite_by_lua_block which called balancer.rewrite()
don’t know much what “balancer” here means though.