github.com/nginxinc/kubernetes-ingress@v1.12.5/docs-web/configuration/virtualserver-and-virtualserverroute-resources.md (about) 1 # VirtualServer and VirtualServerRoute Resources 2 3 The VirtualServer and VirtualServerRoute resources are new load balancing configuration, introduced in release 1.5 as an alternative to the Ingress resource. The resources enable use cases not supported with the Ingress resource, such as traffic splitting and advanced content-based routing. The resources are implemented as [Custom Resources](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/). 4 5 This document is the reference documentation for the resources. To see additional examples of using the resources for specific use cases, go to the [examples-of-custom-resources](https://github.com/nginxinc/kubernetes-ingress/blob/v1.12.5/examples-of-custom-resources) folder in our GitHub repo. 6 7 ## Contents 8 9 - [VirtualServer and VirtualServerRoute Resources](#virtualserver-and-virtualserverroute-resources) 10 - [Contents](#contents) 11 - [VirtualServer Specification](#virtualserver-specification) 12 - [VirtualServer.TLS](#virtualserver-tls) 13 - [VirtualServer.TLS.Redirect](#virtualserver-tls-redirect) 14 - [VirtualServer.Route](#virtualserver-route) 15 - [VirtualServerRoute Specification](#virtualserverroute-specification) 16 - [VirtualServerRoute.Subroute](#virtualserverroute-subroute) 17 - [Common Parts of the VirtualServer and VirtualServerRoute](#common-parts-of-the-virtualserver-and-virtualserverroute) 18 - [Upstream](#upstream) 19 - [Upstream.Buffers](#upstream-buffers) 20 - [Upstream.TLS](#upstream-tls) 21 - [Upstream.Queue](#upstream-queue) 22 - [Upstream.Healthcheck](#upstream-healthcheck) 23 - [Upstream.SessionCookie](#upstream-sessioncookie) 24 - [Header](#header) 25 - [Action](#action) 26 - [Action.Redirect](#action-redirect) 27 - [Action.Return](#action-return) 28 - [Action.Proxy](#action-proxy) 29 - [Split](#split) 30 - [Match](#match) 31 - [Condition](#condition) 32 - [ErrorPage](#errorpage) 33 - [ErrorPage.Redirect](#errorpage-redirect) 34 - [ErrorPage.Return](#errorpage-return) 35 - [Using VirtualServer and VirtualServerRoute](#using-virtualserver-and-virtualserverroute) 36 - [Using Snippets](#using-snippets) 37 - [Validation](#validation) 38 - [Structural Validation](#structural-validation) 39 - [Comprehensive Validation](#comprehensive-validation) 40 - [Customization via ConfigMap](#customization-via-configmap) 41 42 ## VirtualServer Specification 43 44 The VirtualServer resource defines load balancing configuration for a domain name, such as `example.com`. Below is an example of such configuration: 45 ```yaml 46 apiVersion: k8s.nginx.org/v1 47 kind: VirtualServer 48 metadata: 49 name: cafe 50 spec: 51 host: cafe.example.com 52 tls: 53 secret: cafe-secret 54 upstreams: 55 - name: tea 56 service: tea-svc 57 port: 80 58 - name: coffee 59 service: coffee-svc 60 port: 80 61 routes: 62 - path: /tea 63 action: 64 pass: tea 65 - path: /coffee 66 action: 67 pass: coffee 68 - path: ~ ^/decaf/.*\\.jpg$ 69 action: 70 pass: coffee 71 - path: = /green/tea 72 action: 73 pass: tea 74 ``` 75 76 ```eval_rst 77 .. list-table:: 78 :header-rows: 1 79 80 * - Field 81 - Description 82 - Type 83 - Required 84 * - ``host`` 85 - The host (domain name) of the server. Must be a valid subdomain as defined in RFC 1123, such as ``my-app`` or ``hello.example.com``. Wildcard domains like ``*.example.com`` are not allowed. The ``host`` value needs to be unique among all Ingress and VirtualServer resources. See also `Handling Host Collisions </nginx-ingress-controller/configuration/handling-host-collisions>`_. 86 - ``string`` 87 - Yes 88 * - ``tls`` 89 - The TLS termination configuration. 90 - `tls <#virtualserver-tls>`_ 91 - No 92 * - ``policies`` 93 - A list of policies. 94 - `[]policy <#virtualserver-policy>`_ 95 - No 96 * - ``upstreams`` 97 - A list of upstreams. 98 - `[]upstream <#upstream>`_ 99 - No 100 * - ``routes`` 101 - A list of routes. 102 - `[]route <#virtualserver-route>`_ 103 - No 104 * - ``ingressClassName`` 105 - Specifies which Ingress controller must handle the VirtualServer resource. 106 - ``string`` 107 - No 108 * - ``http-snippets`` 109 - Sets a custom snippet in the http context. 110 - ``string`` 111 - No 112 * - ``server-snippets`` 113 - Sets a custom snippet in server context. Overrides the ``server-snippets`` ConfigMap key. 114 - ``string`` 115 - No 116 ``` 117 118 ### VirtualServer.TLS 119 120 The tls field defines TLS configuration for a VirtualServer. For example: 121 ```yaml 122 secret: cafe-secret 123 redirect: 124 enable: true 125 ``` 126 127 ```eval_rst 128 .. list-table:: 129 :header-rows: 1 130 131 * - Field 132 - Description 133 - Type 134 - Required 135 * - ``secret`` 136 - The name of a secret with a TLS certificate and key. The secret must belong to the same namespace as the VirtualServer. The secret must be of the type ``kubernetes.io/tls`` and contain keys named ``tls.crt`` and ``tls.key`` that contain the certificate and private key as described `here <https://kubernetes.io/docs/concepts/services-networking/ingress/#tls>`_. If the secret doesn't exist or is invalid, NGINX will break any attempt to establish a TLS connection to the host of the VirtualServer. 137 - ``string`` 138 - No 139 * - ``redirect`` 140 - The redirect configuration of the TLS for a VirtualServer. 141 - `tls.redirect <#virtualserver-tls-redirect>`_ 142 - No 143 ``` 144 ### VirtualServer.TLS.Redirect 145 146 The redirect field configures a TLS redirect for a VirtualServer: 147 ```yaml 148 enable: true 149 code: 301 150 basedOn: scheme 151 ``` 152 153 ```eval_rst 154 .. list-table:: 155 :header-rows: 1 156 157 * - Field 158 - Description 159 - Type 160 - Required 161 * - ``enable`` 162 - Enables a TLS redirect for a VirtualServer. The default is ``False``. 163 - ``boolean`` 164 - No 165 * - ``code`` 166 - The status code of a redirect. The allowed values are: ``301``\ , ``302``\ , ``307``\ , ``308``. The default is ``301``. 167 - ``int`` 168 - No 169 * - ``basedOn`` 170 - The attribute of a request that NGINX will evaluate to send a redirect. The allowed values are ``scheme`` (the scheme of the request) or ``x-forwarded-proto`` (the ``X-Forwarded-Proto`` header of the request). The default is ``scheme``. 171 - ``string`` 172 - No 173 ``` 174 ### VirtualServer.Policy 175 176 The policy field references a [Policy resource](/nginx-ingress-controller/configuration/policy-resource/) by its name and optional namespace. For example: 177 ```yaml 178 name: access-control 179 ``` 180 181 ```eval_rst 182 .. list-table:: 183 :header-rows: 1 184 185 * - Field 186 - Description 187 - Type 188 - Required 189 * - ``name`` 190 - The name of a policy. If the policy doesn't exist or invalid, NGINX will respond with an error response with the `500` status code. 191 - ``string`` 192 - Yes 193 * - ``namespace`` 194 - The namespace of a policy. If not specified, the namespace of the VirtualServer resource is used. 195 - ``string`` 196 - No 197 ``` 198 199 ### VirtualServer.Route 200 201 The route defines rules for matching client requests to actions like passing a request to an upstream. For example: 202 ```yaml 203 path: /tea 204 action: 205 pass: tea 206 ``` 207 208 ```eval_rst 209 .. list-table:: 210 :header-rows: 1 211 212 * - Field 213 - Description 214 - Type 215 - Required 216 * - ``path`` 217 - The path of the route. NGINX will match it against the URI of a request. Possible values are: a prefix (\ ``/``\ , ``/path``\ ), an exact match (\ ``=/exact/match``\ ), a case insensitive regular expression (\ ``~*^/Bar.*\\.jpg``\ ) or a case sensitive regular expression (\ ``~^/foo.*\\.jpg``\ ). In the case of a prefix (must start with ``/``\ ) or an exact match (must start with ``=``\ ), the path must not include any whitespace characters, ``{``\ , ``}`` or ``;``. In the case of the regex matches, all double quotes ``"`` must be escaped and the match can't end in an unescaped backslash ``\``. The path must be unique among the paths of all routes of the VirtualServer. Check the `location <https://nginx.org/en/docs/http/ngx_http_core_module.html#location>`_ directive for more information. 218 - ``string`` 219 - Yes 220 * - ``policies`` 221 - A list of policies. The policies override the policies of the same type defined in the ``spec`` of the VirtualServer. See `Applying Policies </nginx-ingress-controller/configuration/policy-resource/#applying-policies>`_ for more details. 222 - `[]policy <#virtualserver-policy>`_ 223 - No 224 * - ``action`` 225 - The default action to perform for a request. 226 - `action <#action>`_ 227 - No* 228 * - ``splits`` 229 - The default splits configuration for traffic splitting. Must include at least 2 splits. 230 - `[]split <#split>`_ 231 - No* 232 * - ``matches`` 233 - The matching rules for advanced content-based routing. Requires the default ``action`` or ``splits``. Unmatched requests will be handled by the default ``action`` or ``splits``. 234 - `matches <#match>`_ 235 - No 236 * - ``route`` 237 - The name of a VirtualServerRoute resource that defines this route. If the VirtualServerRoute belongs to a different namespace than the VirtualServer, you need to include the namespace. For example, ``tea-namespace/tea``. 238 - ``string`` 239 - No* 240 * - ``errorPages`` 241 - The custom responses for error codes. NGINX will use those responses instead of returning the error responses from the upstream servers or the default responses generated by NGINX. A custom response can be a redirect or a canned response. For example, a redirect to another URL if an upstream server responded with a 404 status code. 242 - `[]errorPage <#errorpage>`_ 243 - No 244 * - ``location-snippets`` 245 - Sets a custom snippet in the location context. Overrides the ``location-snippets`` ConfigMap key. 246 - ``string`` 247 - No 248 ``` 249 250 \* -- a route must include exactly one of the following: `action`, `splits`, or `route`. 251 252 ## VirtualServerRoute Specification 253 254 The VirtualServerRoute resource defines a route for a VirtualServer. It can consist of one or multiple subroutes. The VirtualServerRoute is an alternative to [Mergeable Ingress types](/nginx-ingress-controller/configuration/ingress-resources/cross-namespace-configuration). 255 256 In the example below, the VirtualServer `cafe` from the namespace `cafe-ns` defines a route with the path `/coffee`, which is further defined in the VirtualServerRoute `coffee` from the namespace `coffee-ns`. 257 258 VirtualServer: 259 ```yaml 260 apiVersion: k8s.nginx.org/v1 261 kind: VirtualServer 262 metadata: 263 name: cafe 264 namespace: cafe-ns 265 spec: 266 host: cafe.example.com 267 upstreams: 268 - name: tea 269 service: tea-svc 270 port: 80 271 routes: 272 - path: /tea 273 action: 274 pass: tea 275 - path: /coffee 276 route: coffee-ns/coffee 277 ``` 278 279 VirtualServerRoute: 280 ```yaml 281 apiVersion: k8s.nginx.org/v1 282 kind: VirtualServerRoute 283 metadata: 284 name: coffee 285 namespace: coffee-ns 286 spec: 287 host: cafe.example.com 288 upstreams: 289 - name: latte 290 service: latte-svc 291 port: 80 292 - name: espresso 293 service: espresso-svc 294 port: 80 295 subroutes: 296 - path: /coffee/latte 297 action: 298 pass: latte 299 - path: /coffee/espresso 300 action: 301 pass: espresso 302 ``` 303 304 Note that each subroute must have a `path` that starts with the same prefix (here `/coffee`), which is defined in the route of the VirtualServer. Additionally, the `host` in the VirtualServerRoute must be the same as the `host` of the VirtualServer. 305 306 ```eval_rst 307 .. list-table:: 308 :header-rows: 1 309 310 * - Field 311 - Description 312 - Type 313 - Required 314 * - ``host`` 315 - The host (domain name) of the server. Must be a valid subdomain as defined in RFC 1123, such as ``my-app`` or ``hello.example.com``. Wildcard domains like ``*.example.com`` are not allowed. Must be the same as the ``host`` of the VirtualServer that references this resource. 316 - ``string`` 317 - Yes 318 * - ``upstreams`` 319 - A list of upstreams. 320 - `[]upstream <#upstream>`_ 321 - No 322 * - ``subroutes`` 323 - A list of subroutes. 324 - `[]subroute <#virtualserverroute-subroute>`_ 325 - No 326 * - ``ingressClassName`` 327 - Specifies which Ingress controller must handle the VirtualServerRoute resource. Must be the same as the ``ingressClassName`` of the VirtualServer that references this resource. 328 - ``string``_ 329 - No 330 ``` 331 332 ### VirtualServerRoute.Subroute 333 334 The subroute defines rules for matching client requests to actions like passing a request to an upstream. For example: 335 ```yaml 336 path: /coffee 337 action: 338 pass: coffee 339 ``` 340 341 ```eval_rst 342 .. list-table:: 343 :header-rows: 1 344 345 * - Field 346 - Description 347 - Type 348 - Required 349 * - ``path`` 350 - The path of the subroute. NGINX will match it against the URI of a request. Possible values are: a prefix (\ ``/``\ , ``/path``\ ), an exact match (\ ``=/exact/match``\ ), a case insensitive regular expression (\ ``~*^/Bar.*\\.jpg``\ ) or a case sensitive regular expression (\ ``~^/foo.*\\.jpg``\ ). In the case of a prefix, the path must start with the same path as the path of the route of the VirtualServer that references this resource. In the case of an exact or regex match, the path must be the same as the path of the route of the VirtualServer that references this resource. In the case of a prefix or an exact match, the path must not include any whitespace characters, ``{``\ , ``}`` or ``;``. In the case of the regex matches, all double quotes ``"`` must be escaped and the match can't end in an unescaped backslash ``\``. The path must be unique among the paths of all subroutes of the VirtualServerRoute. 351 - ``string`` 352 - Yes 353 * - ``policies`` 354 - A list of policies. The policies override *all* policies defined in the route of the VirtualServer that references this resource. The policies also override the policies of the same type defined in the ``spec`` of the VirtualServer. See `Applying Policies </nginx-ingress-controller/configuration/policy-resource/#applying-policies>`_ for more details. 355 - `[]policy <#virtualserver-policy>`_ 356 - No 357 * - ``action`` 358 - The default action to perform for a request. 359 - `action <#action>`_ 360 - No* 361 * - ``splits`` 362 - The default splits configuration for traffic splitting. Must include at least 2 splits. 363 - `[]split <#split>`_ 364 - No* 365 * - ``matches`` 366 - The matching rules for advanced content-based routing. Requires the default ``action`` or ``splits``. Unmatched requests will be handled by the default ``action`` or ``splits``. 367 - `matches <#match>`_ 368 - No 369 * - ``errorPages`` 370 - The custom responses for error codes. NGINX will use those responses instead of returning the error responses from the upstream servers or the default responses generated by NGINX. A custom response can be a redirect or a canned response. For example, a redirect to another URL if an upstream server responded with a 404 status code. 371 - `[]errorPage <#errorpage>`_ 372 - No 373 * - ``location-snippets`` 374 - Sets a custom snippet in the location context. Overrides the ``location-snippets`` of the VirtualServer (if set) or the ``location-snippets`` ConfigMap key. 375 - ``string`` 376 - No 377 ``` 378 379 \* -- a subroute must include exactly one of the following: `action` or `splits`. 380 381 ## Common Parts of the VirtualServer and VirtualServerRoute 382 383 ### Upstream 384 385 The upstream defines a destination for the routing configuration. For example: 386 ```yaml 387 name: tea 388 service: tea-svc 389 subselector: 390 version: canary 391 port: 80 392 lb-method: round_robin 393 fail-timeout: 10s 394 max-fails: 1 395 max-conns: 32 396 keepalive: 32 397 connect-timeout: 30s 398 read-timeout: 30s 399 send-timeout: 30s 400 next-upstream: "error timeout non_idempotent" 401 next-upstream-timeout: 5s 402 next-upstream-tries: 10 403 client-max-body-size: 2m 404 tls: 405 enable: true 406 ``` 407 408 **Note**: The WebSocket protocol is supported without any additional configuration. 409 410 ```eval_rst 411 .. list-table:: 412 :header-rows: 1 413 414 * - Field 415 - Description 416 - Type 417 - Required 418 * - ``name`` 419 - The name of the upstream. Must be a valid DNS label as defined in RFC 1035. For example, ``hello`` and ``upstream-123`` are valid. The name must be unique among all upstreams of the resource. 420 - ``string`` 421 - Yes 422 * - ``service`` 423 - The name of a `service <https://kubernetes.io/docs/concepts/services-networking/service/>`_. The service must belong to the same namespace as the resource. If the service doesn't exist, NGINX will assume the service has zero endpoints and return a ``502`` response for requests for this upstream. For NGINX Plus only, services of type `ExternalName <https://kubernetes.io/docs/concepts/services-networking/service/#externalname>`_ are also supported (check the `prerequisites <https://github.com/nginxinc/kubernetes-ingress/tree/v1.12.5/examples/externalname-services#prerequisites>`_\ ). 424 - ``string`` 425 - Yes 426 * - ``subselector`` 427 - Selects the pods within the service using label keys and values. By default, all pods of the service are selected. Note: the specified labels are expected to be present in the pods when they are created. If the pod labels are updated, the Ingress Controller will not see that change until the number of the pods is changed. 428 - ``map[string]string`` 429 - No 430 * - ``use-cluster-ip`` 431 - Enables using the Cluster IP and port of the service instead of the default behavior of using the IP and port of the pods. When this field is enabled, the fields that configure NGINX behavior related to multiple upstream servers (like ``lb-method`` and ``next-upstream``) will have no effect, as the Ingress Controller will configure NGINX with only one upstream server that will match the service Cluster IP. 432 - ``boolean`` 433 - No 434 * - ``port`` 435 - The port of the service. If the service doesn't define that port, NGINX will assume the service has zero endpoints and return a ``502`` response for requests for this upstream. The port must fall into the range ``1..65535``. 436 - ``uint16`` 437 - Yes 438 * - ``lb-method`` 439 - The load `balancing method <https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#choosing-a-load-balancing-method>`_. To use the round-robin method, specify ``round_robin``. The default is specified in the ``lb-method`` ConfigMap key. 440 - ``string`` 441 - No 442 * - ``fail-timeout`` 443 - The time during which the specified number of unsuccessful attempts to communicate with an upstream server should happen to consider the server unavailable. See the `fail_timeout <https://nginx.org/en/docs/http/ngx_http_upstream_module.html#fail_timeout>`_ parameter of the server directive. The default is set in the ``fail-timeout`` ConfigMap key. 444 - ``string`` 445 - No 446 * - ``max-fails`` 447 - The number of unsuccessful attempts to communicate with an upstream server that should happen in the duration set by the ``fail-timeout`` to consider the server unavailable. See the `max_fails <https://nginx.org/en/docs/http/ngx_http_upstream_module.html#max_fails>`_ parameter of the server directive. The default is set in the ``max-fails`` ConfigMap key. 448 - ``int`` 449 - No 450 * - ``max-conns`` 451 - The maximum number of simultaneous active connections to an upstream server. See the `max_conns <https://nginx.org/en/docs/http/ngx_http_upstream_module.html#max_conns>`_ parameter of the server directive. By default there is no limit. Note: if keepalive connections are enabled, the total number of active and idle keepalive connections to an upstream server may exceed the ``max_conns`` value. 452 - ``int`` 453 - No 454 * - ``keepalive`` 455 - Configures the cache for connections to upstream servers. The value ``0`` disables the cache. See the `keepalive <https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive>`_ directive. The default is set in the ``keepalive`` ConfigMap key. 456 - ``int`` 457 - No 458 * - ``connect-timeout`` 459 - The timeout for establishing a connection with an upstream server. See the `proxy_connect_timeout <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout>`_ directive. The default is specified in the ``proxy-connect-timeout`` ConfigMap key. 460 - ``string`` 461 - No 462 * - ``read-timeout`` 463 - The timeout for reading a response from an upstream server. See the `proxy_read_timeout <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout>`_ directive. The default is specified in the ``proxy-read-timeout`` ConfigMap key. 464 - ``string`` 465 - No 466 * - ``send-timeout`` 467 - The timeout for transmitting a request to an upstream server. See the `proxy_send_timeout <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_send_timeout>`_ directive. The default is specified in the ``proxy-send-timeout`` ConfigMap key. 468 - ``string`` 469 - No 470 * - ``next-upstream`` 471 - Specifies in which cases a request should be passed to the next upstream server. See the `proxy_next_upstream <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream>`_ directive. The default is ``error timeout``. 472 - ``string`` 473 - No 474 * - ``next-upstream-timeout`` 475 - The time during which a request can be passed to the next upstream server. See the `proxy_next_upstream_timeout <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream_timeout>`_ directive. The ``0`` value turns off the time limit. The default is ``0``. 476 - ``string`` 477 - No 478 * - ``next-upstream-tries`` 479 - The number of possible tries for passing a request to the next upstream server. See the `proxy_next_upstream_tries <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream_tries>`_ directive. The ``0`` value turns off this limit. The default is ``0``. 480 - ``int`` 481 - No 482 * - ``client-max-body-size`` 483 - Sets the maximum allowed size of the client request body. See the `client_max_body_size <https://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size>`_ directive. The default is set in the ``client-max-body-size`` ConfigMap key. 484 - ``string`` 485 - No 486 * - ``tls`` 487 - The TLS configuration for the Upstream. 488 - `tls <#upstream-tls>`_ 489 - No 490 * - ``healthCheck`` 491 - The health check configuration for the Upstream. See the `health_check <https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#health_check>`_ directive. Note: this feature is supported only in NGINX Plus. 492 - `healthcheck <#upstream-healthcheck>`_ 493 - No 494 * - ``slow-start`` 495 - The slow start allows an upstream server to gradually recover its weight from 0 to its nominal value after it has been recovered or became available or when the server becomes available after a period of time it was considered unavailable. By default, the slow start is disabled. See the `slow_start <https://nginx.org/en/docs/http/ngx_http_upstream_module.html#slow_start>`_ parameter of the server directive. Note: The parameter cannot be used along with the ``random``\ , ``hash`` or ``ip_hash`` load balancing methods and will be ignored. 496 - ``string`` 497 - No 498 * - ``queue`` 499 - Configures a queue for an upstream. A client request will be placed into the queue if an upstream server cannot be selected immediately while processing the request. By default, no queue is configured. Note: this feature is supported only in NGINX Plus. 500 - `queue <#upstream-queue>`_ 501 - No 502 * - ``buffering`` 503 - Enables buffering of responses from the upstream server. See the `proxy_buffering <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering>`_ directive. The default is set in the ``proxy-buffering`` ConfigMap key. 504 - ``boolean`` 505 - No 506 * - ``buffers`` 507 - Configures the buffers used for reading a response from the upstream server for a single connection. 508 - `buffers <#upstream-buffers>`_ 509 - No 510 * - ``buffer-size`` 511 - Sets the size of the buffer used for reading the first part of a response received from the upstream server. See the `proxy_buffer_size <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffer_size>`_ directive. The default is set in the ``proxy-buffer-size`` ConfigMap key. 512 - ``string`` 513 - No 514 ``` 515 516 ### Upstream.Buffers 517 The buffers field configures the buffers used for reading a response from the upstream server for a single connection: 518 519 ```yaml 520 number: 4 521 size: 8K 522 ``` 523 See the [proxy_buffers](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffers) directive for additional information. 524 525 ```eval_rst 526 .. list-table:: 527 :header-rows: 1 528 529 * - Field 530 - Description 531 - Type 532 - Required 533 * - ``number`` 534 - Configures the number of buffers. The default is set in the ``proxy-buffers`` ConfigMap key. 535 - ``int`` 536 - Yes 537 * - ``size`` 538 - Configures the size of a buffer. The default is set in the ``proxy-buffers`` ConfigMap key. 539 - ``string`` 540 - Yes 541 ``` 542 543 ### Upstream.TLS 544 545 ```eval_rst 546 .. list-table:: 547 :header-rows: 1 548 549 * - Field 550 - Description 551 - Type 552 - Required 553 * - ``enable`` 554 - Enables HTTPS for requests to upstream servers. The default is ``False``\ , meaning that HTTP will be used. 555 - ``boolean`` 556 - No 557 ``` 558 559 ### Upstream.Queue 560 561 The queue field configures a queue. A client request will be placed into the queue if an upstream server cannot be selected immediately while processing the request: 562 563 ```yaml 564 size: 10 565 timeout: 60s 566 ``` 567 568 See [`queue`](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#queue) directive for additional information. 569 570 Note: This feature is supported only in NGINX Plus. 571 572 ```eval_rst 573 .. list-table:: 574 :header-rows: 1 575 576 * - Field 577 - Description 578 - Type 579 - Required 580 * - ``size`` 581 - The size of the queue. 582 - ``int`` 583 - Yes 584 * - ``timeout`` 585 - The timeout of the queue. A request cannot be queued for a period longer than the timeout. The default is ``60s``. 586 - ``string`` 587 - No 588 ``` 589 590 ### Upstream.Healthcheck 591 592 The Healthcheck defines an [active health check](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-health-check/). In the example below we enable a health check for an upstream and configure all the available parameters: 593 594 ```yaml 595 name: tea 596 service: tea-svc 597 port: 80 598 healthCheck: 599 enable: true 600 path: /healthz 601 interval: 20s 602 jitter: 3s 603 fails: 5 604 passes: 5 605 port: 8080 606 tls: 607 enable: true 608 connect-timeout: 10s 609 read-timeout: 10s 610 send-timeout: 10s 611 headers: 612 - name: Host 613 value: my.service 614 statusMatch: "! 500" 615 ``` 616 617 Note: This feature is supported only in NGINX Plus. 618 619 ```eval_rst 620 .. list-table:: 621 :header-rows: 1 622 623 * - Field 624 - Description 625 - Type 626 - Required 627 * - ``enable`` 628 - Enables a health check for an upstream server. The default is ``false``. 629 - ``boolean`` 630 - No 631 * - ``path`` 632 - The path used for health check requests. The default is ``/``. 633 - ``string`` 634 - No 635 * - ``interval`` 636 - The interval between two consecutive health checks. The default is ``5s``. 637 - ``string`` 638 - No 639 * - ``jitter`` 640 - The time within which each health check will be randomly delayed. By default, there is no delay. 641 - ``string`` 642 - No 643 * - ``fails`` 644 - The number of consecutive failed health checks of a particular upstream server after which this server will be considered unhealthy. The default is ``1``. 645 - ``integer`` 646 - No 647 * - ``passes`` 648 - The number of consecutive passed health checks of a particular upstream server after which the server will be considered healthy. The default is ``1``. 649 - ``integer`` 650 - No 651 * - ``port`` 652 - The port used for health check requests. By default, the port of the upstream is used. Note: in contrast with the port of the upstream, this port is not a service port, but a port of a pod. 653 - ``integer`` 654 - No 655 * - ``tls`` 656 - The TLS configuration used for health check requests. By default, the ``tls`` field of the upstream is used. 657 - `upstream.tls <#upstream-tls>`_ 658 - No 659 * - ``connect-timeout`` 660 - The timeout for establishing a connection with an upstream server. By default, the ``connect-timeout`` of the upstream is used. 661 - ``string`` 662 - No 663 * - ``read-timeout`` 664 - The timeout for reading a response from an upstream server. By default, the ``read-timeout`` of the upstream is used. 665 - ``string`` 666 - No 667 * - ``send-timeout`` 668 - The timeout for transmitting a request to an upstream server. By default, the ``send-timeout`` of the upstream is used. 669 - ``string`` 670 - No 671 * - ``headers`` 672 - The request headers used for health check requests. NGINX Plus always sets the ``Host``\ , ``User-Agent`` and ``Connection`` headers for health check requests. 673 - `[]header <#header>`_ 674 - No 675 * - ``statusMatch`` 676 - The expected response status codes of a health check. By default, the response should have status code 2xx or 3xx. Examples: ``"200"``\ , ``"! 500"``\ , ``"301-303 307"``. See the documentation of the `match <https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html?#match>`_ directive. 677 - ``string`` 678 - No 679 ``` 680 681 ### Upstream.SessionCookie 682 683 The SessionCookie field configures session persistence which allows requests from the same client to be passed to the same upstream server. The information about the designated upstream server is passed in a session cookie generated by NGINX Plus. 684 685 In the example below, we configure session persistence with a session cookie for an upstream and configure all the available parameters: 686 687 ```yaml 688 name: tea 689 service: tea-svc 690 port: 80 691 sessionCookie: 692 enable: true 693 name: srv_id 694 path: / 695 expires: 1h 696 domain: .example.com 697 httpOnly: false 698 secure: true 699 ``` 700 See the [`sticky`](https://nginx.org/en/docs/http/ngx_http_upstream_module.html?#sticky) directive for additional information. The session cookie corresponds to the `sticky cookie` method. 701 702 Note: This feature is supported only in NGINX Plus. 703 704 ```eval_rst 705 .. list-table:: 706 :header-rows: 1 707 708 * - Field 709 - Description 710 - Type 711 - Required 712 * - ``enable`` 713 - Enables session persistence with a session cookie for an upstream server. The default is ``false``. 714 - ``boolean`` 715 - No 716 * - ``name`` 717 - The name of the cookie. 718 - ``string`` 719 - Yes 720 * - ``path`` 721 - The path for which the cookie is set. 722 - ``string`` 723 - No 724 * - ``expires`` 725 - The time for which a browser should keep the cookie. Can be set to the special value ``max``\ , which will cause the cookie to expire on ``31 Dec 2037 23:55:55 GMT``. 726 - ``string`` 727 - No 728 * - ``domain`` 729 - The domain for which the cookie is set. 730 - ``string`` 731 - No 732 * - ``httpOnly`` 733 - Adds the ``HttpOnly`` attribute to the cookie. 734 - ``boolean`` 735 - No 736 * - ``secure`` 737 - Adds the ``Secure`` attribute to the cookie. 738 - ``boolean`` 739 - No 740 ``` 741 742 ### Header 743 744 The header defines an HTTP Header: 745 ```yaml 746 name: Host 747 value: example.com 748 ``` 749 750 ```eval_rst 751 .. list-table:: 752 :header-rows: 1 753 754 * - Field 755 - Description 756 - Type 757 - Required 758 * - ``name`` 759 - The name of the header. 760 - ``string`` 761 - Yes 762 * - ``value`` 763 - The value of the header. 764 - ``string`` 765 - No 766 ``` 767 768 ### Action 769 770 The action defines an action to perform for a request. 771 772 In the example below, client requests are passed to an upstream `coffee`: 773 ```yaml 774 path: /coffee 775 action: 776 pass: coffee 777 ``` 778 779 ```eval_rst 780 .. list-table:: 781 :header-rows: 1 782 783 * - Field 784 - Description 785 - Type 786 - Required 787 * - ``pass`` 788 - Passes requests to an upstream. The upstream with that name must be defined in the resource. 789 - ``string`` 790 - No* 791 * - ``redirect`` 792 - Redirects requests to a provided URL. 793 - `action.redirect <#action-redirect>`_ 794 - No* 795 * - ``return`` 796 - Returns a preconfigured response. 797 - `action.return <#action-return>`_ 798 - No* 799 * - ``proxy`` 800 - Passes requests to an upstream with the ability to modify the request/response (for example, rewrite the URI or modify the headers). 801 - `action.proxy <#action-proxy>`_ 802 - No* 803 ``` 804 805 \* -- an action must include exactly one of the following: `pass`, `redirect`, `return` or `proxy`. 806 807 ### Action.Redirect 808 809 The redirect action defines a redirect to return for a request. 810 811 In the example below, client requests are passed to a url `http://www.nginx.com`: 812 ```yaml 813 redirect: 814 url: http://www.nginx.com 815 code: 301 816 ``` 817 818 ```eval_rst 819 .. list-table:: 820 :header-rows: 1 821 822 * - Field 823 - Description 824 - Type 825 - Required 826 * - ``url`` 827 - The URL to redirect the request to. Supported NGINX variables: ``$scheme``\ , ``$http_x_forwarded_proto``\ , ``$request_uri``\ , ``$host``. Variables must be enclosed in curly braces. For example: ``${host}${request_uri}``. 828 - ``string`` 829 - Yes 830 * - ``code`` 831 - The status code of a redirect. The allowed values are: ``301``\ , ``302``\ , ``307``\ , ``308``. The default is ``301``. 832 - ``int`` 833 - No 834 ``` 835 836 ### Action.Return 837 838 The return action defines a preconfigured response for a request. 839 840 In the example below, NGINX will respond with the preconfigured response for every request: 841 ```yaml 842 return: 843 code: 200 844 type: text/plain 845 body: "Hello World\n" 846 ``` 847 848 ```eval_rst 849 .. list-table:: 850 :header-rows: 1 851 852 * - Field 853 - Description 854 - Type 855 - Required 856 * - ``code`` 857 - The status code of the response. The allowed values are: ``2XX``, ``4XX`` or ``5XX``. The default is ``200``. 858 - ``int`` 859 - No 860 * - ``type`` 861 - The MIME type of the response. The default is ``text/plain``. 862 - ``string`` 863 - No 864 * - ``body`` 865 - The body of the response. Supports NGINX variables*. Variables must be enclosed in curly brackets. For example: ``Request is ${request_uri}\n``. 866 - ``string`` 867 - Yes 868 ``` 869 870 \* -- Supported NGINX variables: `$request_uri`, `$request_method`, `$request_body`, `$scheme`, `$http_`, `$args`, `$arg_`, `$cookie_`, `$host`, `$request_time`, `$request_length`, `$nginx_version`, `$pid`, `$connection`, `$remote_addr`, `$remote_port`, `$time_iso8601`, `$time_local`, `$server_addr`, `$server_port`, `$server_name`, `$server_protocol`, `$connections_active`, `$connections_reading`, `$connections_writing` and `$connections_waiting`. 871 872 ### Action.Proxy 873 874 The proxy action passes requests to an upstream with the ability to modify the request/response (for example, rewrite the URI or modify the headers). 875 876 In the example below, the request URI is rewritten to `/`, and the request and the response headers are modified: 877 ```yaml 878 proxy: 879 upstream: coffee 880 requestHeaders: 881 pass: true 882 set: 883 - name: My-Header 884 value: Value 885 - name: Client-Cert 886 value: ${ssl_client_escaped_cert} 887 responseHeaders: 888 add: 889 - name: My-Header 890 value: Value 891 - name: IC-Nginx-Version 892 value: ${nginx_version} 893 always: true 894 hide: 895 - x-internal-version 896 ignore: 897 - Expires 898 - Set-Cookie 899 pass: 900 - Server 901 rewritePath: / 902 ``` 903 904 ```eval_rst 905 .. list-table:: 906 :header-rows: 1 907 908 * - Field 909 - Description 910 - Type 911 - Required 912 * - ``upstream`` 913 - The name of the upstream which the requests will be proxied to. The upstream with that name must be defined in the resource. 914 - ``string`` 915 - Yes 916 * - ``requestHeaders`` 917 - The request headers modifications. 918 - `action.Proxy.RequestHeaders <#action-proxy-requestheaders>`_ 919 - No 920 * - ``responseHeaders`` 921 - The response headers modifications. 922 - `action.Proxy.ResponseHeaders <#action-proxy-responseheaders>`_ 923 - No 924 * - ``rewritePath`` 925 - The rewritten URI. If the route path is a regular expression (starts with ~), the rewritePath can include capture groups with ``$1-9``. For example `$1` for the first group, and so on. For more information, check the `rewrite <https://github.com/nginxinc/kubernetes-ingress/tree/v1.12.5/examples-of-custom-resources/rewrites>`_ example. 926 - ``string`` 927 - No 928 ``` 929 930 ### Action.Proxy.RequestHeaders 931 932 The RequestHeaders field modifies the headers of the request to the proxied upstream server. 933 934 ```eval_rst 935 .. list-table:: 936 :header-rows: 1 937 938 * - Field 939 - Description 940 - Type 941 - Required 942 * - ``pass`` 943 - Passes the original request headers to the proxied upstream server. See the `proxy_pass_request_header <http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass_request_headers>`_ directive for more information. Default is true. 944 - ``bool`` 945 - No 946 * - ``set`` 947 - Allows redefining or appending fields to present request headers passed to the proxied upstream servers. See the `proxy_set_header <http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header>`_ directive for more information. 948 - `[]header <#action-proxy-requestheaders-set-header>`_ 949 - No 950 ``` 951 952 ### Action.Proxy.RequestHeaders.Set.Header 953 954 The header defines an HTTP Header: 955 ```yaml 956 name: My-Header 957 value: My-Value 958 ``` 959 960 It is possible to override the default value of the `Host` header, which the Ingress Controller sets to [`$host`](https://nginx.org/en/docs/http/ngx_http_core_module.html#var_host): 961 ```yaml 962 name: Host 963 value: example.com 964 ``` 965 966 ```eval_rst 967 .. list-table:: 968 :header-rows: 1 969 970 * - Field 971 - Description 972 - Type 973 - Required 974 * - ``name`` 975 - The name of the header. 976 - ``string`` 977 - Yes 978 * - ``value`` 979 - The value of the header. Supports NGINX variables*. Variables must be enclosed in curly brackets. For example: ``${scheme}``. 980 - ``string`` 981 - No 982 ``` 983 984 \* -- Supported NGINX variables: `$request_uri`, `$request_method`, `$request_body`, `$scheme`, `$http_`, `$args`, `$arg_`, `$cookie_`, `$host`, `$request_time`, `$request_length`, `$nginx_version`, `$pid`, `$connection`, `$remote_addr`, `$remote_port`, `$time_iso8601`, `$time_local`, `$server_addr`, `$server_port`, `$server_name`, `$server_protocol`, `$connections_active`, `$connections_reading`, `$connections_writing`, `$connections_waiting`, `$ssl_cipher`, `$ssl_ciphers`, `$ssl_client_cert`, `$ssl_client_escaped_cert`, `$ssl_client_fingerprint`, `$ssl_client_i_dn`, `$ssl_client_i_dn_legacy`, `$ssl_client_raw_cert`, `$ssl_client_s_dn`, `$ssl_client_s_dn_legacy`, `$ssl_client_serial`, `$ssl_client_v_end`, `$ssl_client_v_remain`, `$ssl_client_v_start`, `$ssl_client_verify`, `$ssl_curves`, `$ssl_early_data`, `$ssl_protocol`, `$ssl_server_name`, `$ssl_session_id`, `$ssl_session_reused`, `$jwt_claim_` (NGINX Plus only) and `$jwt_header_` (NGINX Plus only). 985 986 ### Action.Proxy.ResponseHeaders 987 988 The ResponseHeaders field modifies the headers of the response to the client. 989 990 ```eval_rst 991 .. list-table:: 992 :header-rows: 1 993 994 * - Field 995 - Description 996 - Type 997 - Required 998 * - ``hide`` 999 - The headers that will not be passed* in the response to the client from a proxied upstream server. See the `proxy_hide_header <http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_header>`_ directive for more information. 1000 - ``bool`` 1001 - No 1002 * - ``pass`` 1003 - Allows passing the hidden header fields* to the client from a proxied upstream server. See the `proxy_pass_header <http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass_header>`_ directive for more information. 1004 - ``[]string`` 1005 - No 1006 * - ``ignore`` 1007 - Disables processing of certain headers** to the client from a proxied upstream server. See the `proxy_ignore_headers <http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ignore_headers>`_ directive for more information. 1008 - ``[]string`` 1009 - No 1010 * - ``add`` 1011 - Adds headers to the response to the client. 1012 - `[]addHeader <#addheader>`_ 1013 - No 1014 ``` 1015 1016 \* -- Default hidden headers are: `Date`, `Server`, `X-Pad` and `X-Accel-...`. 1017 1018 \** -- The following fields can be ignored: `X-Accel-Redirect`, `X-Accel-Expires`, `X-Accel-Limit-Rate`, `X-Accel-Buffering`, `X-Accel-Charset`, `Expires`, `Cache-Control`, `Set-Cookie` and `Vary`. 1019 1020 ### AddHeader 1021 1022 The addHeader defines an HTTP Header with an optional `always` field: 1023 ```yaml 1024 name: My-Header 1025 value: My-Value 1026 always: true 1027 ``` 1028 1029 ```eval_rst 1030 .. list-table:: 1031 :header-rows: 1 1032 1033 * - Field 1034 - Description 1035 - Type 1036 - Required 1037 * - ``name`` 1038 - The name of the header. 1039 - ``string`` 1040 - Yes 1041 * - ``value`` 1042 - The value of the header. Supports NGINX variables*. Variables must be enclosed in curly brackets. For example: ``${scheme}``. 1043 - ``string`` 1044 - No 1045 * - ``always`` 1046 - If set to true, add the header regardless of the response status code**. Default is false. See the `add_header <http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header>`_ directive for more information. 1047 - ``bool`` 1048 - No 1049 ``` 1050 1051 \* -- Supported NGINX variables: `$request_uri`, `$request_method`, `$request_body`, `$scheme`, `$http_`, `$args`, `$arg_`, `$cookie_`, `$host`, `$request_time`, `$request_length`, `$nginx_version`, `$pid`, `$connection`, `$remote_addr`, `$remote_port`, `$time_iso8601`, `$time_local`, `$server_addr`, `$server_port`, `$server_name`, `$server_protocol`, `$connections_active`, `$connections_reading`, `$connections_writing`, `$connections_waiting`, `$ssl_cipher`, `$ssl_ciphers`, `$ssl_client_cert`, `$ssl_client_escaped_cert`, `$ssl_client_fingerprint`, `$ssl_client_i_dn`, `$ssl_client_i_dn_legacy`, `$ssl_client_raw_cert`, `$ssl_client_s_dn`, `$ssl_client_s_dn_legacy`, `$ssl_client_serial`, `$ssl_client_v_end`, `$ssl_client_v_remain`, `$ssl_client_v_start`, `$ssl_client_verify`, `$ssl_curves`, `$ssl_early_data`, `$ssl_protocol`, `$ssl_server_name`, `$ssl_session_id`, `$ssl_session_reused`, `$jwt_claim_` (NGINX Plus only) and `$jwt_header_` (NGINX Plus only). 1052 1053 \*\* -- If `always` is false, the response header is added only if the response status code is any of `200`, `201`, `204`, `206`, `301`, `302`, `303`, `304`, `307` or `308`. 1054 1055 ### Split 1056 1057 The split defines a weight for an action as part of the splits configuration. 1058 1059 In the example below NGINX passes 80% of requests to the upstream `coffee-v1` and the remaining 20% to `coffee-v2`: 1060 ```yaml 1061 splits: 1062 - weight: 80 1063 action: 1064 pass: coffee-v1 1065 - weight: 20 1066 action: 1067 pass: coffee-v2 1068 ``` 1069 1070 ```eval_rst 1071 .. list-table:: 1072 :header-rows: 1 1073 1074 * - Field 1075 - Description 1076 - Type 1077 - Required 1078 * - ``weight`` 1079 - The weight of an action. Must fall into the range ``1..99``. The sum of the weights of all splits must be equal to ``100``. 1080 - ``int`` 1081 - Yes 1082 * - ``action`` 1083 - The action to perform for a request. 1084 - `action <#action>`_ 1085 - Yes 1086 ``` 1087 1088 ### Match 1089 1090 The match defines a match between conditions and an action or splits. 1091 1092 In the example below, NGINX routes requests with the path `/coffee` to different upstreams based on the value of the cookie `user`: 1093 * `user=john` -> `coffee-future` 1094 * `user=bob` -> `coffee-deprecated` 1095 * If the cookie is not set or not equal to either `john` or `bob`, NGINX routes to `coffee-stable` 1096 1097 ```yaml 1098 path: /coffee 1099 matches: 1100 - conditions: 1101 - cookie: user 1102 value: john 1103 action: 1104 pass: coffee-future 1105 - conditions: 1106 - cookie: user 1107 value: bob 1108 action: 1109 pass: coffee-deprecated 1110 action: 1111 pass: coffee-stable 1112 ``` 1113 1114 In the next example, NGINX routes requests based on the value of the built-in [`$request_method` variable](https://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_method), which represents the HTTP method of a request: 1115 * all POST requests -> `coffee-post` 1116 * all non-POST requests -> `coffee` 1117 1118 ```yaml 1119 path: /coffee 1120 matches: 1121 - conditions: 1122 - variable: $request_method 1123 value: POST 1124 action: 1125 pass: coffee-post 1126 action: 1127 pass: coffee 1128 ``` 1129 1130 ```eval_rst 1131 .. list-table:: 1132 :header-rows: 1 1133 1134 * - Field 1135 - Description 1136 - Type 1137 - Required 1138 * - ``conditions`` 1139 - A list of conditions. Must include at least 1 condition. 1140 - `[]condition <#condition>`_ 1141 - Yes 1142 * - ``action`` 1143 - The action to perform for a request. 1144 - `action <#action>`_ 1145 - No* 1146 * - ``splits`` 1147 - The splits configuration for traffic splitting. Must include at least 2 splits. 1148 - `[]split <#split>`_ 1149 - No* 1150 ``` 1151 1152 \* -- a match must include exactly one of the following: `action` or `splits`. 1153 1154 ### Condition 1155 1156 The condition defines a condition in a match. 1157 1158 ```eval_rst 1159 .. list-table:: 1160 :header-rows: 1 1161 1162 * - Field 1163 - Description 1164 - Type 1165 - Required 1166 * - ``header`` 1167 - The name of a header. Must consist of alphanumeric characters or ``-``. 1168 - ``string`` 1169 - No* 1170 * - ``cookie`` 1171 - The name of a cookie. Must consist of alphanumeric characters or ``_``. 1172 - ``string`` 1173 - No* 1174 * - ``argument`` 1175 - The name of an argument. Must consist of alphanumeric characters or ``_``. 1176 - ``string`` 1177 - No* 1178 * - ``variable`` 1179 - The name of an NGINX variable. Must start with ``$``. See the list of the supported variables below the table. 1180 - ``string`` 1181 - No* 1182 * - ``value`` 1183 - The value to match the condition against. How to define a value is shown below the table. 1184 - ``string`` 1185 - Yes 1186 ``` 1187 1188 \* -- a condition must include exactly one of the following: `header`, `cookie`, `argument` or `variable`. 1189 1190 Supported NGINX variables: `$args`, `$http2`, `$https`, `$remote_addr`, `$remote_port`, `$query_string`, `$request`, `$request_body`, `$request_uri`, `$request_method`, `$scheme`. Find the documentation for each variable [here](https://nginx.org/en/docs/varindex.html). 1191 1192 The value supports two kinds of matching: 1193 * *Case-insensitive string comparison*. For example: 1194 * `john` -- case-insensitive matching that succeeds for strings, such as `john`, `John`, `JOHN`. 1195 * `!john` -- negation of the case-incentive matching for john that succeeds for strings, such as `bob`, `anything`, `''` (empty string). 1196 * *Matching with a regular expression*. Note that NGINX supports regular expressions compatible with those used by the Perl programming language (PCRE). For example: 1197 * `~^yes` -- a case-sensitive regular expression that matches any string that starts with `yes`. For example: `yes`, `yes123`. 1198 * `!~^yes` -- negation of the previous regular expression that succeeds for strings like `YES`, `Yes123`, `noyes`. (The negation mechanism is not part of the PCRE syntax). 1199 * `~*no$` -- a case-insensitive regular expression that matches any string that ends with `no`. For example: `no`, `123no`, `123NO`. 1200 1201 **Note**: a value must not include any unescaped double quotes (`"`) and must not end with an unescaped backslash (`\`). For example, the following are invalid values: `some"value`, `somevalue\`. 1202 1203 1204 ### ErrorPage 1205 1206 The errorPage defines a custom response for a route for the case when either an upstream server responds with (or NGINX generates) an error status code. The custom response can be a redirect or a canned response. See the [error_page](https://nginx.org/en/docs/http/ngx_http_core_module.html#error_page) directive for more information. 1207 ```yaml 1208 path: /coffee 1209 errorPages: 1210 - codes: [502, 503] 1211 redirect: 1212 code: 301 1213 url: https://nginx.org 1214 - codes: [404] 1215 return: 1216 code: 200 1217 body: "Original resource not found, but success!" 1218 ``` 1219 1220 ```eval_rst 1221 .. list-table:: 1222 :header-rows: 1 1223 1224 * - Field 1225 - Description 1226 - Type 1227 - Required 1228 * - ``codes`` 1229 - A list of error status codes. 1230 - ``[]int`` 1231 - Yes 1232 * - ``redirect`` 1233 - The redirect action for the given status codes. 1234 - `errorPage.Redirect <#errorpage-redirect>`_ 1235 - No* 1236 * - ``return`` 1237 - The canned response action for the given status codes. 1238 - `errorPage.Return <#errorpage-return>`_ 1239 - No* 1240 ``` 1241 1242 \* -- an errorPage must include exactly one of the following: `return` or `redirect`. 1243 1244 ### ErrorPage.Redirect 1245 1246 The redirect defines a redirect for an errorPage. 1247 1248 In the example below, NGINX responds with a redirect when a response from an upstream server has a 404 status code. 1249 1250 ```yaml 1251 codes: [404] 1252 redirect: 1253 code: 301 1254 url: ${scheme}://cafe.example.com/error.html 1255 ``` 1256 1257 ```eval_rst 1258 .. list-table:: 1259 :header-rows: 1 1260 1261 * - Field 1262 - Description 1263 - Type 1264 - Required 1265 * - ``code`` 1266 - The status code of a redirect. The allowed values are: ``301``\ , ``302``\ , ``307``\ , ``308``. The default is ``301``. 1267 - ``int`` 1268 - No 1269 * - ``url`` 1270 - The URL to redirect the request to. Supported NGINX variables: ``$scheme``\ and ``$http_x_forwarded_proto``\. Variables must be enclosed in curly braces. For example: ``${scheme}``. 1271 - ``string`` 1272 - Yes 1273 ``` 1274 1275 ### ErrorPage.Return 1276 1277 The return defines a canned response for an errorPage. 1278 1279 In the example below, NGINX responds with a canned response when a response from an upstream server has either 401 or 403 status code. 1280 1281 ```yaml 1282 codes: [401, 403] 1283 return: 1284 code: 200 1285 type: application/json 1286 body: | 1287 {\"msg\": \"You don't have permission to do this\"} 1288 headers: 1289 - name: x-debug-original-statuses 1290 value: ${upstream_status} 1291 ``` 1292 1293 ```eval_rst 1294 .. list-table:: 1295 :header-rows: 1 1296 1297 * - Field 1298 - Description 1299 - Type 1300 - Required 1301 * - ``code`` 1302 - The status code of the response. The default is the status code of the original response. 1303 - ``int`` 1304 - No 1305 * - ``type`` 1306 - The MIME type of the response. The default is ``text/html``. 1307 - ``string`` 1308 - No 1309 * - ``body`` 1310 - The body of the response. Supported NGINX variable: ``$upstream_status`` \ . Variables must be enclosed in curly braces. For example: ``${upstream_status}``. 1311 - ``string`` 1312 - Yes 1313 * - ``headers`` 1314 - The custom headers of the response. 1315 - `errorPage.Return.Header <#errorpage-return-header>`_ 1316 - No 1317 ``` 1318 1319 ### ErrorPage.Return.Header 1320 1321 The header defines an HTTP Header for a canned response in an errorPage: 1322 1323 ```yaml 1324 name: x-debug-original-statuses 1325 value: ${upstream_status} 1326 ``` 1327 1328 ```eval_rst 1329 .. list-table:: 1330 :header-rows: 1 1331 1332 * - Field 1333 - Description 1334 - Type 1335 - Required 1336 * - ``name`` 1337 - The name of the header. 1338 - ``string`` 1339 - Yes 1340 * - ``value`` 1341 - The value of the header. Supported NGINX variable: ``$upstream_status`` \ . Variables must be enclosed in curly braces. For example: ``${upstream_status}``. 1342 - ``string`` 1343 - No 1344 ``` 1345 1346 ## Using VirtualServer and VirtualServerRoute 1347 1348 You can use the usual `kubectl` commands to work with VirtualServer and VirtualServerRoute resources, similar to Ingress resources. 1349 1350 For example, the following command creates a VirtualServer resource defined in `cafe-virtual-server.yaml` with the name `cafe`: 1351 ``` 1352 $ kubectl apply -f cafe-virtual-server.yaml 1353 virtualserver.k8s.nginx.org "cafe" created 1354 ``` 1355 1356 You can get the resource by running: 1357 ``` 1358 $ kubectl get virtualserver cafe 1359 NAME STATE HOST IP PORTS AGE 1360 cafe Valid cafe.example.com 12.13.23.123 [80,443] 3m 1361 ``` 1362 1363 In the kubectl get and similar commands, you can also use the short name `vs` instead of `virtualserver`. 1364 1365 Working with VirtualServerRoute resources is analogous. In the kubectl commands, use `virtualserverroute` or the short name `vsr`. 1366 1367 ### Using Snippets 1368 1369 Snippets allow you to insert raw NGINX config into different contexts of NGINX configuration. In the example below, we use snippets to configure several NGINX features in a VirtualServer: 1370 1371 ```yaml 1372 apiVersion: k8s.nginx.org/v1 1373 kind: VirtualServer 1374 metadata: 1375 name: cafe 1376 namespace: cafe 1377 spec: 1378 http-snippets: | 1379 limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s; 1380 proxy_cache_path /tmp keys_zone=one:10m; 1381 host: cafe.example.com 1382 tls: 1383 secret: cafe-secret 1384 server-snippets: | 1385 limit_req zone=mylimit burst=20; 1386 upstreams: 1387 - name: tea 1388 service: tea-svc 1389 port: 80 1390 - name: coffee 1391 service: coffee-svc 1392 port: 80 1393 routes: 1394 - path: /tea 1395 location-snippets: | 1396 proxy_cache one; 1397 proxy_cache_valid 200 10m; 1398 action: 1399 pass: tea 1400 - path: /coffee 1401 action: 1402 pass: coffee 1403 ``` 1404 1405 Snippets are intended to be used by advanced NGINX users who need more control over the generated NGINX configuration. 1406 1407 However, because of the disadvantages described below, snippets are disabled by default. To use snippets, set the [`enable-snippets`](/nginx-ingress-controller/configuration/global-configuration/command-line-arguments#cmdoption-enable-snippets) command-line argument. 1408 1409 Disadvantages of using snippets: 1410 * *Complexity*. To use snippets, you will need to: 1411 * Understand NGINX configuration primitives and implement a correct NGINX configuration. 1412 * Understand how the IC generates NGINX configuration so that a snippet doesn't interfere with the other features in the configuration. 1413 * *Decreased robustness*. An incorrect snippet makes the NGINX config invalid which will lead to a failed reload. This will prevent any new configuration updates, including updates for the other VirtualServer and VirtualServerRoute resources until the snippet is fixed. 1414 * *Security implications*. Snippets give access to NGINX configuration primitives and those primitives are not validated by the Ingress Controller. For example, a snippet can configure NGINX to serve the TLS certificates and keys used for TLS termination for Ingress and VirtualServer resources. 1415 1416 To help catch errors when using snippets, the Ingress Controller reports config reload errors in the logs as well as in the events and status field of VirtualServer and VirtualServerRoute resources. Additionally, a number of Prometheus metrics show the stats about failed reloads – `controller_nginx_last_reload_status` and `controller_nginx_reload_errors_total`. 1417 1418 > Note that during a period when the NGINX config includes an invalid snippet, NGINX will continue to operate with the latest valid configuration. 1419 1420 ### Validation 1421 1422 Two types of validation are available for VirtualServer and VirtualServerRoute resources: 1423 * *Structural validation* by the `kubectl` and Kubernetes API server. 1424 * *Comprehensive validation* by the Ingress Controller. 1425 1426 #### Structural Validation 1427 1428 The custom resource definitions for VirtualServer and VirtualServerRoute include structural OpenAPI schema which describes the type of every field of those resources. 1429 1430 If you try to create (or update) a resource that violates the structural schema (for example, you use a string value for the port field of an upstream), `kubectl` and Kubernetes API server will reject such a resource: 1431 * Example of `kubectl` validation: 1432 ``` 1433 $ kubectl apply -f cafe-virtual-server.yaml 1434 error: error validating "cafe-virtual-server.yaml": error validating data: ValidationError(VirtualServer.spec.upstreams[0].port): invalid type for org.nginx.k8s.v1.VirtualServer.spec.upstreams.port: got "string", expected "integer"; if you choose to ignore these errors, turn validation off with --validate=false 1435 ``` 1436 * Example of Kubernetes API server validation: 1437 ``` 1438 $ kubectl apply -f cafe-virtual-server.yaml --validate=false 1439 The VirtualServer "cafe" is invalid: []: Invalid value: map[string]interface {}{ ... }: validation failure list: 1440 spec.upstreams.port in body must be of type integer: "string" 1441 ``` 1442 1443 If a resource is not rejected (it doesn't violate the structural schema), the Ingress Controller will validate it further. 1444 1445 #### Comprehensive Validation 1446 1447 The Ingress Controller validates the fields of the VirtualServer and VirtualServerRoute resources. If a resource is invalid, the Ingress Controller will reject it: the resource will continue to exist in the cluster, but the Ingress Controller will ignore it. 1448 1449 You can check if the Ingress Controller successfully applied the configuration for a VirtualServer. For our example `cafe` VirtualServer, we can run: 1450 ``` 1451 $ kubectl describe vs cafe 1452 . . . 1453 Events: 1454 Type Reason Age From Message 1455 ---- ------ ---- ---- ------- 1456 Normal AddedOrUpdated 16s nginx-ingress-controller Configuration for default/cafe was added or updated 1457 ``` 1458 Note how the events section includes a Normal event with the AddedOrUpdated reason that informs us that the configuration was successfully applied. 1459 1460 If you create an invalid resource, the Ingress Controller will reject it and emit a Rejected event. For example, if you create a VirtualServer `cafe` with two upstream with the same name `tea`, you will get: 1461 ``` 1462 $ kubectl describe vs cafe 1463 . . . 1464 Events: 1465 Type Reason Age From Message 1466 ---- ------ ---- ---- ------- 1467 Warning Rejected 12s nginx-ingress-controller VirtualServer default/cafe is invalid and was rejected: spec.upstreams[1].name: Duplicate value: "tea" 1468 ``` 1469 Note how the events section includes a Warning event with the Rejected reason. 1470 1471 Additionally, this information is also available in the `status` field of the VirtualServer resource. Note the Status section of the VirtualServer: 1472 1473 ``` 1474 $ kubectl describe vs cafe 1475 . . . 1476 Status: 1477 External Endpoints: 1478 Ip: 12.13.23.123 1479 Ports: [80,443] 1480 Message: VirtualServer default/cafe is invalid and was rejected: spec.upstreams[1].name: Duplicate value: "tea" 1481 Reason: Rejected 1482 State: Invalid 1483 ``` 1484 1485 The Ingress Controller validates VirtualServerRoute resources in a similar way. 1486 1487 **Note**: If you make an existing resource invalid, the Ingress Controller will reject it and remove the corresponding configuration from NGINX. 1488 1489 ## Customization via ConfigMap 1490 1491 You can customize the NGINX configuration for VirtualServer and VirtualServerRoutes resources using the [ConfigMap](/nginx-ingress-controller/configuration/global-configuration/configmap-resource). Most of the ConfigMap keys are supported, with the following exceptions: 1492 * `proxy-hide-headers` 1493 * `proxy-pass-headers` 1494 * `hsts` 1495 * `hsts-max-age` 1496 * `hsts-include-subdomains` 1497 * `hsts-behind-proxy` 1498 * `redirect-to-https` 1499 * `ssl-redirect`