@@ -87,6 +87,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
8787
8888 wsServices := getWebsocketServices (ingEx )
8989 spServices := getSessionPersistenceServices (ingEx )
90+ rewrites := getRewrites (ingEx )
9091 sslServices := getSSLServices (ingEx )
9192
9293 if ingEx .Ingress .Spec .Backend != nil {
@@ -129,7 +130,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
129130 upstreams [upsName ] = upstream
130131 }
131132
132- loc := createLocation (pathOrDefault (path .Path ), upstreams [upsName ], & ingCfg , wsServices [path .Backend .ServiceName ], sslServices [path .Backend .ServiceName ])
133+ loc := createLocation (pathOrDefault (path .Path ), upstreams [upsName ], & ingCfg , wsServices [path .Backend .ServiceName ], rewrites [ path . Backend . ServiceName ], sslServices [path .Backend .ServiceName ])
133134 locations = append (locations , loc )
134135
135136 if loc .Path == "/" {
@@ -139,7 +140,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
139140
140141 if rootLocation == false && ingEx .Ingress .Spec .Backend != nil {
141142 upsName := getNameForUpstream (ingEx .Ingress , emptyHost , ingEx .Ingress .Spec .Backend .ServiceName )
142- loc := createLocation (pathOrDefault ("/" ), upstreams [upsName ], & ingCfg , wsServices [ingEx .Ingress .Spec .Backend .ServiceName ], sslServices [ingEx .Ingress .Spec .Backend .ServiceName ])
143+ loc := createLocation (pathOrDefault ("/" ), upstreams [upsName ], & ingCfg , wsServices [ingEx .Ingress .Spec .Backend .ServiceName ], rewrites [ ingEx . Ingress . Spec . Backend . ServiceName ], sslServices [ingEx .Ingress .Spec .Backend .ServiceName ])
143144 locations = append (locations , loc )
144145 }
145146
@@ -164,7 +165,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
164165
165166 upsName := getNameForUpstream (ingEx .Ingress , emptyHost , ingEx .Ingress .Spec .Backend .ServiceName )
166167
167- loc := createLocation (pathOrDefault ("/" ), upstreams [upsName ], & ingCfg , wsServices [ingEx .Ingress .Spec .Backend .ServiceName ], sslServices [ingEx .Ingress .Spec .Backend .ServiceName ])
168+ loc := createLocation (pathOrDefault ("/" ), upstreams [upsName ], & ingCfg , wsServices [ingEx .Ingress .Spec .Backend .ServiceName ], rewrites [ ingEx . Ingress . Spec . Backend . ServiceName ], sslServices [ingEx .Ingress .Spec .Backend .ServiceName ])
168169 locations = append (locations , loc )
169170
170171 server .Locations = locations
@@ -201,6 +202,42 @@ func getWebsocketServices(ingEx *IngressEx) map[string]bool {
201202 return wsServices
202203}
203204
205+ func getRewrites (ingEx * IngressEx ) map [string ]string {
206+ rewrites := make (map [string ]string )
207+
208+ if services , exists := ingEx .Ingress .Annotations ["nginx.org/rewrites" ]; exists {
209+ for _ , svc := range strings .Split (services , ";" ) {
210+ if serviceName , rewrite , err := parseRewrites (svc ); err != nil {
211+ glog .Errorf ("In %v nginx.org/rewrites contains invalid declaration: %v, ignoring" , ingEx .Ingress .Name , err )
212+ } else {
213+ rewrites [serviceName ] = rewrite
214+ }
215+ }
216+ }
217+
218+ return rewrites
219+ }
220+
221+ func parseRewrites (service string ) (serviceName string , rewrite string , err error ) {
222+ parts := strings .SplitN (service , " " , 2 )
223+
224+ if len (parts ) != 2 {
225+ return "" , "" , fmt .Errorf ("Invalid rewrite format: %s\n " , service )
226+ }
227+
228+ svcNameParts := strings .Split (parts [0 ], "=" )
229+ if len (svcNameParts ) != 2 {
230+ return "" , "" , fmt .Errorf ("Invalid rewrite format: %s\n " , svcNameParts )
231+ }
232+
233+ rwPathParts := strings .Split (parts [1 ], "=" )
234+ if len (rwPathParts ) != 2 {
235+ return "" , "" , fmt .Errorf ("Invalid rewrite format: %s\n " , rwPathParts )
236+ }
237+
238+ return svcNameParts [1 ], rwPathParts [1 ], nil
239+ }
240+
204241func getSSLServices (ingEx * IngressEx ) map [string ]bool {
205242 sslServices := make (map [string ]bool )
206243
@@ -244,14 +281,15 @@ func parseStickyService(service string) (serviceName string, stickyCookie string
244281 return svcNameParts [1 ], parts [1 ], nil
245282}
246283
247- func createLocation (path string , upstream Upstream , cfg * Config , websocket bool , ssl bool ) Location {
284+ func createLocation (path string , upstream Upstream , cfg * Config , websocket bool , rewrite string , ssl bool ) Location {
248285 loc := Location {
249286 Path : path ,
250287 Upstream : upstream ,
251288 ProxyConnectTimeout : cfg .ProxyConnectTimeout ,
252289 ProxyReadTimeout : cfg .ProxyReadTimeout ,
253290 ClientMaxBodySize : cfg .ClientMaxBodySize ,
254291 Websocket : websocket ,
292+ Rewrite : rewrite ,
255293 SSL : ssl ,
256294 }
257295
0 commit comments