How to edit the request header in nginx and then send to backend?

686 views Asked by At

now nginx has a list headers_in in ngx_http_request_t, my requirement to is do some change for example encoding to one of the header, I need do this in source code, it seemes a little like the proxy_set_header command in configuration. Directly set value in the table_elt_t seems rude, Does any one has any idea?

1

There are 1 answers

0
Quinn Comendant On

You can modifying the request header by adding a new header to the upstream request and including string captures. Here's a simple example that replaces Mozilla/5.0 with Mozilla/6.0 in the User-Agent header:

set $ua $http_user_agent;
if ($http_user_agent ~ "^Mozilla/5.0 (.+)$") {
    set $ua "Mozilla/6.0 $1";
}
proxy_set_header User-Agent $us;

Although, it sounds like you also want to apply some programatic transformation to your replaced header (you mention "encoding"), which I don't think is possible. There might be a different solution if you explain your problem in more detail with examples.