Hot change "Location" in the reply header 302 code from proxy-host.

4/25/2018

I have the kubernetes-cluster and a few pods/containers with web-app. Pods connecting to each other by pod's name with listen port 9999 (like security-rest-api:9999, common-rest-api:9999, etc). To outside listen nginx-pod with outside address http://e.net:30200/.

((app-pods:9999)-(nginx-pod:80)-(nginx-service:30200))-Network

Nginx use follow configuration for interactive with app-pods.

    server {

    listen       80;
    server_name  e.net;

    location / {
        proxy_pass              http://web-console:9999/;
        proxy_redirect          http://web-console:9999/ http://e.net:30200/;  
    }

    location /common {
        proxy_pass              http://common-rest-api:9999/common;
        proxy_redirect          http://common-rest-api:9999/ http://e.net:30200/;  

    }

    location /security {
        proxy_pass              http://security-rest-api:9999/security;
        proxy_redirect          http://security-rest-api:9999/ http://e.net:30200/;     

    } }

It's working very well, but I have the one problem with 302-reply from app-pods:

If I try to login in my app, I get follow the 302 reply header:

HTTP/1.1 302 Found
Connection: keep-alive
Content-Length: 0
Date: Wed, 25 Apr 2018 10:37:50 GMT
Location: http://e.net:30200/security/rest/auth/login.html?callbackUrl=http://security-rest-api:9999/security/rest/namespace
Server: nginx/1.13.9

App-pods generated URL parameter "callbackUrl" from the Host request header Inside containers network and this URL parameter to get to the endpoint browser. Of course, next request get 404 code.

I can't to edit app-code (in app-pods don't use nginx), but I want to change 'security-rest-api:9999' to 'e.net:30200' parameter in the Location 302 reply header. How I can do it?

redirect isn't suitable since this generate new 302-reply and not solve my problem. sub_filter change only reply body, but not reply head (where is Location parameter). request_uri not working too, since this parameter work with request header only.

-- Piknik
kubernetes
nginx

1 Answer

5/2/2018

No, It's not working.

I tested this situation and finded work's config:

if ($args ~* (.*)(callbackUrl=http://security-rest-api:9999/)(.*)) {
    set $args $1callbackUrl=http://e.net:30200/$3;
    rewrite ^(.*)$ $1;
}
if ($args ~* (.*)(callbackURL=http%3A%2F%2Fsecurity-rest-api%3A9999%2F)(.*)) {
    set $args $1callbackURL=http%3A%2F%2Fe.net%3A30200%2F$3;
    rewrite ^(.*)$ $1;
}


location /security {
    proxy_pass http://security-rest-api:9999/security;
    proxy_set_header Host $http_host;
    proxy_redirect http://security-rest-api:9999/ http://e.net:30200/;

}

Later, I will try to use this config on the pre-production stand and if this work (or work after corrects) - I will write it here.

Thanks for help information: https://blog.imaginea.com/modifying-query-parameters-nginx-in-reverse-proxy-mode/

And thanks for you all, too!

Edit

I tested this config and have 2 edits:

  1. If you to use un-standart port - you need write "proxy_set_header Host $http_host;" in location section;
  2. URL in attributes can be like "http://security-rest-api:9999/" and like "http%3A%2F%2Fsecurity-rest-api%3A9999%2F". You need to use both conditions for each type of attribute.

I corrected code with this edits

-- Piknik
Source: StackOverflow