Following is my ngnix template which is working fine:
enterapiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
nginx.ingress.kubernetes.io/proxy-body-size: "15m"
nginx.ingress.kubernetes.io/configuration-snippet: |
set $a $http_x_correlation_id;
set $my_header $request_id;
proxy_set_header X_Correlation_Id $my_header;
rewrite ^/app-name(/|$)(.*) /$2 break;
name: rtfp-ingress-template
namespace: rtfp
spec:
ingressClassName: rtf-nginx
rules:
- host: ap.abc.com
http:
paths:
- path: /app-name
pathType: ImplementationSpecific
backend:
service:
name: service
port:
number: 80
But as soon as i introduce a if statement to optionally add header ie. if its not coming from client then header should be added, it fails with 404
nginx.ingress.kubernetes.io/configuration-snippet: |
set $a $http_x_correlation_id;
if ($a = ''){
set $my_header $request_id;
proxy_set_header X_Correlation_Id $my_header;
}
rewrite ^/app-name(/|$)(.*) /$2 break;
If this is not possible via configuration-snippet, please let me know what are the steps to achieve it via lua script and any good tutorial for lua script in an ngnix kubernetes environment.
you can achieve conditional behavior using the map module.
Here's an example of how you can conditionally set the X_Correlation_Id header:
Remember that using if statements can be problematic in Nginx, and it's generally better to use the map module or other configuration options whenever possible