Muhammad Rehan Saeed
Muhammad Rehan Saeed

Reputation: 38487

Sharing MIME type list using GZIP and BROTLI in NGINX

I want to enable GZIP and Brotli compression using NGINX. I have to supply each with their own list of MIME types like so in my nginx.conf:

gzip_types   text/plain
             text/css
             ...etc;

brotli_types text/plain
             text/css
             ...etc;

How can I create a single list of MIME types that can be used by both settings?

Upvotes: 7

Views: 985

Answers (1)

Danila Vershinin
Danila Vershinin

Reputation: 9875

It is pretty much a one off task to set the two lists in sync since the number of MIME types that could benefit from compression floats around 20.

If it's absolutely desired to manage the list from a central location, I would suggest looking into developing an Ansible playbook to push out Nginx configuration to server.

Part of the Ansible playbook relevant for pushing the corresponding configuration would look like this:

- name: "Set fact for compressible MIME types"
  set_fact:
    compressibles:
      - "text/css"
      - "application/javascript"
      - "..."

- name: "copy {{ item }} conf.d config file"
  template: 
    src: "{{ item }}.conf.j2" 
    dest: "/etc/nginx/conf.d/{{ item }}.conf"
  with_items: 
    - brotli
    - gzip 
  notify: reload nginx

gzip.conf.j2:

gzip on;

gzip_types {{ compressibles|join(' ') }};

# whatever else you think is relevant for gzip configuration
# ...

brotli.conf.j2

brotli on;

brotli_types {{ compressibles|join(' ') }};

# whatever else you think is relevant for brotli configuration
# ...

Upvotes: 0

Related Questions