skip to Main Content

I am trying to rewrite custom header information like "Author" (not part of the URL) using nginx reverse proxy.
The header information "Author:" should be rewritten from "test123" to e.g. "BASIC"

command:

admin1@nginx1:~$ curl -x 192.168.175.134:80 http://home1.MyWeb.eu:8081/home1/index.html?t=1 -H "Author: test123" -vk

TCPdump on apache:

--
GET /home1/index.html?t=1 HTTP/1.0
Host: home1.MyWeb.eu
Connection: close
User-Agent: curl/7.58.0
Accept: */*
Proxy-Connection: Keep-Alive
Author: test123

wanted result:

--
GET /home1/index.html?t=1 HTTP/1.0
Host: home1.MyWeb.eu
Connection: close
User-Agent: curl/7.58.0
Accept: */*
Proxy-Connection: Keep-Alive
Author: BASIC

3

Answers


  1. Chosen as BEST ANSWER

    Hi I have solved the issue like this:

    curl -x localhost:80 https://www.dummy.com -H "Authorization: test12" -vk
    

    NGINX configuration:

    server {
        listen 127.0.0.2:443 ssl;
            # the included file below contains ssl certificates
        include snippets/www.dummy.com.conf;
        root /var/www/html;
    
    set $MyAuthorization 'Basic bGaa9zX25ljYhhWxlcl9=';
    
        location / {
            proxy_pass https://www;
                    proxy_set_header Host www.dummy.com;
    
          proxy_set_header Authorization $MyAuthorization;
        
        }
    }
    

  2. You can use the proxy_set_header in your configuration. I.e.:

    proxy_set_header            Author        "BASIC";
    
    Login or Signup to reply.
  3. I made with setting variables. A little ugly but seems to work.

            location / {
                <...>
    
                set $rewritten_header $http_myheader;
                if ($http_myheader = "something") {
                  set $rewritten_header somethingelse;
                }
                proxy_set_header Myheader $rewritten_header;
            }
    

    The above will rewrite your header only if the condition match. Otherwise keep original value.

    I think more elegant to use map in case you have a large mapping.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search