-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce annotation for rewrite rule #63
Comments
I think we can add something like this: apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: cafe-ingress
annotations:
nginx.org/rewrites: "serviceName=tea rewrite=/;serviceName=coffee-svc rewrite=/beans/"
spec:
rules:
- host: cafe.example.com
http:
paths:
- path: /tea/
backend:
serviceName: tea-svc
servicePort: 80
- path: /coffee/
backend:
serviceName: coffee-svc
servicePort: 80 Requests to the tea service are rewritten as follows:
Requests to the coffee service are rewritten as follows:
To accomplish that the following NGINX configuration can be used: location /tea/ {
proxy_pass http://tea-svc/;
}
location /coffee/ {
proxy_pass http://coffee-svc/beans/;
} See http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass for details. However, rewrites don't guarantee that the application will work. Often applications return web pages with absolute links so that the rewriting of response bodies is also necessary. |
I guess there is no way to rewrite the response body with our NGINX template, like:
as well? |
location /tea/ {
proxy_pass http://tea-svc/;
}
location /coffee/ {
proxy_pass http://coffee-svc/beans/;
} I implemented this and it does not work for my use case. What I need is something like this: location /tea {
proxy_pass http://tea-svc;
}
location /coffee {
rewrite ^/coffee/(.*) /$1 break;
proxy_pass http://coffee-svc;
} |
Rewriting of the response body of a request is done with the sub_filter module. Typically it requires many rewrite rules. For example,
It would be very messy and complicated if we put such rules in an annotation. For special cases like rewrites, I can suggest the following:
location {{$location.Path}} {
...
{{if eq $location.Path "/rabitmq/"}}
# rewrite url
# rewrite ...
proxy_pass http://{{$location.Upstream.Name}};
# sub_filter rules to rewrite the response
# sub_filter ...
{{else}}
# handle default cases
proxy_pass http://{{$location.Upstream.Name}};
{{end}}
}{{end}} This way you can customize the location block based on the location path, for example. More info about go templates is here https://golang.org/pkg/text/template/ Will any of those work for you? |
@jmastr For the following code snippet
A request to |
With this config: location /rabbitmq {
proxy_http_version 1.1;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
client_max_body_size 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://default-cafe-ingress-cafe.example.com-rabbitmq-svc/rabbitmq;
} I get:
With: location /rabbitmq {
proxy_http_version 1.1;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
client_max_body_size 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
rewrite ^/rabbitmq/(.*) /$1 break;
proxy_pass http://default-cafe-ingress-cafe.example.com-rabbitmq-svc;
} I get (what I want):
I just can state how it is... |
@jmastr location /rabbitmq/ {
proxy_http_version 1.1;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
client_max_body_size 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://default-cafe-ingress-cafe.example.com-rabbitmq-svc/rabbitmq/;
} |
This: location /rabbitmq/ {
proxy_http_version 1.1;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
client_max_body_size 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://default-cafe-ingress-cafe.example.com-rabbitmq-svc/rabbitmq/;
} gives me this:
and this:
Btw. thank you very much for your support/help. |
@jmastr
location /rabbitmq/ {
....
proxy_pass http://default-cafe-ingress-cafe.example.com-rabbitmq-svc/;
} Thx! no problem! |
Great! That works! location /coffee/ {
...
proxy_pass http://default-cafe-ingress-cafe.example.com-coffee-svc/;
} works as well! Can I just add those two |
If it works for your applications, I assume so. But doesn't confirm to Ingress API if it is enabled by default though. |
See #60
Instead of hard-coding the rewrite rule one could introduce a further annotation like in #61.
The text was updated successfully, but these errors were encountered: