Opened 7 years ago

Last modified 7 years ago

#1369 new enhancement

Add proxy_detect_mime setting

Reported by: https://stackoverflow.com/users/1100117/higuita Owned by:
Priority: minor Milestone:
Component: nginx-core Version: 1.13.x
Keywords: Cc:
uname -a: Linux serverF04 4.9.0-0.bpo.2-amd64 #1 SMP Debian 4.9.18-1~bpo8+1 (2017-04-10) x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.10.3
built with OpenSSL 1.0.2k 26 Jan 2017
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_flv_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/headers-more-nginx-module --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nginx-auth-pam --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nginx-cache-purge --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nginx-dav-ext-module --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nginx-development-kit --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nginx-echo --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/ngx-fancyindex --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nchan --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nginx-lua --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nginx-upload-progress --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/nginx-upstream-fair --add-dynamic-module=/build/nginx-4hGAP1/nginx-1.10.3/debian/modules/ngx_http_substitutions_filter_module

Description

Right now, nginx using proxy_pass will not touch the content-type and it is the upstream responsibility to sent the correct content-type.

The problem is that not always we control the upstream (external apps), have very long deployment cycles to get the proper fix in the upstream (complex upstream apps, external teams, etc) , or the upstream already have LOTs of old data that can't be easily fixed (huge S3 bucket, old DB without proper type column)

A workaround could be simply do a "proxy_hide_header Content-Type;" and then add a "proxy_set_header content-type $mime" or "default_type $mime;", with a map like

map $request_uri $mime {

default application/octet-stream;
~*\.jpe?g image/jpeg;
~*\.gif image/gif;
~*\.png image/png;

}

i didn't test this solution, as it feels dirty and totally cloning the nginx mime-type detection in maps regex ... so why not expand it a little to cover this also!?

Adding a new proxy_detect_mime (off by default) would do that automatically, drop any upstream content-type header, and apply the mime detection to the uri

Most people will not use it, but might be a life saver for people that needs it

Change History (1)

comment:1 by https://stackoverflow.com/users/1100117/higuita, 7 years ago

Please replace the s/proxy_set_header/add_header/ above, as proxy_set_header is to send to the upsteam and we want to fix the content-type sent for the client

Note: See TracTickets for help on using tickets.