Paul
Paul

Reputation: 2564

How to configure NGINX to serve files with custom extension with gzip compression?

I can enable nginx gzip compression for MIME types. I however need to serve large files which have a different extension (.roadData). A .roadData is essentially json.

How can I ask nginx to serve .roadData files with compression?

Thanks

Upvotes: 1

Views: 862

Answers (2)

Danila Vershinin
Danila Vershinin

Reputation: 9855

You can specify the MIME type for your custom extensions, like the following:

http {
    include /etc/nginx/mime.types;
    types {
        application/json roadData;
    }
}

Then all the .roadData files will be served with the appropriate MIME type, and NGINX will compress them if you have specified that type for compression.

In this way, you extend the server-wide MIME-types with your own, for another extension.

Alternatively, you can specify it as default for location, provided that roadData are stored in a single directory withou other files:

location /download/ {
    types        { }
    default_type application/json;
}

Like the other answer mentions, pre-compressed gzip files make sense there, because you don't want NGINX to recompress those large files on every request.

Use gzip_static on; and create a compressed version of the files.

One of the most efficient tools there would be zopfli.

Upvotes: 1

k010mb0
k010mb0

Reputation: 379

add mime type for your extension

types {
 application/json roadData;
}

enable compression for your mime type

gzip_static  on;
gzip  on;
gzip_types  application/json;

to offload nginx from dynamic compression create .gz (filename.roadData.gz) files next to original files so nginx will response with pre-compressed files.

Upvotes: 0

Related Questions