Override Content-Type header with proxied requests

Hi !!

i have a Nginx server that operates as a reverse proxy to a my bucket in
Amazon S3.

Amazon S3 service could deliver contents with wrong Content-Type header,
so i would like to override this header by referring to file extension.

In other servers i have just configured the “types” block with all mime
types mapped with file estensions,
but this approach only works when Nginx delivers contents directly (as a
origin server).
If the server is a reverse proxy, doesn’t add a new Content-Type header,
but
honors Content-Type (if exists) received by the origin.

Is it possible to override the content-type response header using
“types”
block? Is there any best practice to override content-type header by
file
extensions? Is “map” suggested for this purpose or using multiple
“location”
block is better?

Thanks in advance!!

Andrea

Posted at Nginx Forum:

Here’s a thread I started on this a while back. I didn’t get any
replies.

http://mailman.nginx.org/pipermail/nginx/2011-September/029344.html

Jonathan

Jonathan M. // Oxford, London, UK
http://www.jpluscplusm.com/contact.html

Try this.

At the http level define a map directive that maps upstream
Content-Types
to the correct ones.

map $upstream_content_type $s3_content_type {
# S3 -> real…
}

On the location that proxy passes.

proxy_hide_header Content-Type;
add_header Content-Type $s3_content_type;

----appa

Oops it’s $upstream_http_content_type instead. Should read:

map $upstream_http_content_type $s3_content_type {
# S3 -> real…
}

----appa

Thanks for the reply!!

This approach can be a good solution.
I wonder if this can affect the server perfomance.

Another solution could be to create a location for each file extension
that
only adds the correct Content-Type header.
This is certainly less maintainable than a map, but is it better for the
perfomance ?

Does not exist any native Nginx directive (or in a third party module)
that
permits Content-Type overriding (using Nginx mime.types “types” file)
for
proxied requests ?

Thanks again!

Andrea

Posted at Nginx Forum:

Hello Appa,

I am having the same issue. I want NGINX to serve all the json file from
upstream as a content type of application/json.I tried with map just
like
you suggested but I am not able to figure out what should be inside
map(That
you have written below), Can you please suggest?

map $upstream_http_content_type $s3_content_type {

S3 → real…

}

location ~ .json$ {
proxy_hide_header Content-Type;
add_header Content-Type $s3_content_type;
}

–Manish

Posted at Nginx Forum: