RameshVel
RameshVel

Reputation: 65877

Nginx string manipulation on a variable

Is there a way i can apply string replace on a nginx variable. I am using nginx as a proxy for s3 restricted downloads. And i am forwarding the $upstream_http_etag to the response header with the different name.

add_header Content-MD5 $upstream_http_etag;
proxy_set_header Content-MD5 $upstream_http_etag;

The problem is etag is double quoted. I wanted to remove this double quotes before adding it to the header. Is there a possible way to do this.

I know i can strip it down on the client. But this is for older apps to work without the updates.

Any help is much appreciated.

Upvotes: 4

Views: 3878

Answers (1)

xiaochen
xiaochen

Reputation: 1305

One possible solution is use header_filter_by_lua directive of lua-nginx-module instead of add_header directive.

For example,

location / {
    header_filter_by_lua '
            -- delete double quoted
            local value = string.gsub(ngx.var.upstream_http_etag or "", [[^"(.+)"$]], "%1")
            -- add response header
            ngx.header["Content-MD5"] = value
    ';
    proxy_pass ...;
}

BTW, proxy_set_header is used to pass extra request header to upstream, however, $upstream_http_etag is upstream response header.
So proxy_set_header Content-MD5 $upstream_http_etag; makes no sense.

Upvotes: 1

Related Questions