Kasheftin
Kasheftin

Reputation: 9473

Force nginx to send specific Content-Type

How to overwrite default Content-Type in nginx? Currently when I request 01.dae file, there's

Content-Type: application/octet-stream;

And I want it to be

Content-Type: application/xml;

I tried something like

location ~* \.dae$ {
  types { };
  default_type application/xml;
}

and

location ~* \.dae$ {
  add_header Content-Type application/xml;
}

but nothing works.

Upvotes: 86

Views: 154887

Answers (4)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42849

You can edit /etc/nginx/mime.types and add it

types {
    application/xml        dae;
}

I haven't found the the exact string application/xml in my mime.types so I suppose you can directly include it inside your server block, in the server scope or something.

If you do not have access to the system mime.types then you can set it in the location block if you have access to that. https://nginx.org/en/docs/http/ngx_http_core_module.html#types

WARNING When you set types it will overwrite all mime types set in /etc/nginx/mime.types. To avoid this target specific extensions with a Regular expression location block. Also know that locations can be nested, like so:

server {
    # ...

    location / {
        root   /usr/share/nginx/html;
        index  index.html;

        location ~* \.mjs$ {# target only *.mjs files
            # now we can safely override types since we are only
            # targeting a single file extension.
            types {
                text/javascript mjs;
            }
        }
    }
}

Upvotes: 49

Vladimir
Vladimir

Reputation: 507

location ~* \.dae$ {
    types { } default_type "application/xml; charset=utf-8";
}

Upvotes: 6

Oleg Neumyvakin
Oleg Neumyvakin

Reputation: 10292

In case you have no file extension:

location ~ something {
   default_type application/xml;
}

Nginx docs for default_type

In case you are setting up let's encrypt certificate with a client which creates http server: How to use golang lego let's encrypt client behind nginx?

Upvotes: 102

guo simon
guo simon

Reputation: 1

Add "the application/xml dae;" to your server scope or location scope. Or add it into the mime.type.

Both of them work for me.

Upvotes: -1

Related Questions