Change content-type with proxy_pass

Our object storage set Content-Type: application/octet-stream to images which leads browsers to download them. We need to let browsers preview them. So we use nginx like this:

location /os/ {
  proxy_pass http://os.company.com/;
  proxy_hide_header Content-Type;
}

It will remove Content-Type: application/octet-stream header. But how could we set the default content type such as image/png or image/jpg to the response?

1 Like

add_header perhaps?

add_header {according to the file name extension}, how to fill in the blank?

Perhaps using multiple location blocks? E.g.


location ~ .+\.png$ {
    ...
    add_header Content-Type image/png;
    # Or maybe this would also work
    # default_type image/png;
}

There may be a better way if you’d need a lot of these…

What if I’d like to support all the mime types which were supported by nginx by default?

So the question is really

“How to make nginx override the Content-Type header?”

Can do it with maps and you still need to fill out a map table for all the types you want to support, but…

http {
    ...
    map $uri $fix_content_type {
        default application/octet-stream;
        ~(.*.png)$ image/png;
        ~(.*.jpg)$ image/jpeg;
    }

    server {
        ...
        location /os/ {
            proxy_pass http://os.company.com/;
            proxy_hide_header Content-Type;
            add_header Content-Type $fix_content_type;
        }
    }
}
2 Likes

This would be a solution. Thank you.

Is there any way to use the content of the default mime type file?

1 Like

I don’t see an obvious way as the format is different.

You can include a file in the map, so you could write a script to convert the mime.types into the right format and include that…

1 Like

Thank you for answering the question. I’ll take the map way as the solution. But I suggest that it should be an obvious way to re-apply the default behavior. And thanks again for the great software.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.