Wednesday, 11 March 2026

Adding SnippetsFIlter to HTTPRoute in Nginx Gateway to Implement Proxy Annotations of Retired Ingress Nginx

 We have discussed "Setup Nginx-Gateway Routes and Create Gateway Policies to Map with Ingress Nginx Annotations" in a previous post. However, we have not setup all that were previously set via ingress-nginx annotations, and only partial setup was made using ClientSettingsPolicy and UpstreamSettingsPolicy. Some proxy annotations that we used with ingress-nginx can only be setup by using a SnippetsFilter. 

Such SnippetsFilter is shown below for example.


The above is similar to setting up below annotations in ingress-nginx.

proxy-connect-timeout: 30 proxy-send-timeout: 30 proxy-read-timeout: 30 proxy-max-temp-file-size: 0

In addtional to above 4 settings we have set proxy_request_buffering to off as well.

Now let's check how we can get snippetsfilter setup properly for given routes. The first step is to enable snippets in nginx gateway fabric. This can be done via helm deployment by setting nginxGateway.snippets.enable=true.

Full helm setup below, and refer the blog here for more details.

helm upgrade ngf oci://ghcr.io/nginx/charts/nginx-gateway-fabric --install `
    --namespace nginx-gateway `
    --version 2.4.1 `
    -f $nginxGatewayFabricHelmValuesManifest `
    --set nginx.service.type="LoadBalancer" `
    --set nginx.service.loadBalancerIP=$nginxGatewayLoadBalancerIp `
    --set nginxGateway.replicas=3 `
    --set nginxGateway.snippets.enable=true;

When above helm deployed nginx gateway fabric should have the --snippets in controller.



Then we can setup snippetsfilter using below yaml. Note that here we keep proxy_request_buffering on; for elastic search and kibana, and have increased timeout for reads.

---
apiVersion: gateway.nginx.org/v1alpha1
kind: SnippetsFilter
metadata:
  name: selfhost-apps-gateway-snippets-filter
  namespace: elastic-stack
spec:
  snippets:
    - context: http.server.location
      value: |
        proxy_max_temp_file_size 0;
        proxy_request_buffering on;
        proxy_connect_timeout 5s;
        proxy_send_timeout 15s;
        proxy_read_timeout 120s;

Once created the snippetsfilter should have the status of accepted as shown below.



Then we can use the snippetsfilter in the routes as shown below. The change is shown in the  picture. Each route needs to be added with relevant snippetsfilter. The snippets filter should stay in same namespace as HTTPRoute.



---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: es-search-http-route
  namespace: elastic-stack
spec:
  parentRefs:
  - name: selfhost-apps-gateway
    namespace: nginx-gateway  
  hostnames:
  - "es-search${sys_sh_svc_deploy_dns_zone}$"
  rules:
  - filters:
    - type: ExtensionRef
      extensionRef:
        group: gateway.nginx.org
        kind: SnippetsFilter
        name: selfhost-apps-gateway-snippets-filter
    matches:
    - path:
        type: PathPrefix
        value: /
    backendRefs:
    - name: es-search-es-http
      port: 9200

Th applied route should have ResolvedRefs status as True, indication ExtensionRef of snippetsfilter is correctly resoved for the httproute. This will make the settings in snippetsfilter to get applied to the httproute.


Below is comparision of ingress nginx annotations and which way to set them up in nginx-gateway.



No comments:

Popular Posts