Overview: VirtualServer and VirtualServer Routing in nginx-ingress

Muralidharan Ganapathy
2 min readJun 21, 2024

--

VirtualServer is a type of native custom ingress resource, which can act along the ingress resource or act as a replacement for the ingress resource that does load balancing. It provides extended flexibility and customizability for adding header annotations for handling routing and can act as a proxy loadbalancer within the server itself.

VirtualServer Routing:

VirtualServer Route defines route for VirtualServers. A VirtualServer along with VirtualServer Route acts as an alternate for mergeable ingress.

Example for VirtualServer:

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
name: cafe
spec:
host: domain.example.com
listener:
http: http-8083
https: https-8443
tls:
secret: sslSecret
gunzip: on
upstreams:
- name: service1
service: service-file-name
port: 80
routes:
- path: /path1
action:
pass: service1

Adding below the table view for detailing each properties.

Example for VirtualServer Routing:

A VirtualServer Route can be considered an equivalent for a kubernetes ingress service. It has advantage of adding multiple subroutes for the main route from VirtualServer and can add additional spec and properties for each subroutes.

apiVersion: k8s.nginx.org/v1
kind: VirtualServerRoute
metadata:
name: example-virtual-route
namespace: namespace
spec:
host: domain.example.com
upstreams:
- name: upstream-service-name
service: service-name
port: port-number
subroutes:
- path: /route/subroute
action:
pass: subroute

Handling CORS issues using Virtual-Server:

Below are the annotations added to a kubernetes ingress resource for handling cors resource issue

nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
nginx.ingress.kubernetes.io/cors-allow-headers: "X-Forwarded-For"nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS"nginx.ingress.kubernetes.io/cors-allow-origin: "*"nginx.ingress.kubernetes.io/cors-max-age: "seconds"

The equivalent for these annotations in nginx VirtualServer is

responseHeaders:
add:
- name: Access-Control-Allow-Credentials
value: "true"
- name: Access-Control-Allow-Headers
value: "X-Forwarded-For"
- name: Access-Control-Allow-Methods
value: "PUT, GET, POST, OPTIONS"
- name: Access-Control-Allow-Origin
value: "*"
- name: Access-Control-Max-Age
value: "seconds"

The above properties will enable the cors actions to be allowed in the resources.

--

--