2017-03-15 01:22:02 +00:00
# Rewrite
2017-03-13 15:03:47 +00:00
This example demonstrates how to use the Rewrite annotations
## Prerequisites
You will need to make sure you Ingress targets exactly one Ingress
2018-01-18 15:30:29 +00:00
controller by specifying the [ingress.class annotation ](/README.md#annotation-ingressclass ),
and that you have an ingress controller [running ](/deploy/README.md ) in your cluster.
2017-03-13 15:03:47 +00:00
## Deployment
Rewriting can be controlled using the following annotations:
|Name|Description|Values|
| --- | --- | --- |
2017-11-24 18:46:51 +00:00
|nginx.ingress.kubernetes.io/rewrite-target|Target URI where the traffic must be redirected|string|
|nginx.ingress.kubernetes.io/add-base-url|indicates if is required to add a base tag in the head of the responses from the upstream servers|bool|
|nginx.ingress.kubernetes.io/base-url-scheme|Override for the scheme passed to the base tag|string|
|nginx.ingress.kubernetes.io/ssl-redirect|Indicates if the location section is accessible SSL only (defaults to True when Ingress contains a Certificate)|bool|
|nginx.ingress.kubernetes.io/force-ssl-redirect|Forces the redirection to HTTPS even if the Ingress is not TLS Enabled|bool|
2017-12-13 17:29:41 +00:00
|nginx.ingress.kubernetes.io/app-root|Defines the Application Root that the Controller must redirect if it's in '/' context|string|
2017-03-13 15:03:47 +00:00
## Validation
### Rewrite Target
2017-10-16 12:55:46 +00:00
2017-03-08 12:02:13 +00:00
Create an Ingress rule with a rewrite annotation:
2017-10-16 12:55:46 +00:00
```console
2017-03-08 12:02:13 +00:00
$ echo "
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
2017-11-24 18:46:51 +00:00
nginx.ingress.kubernetes.io/rewrite-target: /
2017-03-08 12:02:13 +00:00
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
2017-10-13 13:55:03 +00:00
serviceName: http-svc
2017-03-08 12:02:13 +00:00
servicePort: 80
path: /something
" | kubectl create -f -
```
Check the rewrite is working
```
$ curl -v http://172.17.4.99/something -H 'Host: rewrite.bar.com'
* Trying 172.17.4.99...
* Connected to 172.17.4.99 (172.17.4.99) port 80 (#0)
> GET /something HTTP/1.1
> Host: rewrite.bar.com
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP / 1 . 1 200 OK
< Server: nginx / 1 . 11 . 0
< Date: Tue , 31 May 2016 16:07:31 GMT
< Content-Type: text / plain
< Transfer-Encoding: chunked
< Connection: keep-alive
<
CLIENT VALUES:
client_address=10.2.56.9
command=GET
real path=/
query=nil
request_version=1.1
request_uri=http://rewrite.bar.com:8080/
SERVER VALUES:
server_version=nginx: 1.9.11 - lua: 10001
HEADERS RECEIVED:
accept=*/*
connection=close
host=rewrite.bar.com
user-agent=curl/7.43.0
x-forwarded-for=10.2.56.1
x-forwarded-host=rewrite.bar.com
x-forwarded-port=80
x-forwarded-proto=http
x-real-ip=10.2.56.1
BODY:
* Connection #0 to host 172.17.4.99 left intact
-no body in request-
```
2017-03-13 15:03:47 +00:00
### App Root
Create an Ingress rule with a app-root annotation:
```
$ echo "
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
2017-11-24 18:46:51 +00:00
nginx.ingress.kubernetes.io/app-root: /app1
2017-03-13 15:03:47 +00:00
name: approot
namespace: default
spec:
rules:
- host: approot.bar.com
http:
paths:
- backend:
2017-10-13 13:55:03 +00:00
serviceName: http-svc
2017-03-13 15:03:47 +00:00
servicePort: 80
path: /
" | kubectl create -f -
```
Check the rewrite is working
```
$ curl -I -k http://approot.bar.com/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.11.10
Date: Mon, 13 Mar 2017 14:57:15 GMT
Content-Type: text/html
Content-Length: 162
Location: http://stickyingress.example.com/app1
Connection: keep-alive
2017-03-15 01:22:02 +00:00
```