nginx gzip duplicate MIME type “text/html”

Nginx GZIP配置
本文介绍了Nginx中GZIP压缩的相关配置参数及其作用,包括如何避免重复配置MIME类型导致的警告,并解决了IE6浏览器在启用GZIP后无法显示页面的问题。

指令

  • [#gzip gzip]
  • [#gzip_buffers gzip_buffers]
  • [#gzip_comp_level gzip_comp_level ]
  • [#gzip_min_length gzip_min_length]
  • [#gzip_http_version gzip_http_version]
  • [#gzip_proxied gzip_proxied]
  • [#gzip_types gzip_types]

 

把nginx升级到最新以后,发现用原来的配置启动的时候会提示:
Starting nginx: [warn]: duplicate MIME type “text/html” in /usr/local/nginx/conf/nginx.conf:23

解决办法:去掉下面一行中的“text/html”。

gzip_types  text/plain text/html  text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

原因:text/html根本就不需要写的,gzip默认就会压缩它的,只不过以前的nginx版本不提示这个警告而已,新版本的会出这个警告。

gzip on;
gzip_static on; # Only use this if you compiled it yourself -or- with this module
gzip_proxied any;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
gzip_http_version 1.1;
gzip_min_length 10;
gzip_comp_level 9;
# text/css application/x-javascript and text/javascript are really your choice...
gzip_types text/plain application/xhtml+xml text/xml application/xml application/xml+rss;
 

 

最近在开发一个项目,开发环境为 win + apache & linux + nginx,开发过程中在IE 6,7,8,ff下测试均正确无误,但在测试部的服务器下竟然所有带iframe的网页都无法显示,有点像404,但是很奇怪单独打开页面有时可以正常显 示有时不可以,而且在IE7,8和ff下不会出现这种,很郁闷..
         后来查看返回数据浏览器尽然接收到了数据但就是无法显示,仔细对比header头发现开发环境中没有使用动态gzip压缩,而测试环境中使用了动态 gzip压缩,后来打开开发环境nginx服务器的gzip压缩,重现了IE6无法显示页面的情况,在网上搜索了下出现这种情况大多数为引入js文件的时 候会出现,总结了下对于页面中引入外部资源的都有可能出现这种情况…严重鄙视IE…
Nginx下遇到IE6关闭gizp压缩只需要加: gzip_disable ”MSIE [1-6] \.”;

 

 

 

gzip

语法: gzip on|off

默认值: gzip off

作用域: http, server, location, if (x) location

开启或者关闭gzip模块

 

 

gzip_buffers

语法: gzip_buffers number size

默认值: gzip_buffers 4 4k/8k

作用域: http, server, location


设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。例如 4 4k 代表以4k为单位,按照原始数据大小以4k为单位的4倍申请内存。 4 8k 代表以8k为单位,按照原始数据大小以8k为单位的4倍申请内存。

如果没有设置,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。

 

 

gzip_comp_level

语法: gzip_comp_level 1..9

默认值: gzip_comp_level 1

作用域: http, server, location

gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu)。

 

gzip_min_length

语法: gzip_min_length length

默认值: gzip_min_length 0

作用域: http, server, location


设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。

默认值是0,不管页面多大都压缩。

建议设置成大于1k的字节数,小于1k可能会越压越大。即: gzip_min_length 1024

 

 

gzip_http_version

语法: gzip_http_version 1.0|1.1

默认值: gzip_http_version 1.1

作用域: http, server, location

识别http的协议版本。由于早期的一些浏览器或者http客户端,可能不支持gzip自解压,用户就会看到乱码,所以做一些判断还是有必要的。 注:21世纪都来了,现在除了类似于百度的蜘蛛之类的东西不支持自解压,99.99%的浏览器基本上都支持gzip解压了,所以可以不用设这个值,保持系 统默认即可。

 

 

gzip_proxied

语法: gzip_proxied [off|expired|no-cache|no-store|private|no_last_modified|no_etag|auth|any] ...

默认值: gzip_proxied off

作用域: http, server, location

Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。

  • off - 关闭所有的代理结果数据的压缩
  • expired - 启用压缩,如果header头中包含 "Expires" 头信息
  • no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
  • no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
  • private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
  • no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
  • no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
  • auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
  • any - 无条件启用压缩

 

 

gzip_types

语法: gzip_types mime-type [mime-type ...]

默认值: gzip_types text/html

作用域: http, server, location

匹配MIME类型进行压缩,(无论是否指定)"text/html"类型总是会被压缩的。


注意:如果作为http server来使用,主配置文件中要包含文件类型配置文件

http
{
 include       conf/mime.types;
 ......
}


如果你希望压缩常规的文件类型,可以写成这个样子

http 
{
: include       conf/mime.types;

: gzip on;
: gzip_min_length  1000;
: gzip_buffers     4 8k;   
: gzip_http_version 1.1; 
: gzip_types       application/x-javascript text/css application/xml;

: ...... 
}
user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log crit; pid /www/server/nginx/logs/nginx.pid; worker_rlimit_nofile 100000; stream { log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time'; access_log /www/wwwlogs/tcp-access.log tcp_format; error_log /www/wwwlogs/tcp-error.log; include /www/server/panel/vhost/nginx/tcp/*.conf; } events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; types { application/font-sfnt ttf; font/truetype ttf; font/opentype otf; application/font-woff woff; application/font-woff2 woff2; } default_type application/font-sfnt; #include luawaf.conf; include proxy.conf; default_type application/octet-stream; server_names_hash_bucket_size 512; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn_zone $server_name zone=perserver:10m; server_tokens off; access_log off; server { listen 888; server_name phpmyadmin; index index.html index.htm index.php; root /www/server/phpmyadmin; location ~ /tmp/ { return 403; } #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } # 修正后的字体配置块 location ~* \.(ttf|otf|woff2?)$ { # 在开头添加 access_log /www/wwwlogs/font_access.log; error_log /www/wwwlogs/font_error.log; # ... 其他配置 ... root /www/wwwroot/yinzhang.jinshelby.com; rewrite ^/xcx/font/(.*)$ /public/xcx/font/$ 1 break; # 关键 MIME 类型 types {} default_type application/font-sfnt; # CORS 头 add_header Access-Control-Allow-Origin * always; add_header Access-Control-Allow-Methods 'GET, OPTIONS' always; add_header Access-Control-Allow-Headers 'Range' always; add_header Access-Control-Expose-Headers 'Content-Length,Content-Range' always; # 缓存 expires 1y; add_header Cache-Control "public, max-age=31536000, immutable"; # 字节范围支持(安卓必需) add_header Accept-Ranges bytes; if ($ request_method = 'OPTIONS') { return 204; } } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log /www/wwwlogs/access.log; } include /www/server/panel/vhost/nginx/*.conf; } 现在完整配置按照你说的是这样的 , 但是保存报错: nginx version: nginx/1.20.2 nginx: [warn] duplicate extension "ttf", content type: "font/truetype", previous content type: "application/font-sfnt" in /www/server/nginx/conf/nginx.conf:27 nginx: [warn] duplicate extension "woff", content type: "application/font-woff", previous content type: "font/woff" in /www/server/nginx/conf/nginx.conf:29 nginx: [warn] duplicate extension "woff2", content type: "application/font-woff2", previous content type: "font/woff2" in /www/server/nginx/conf/nginx.conf:30 nginx: [emerg] "default_type" directive is duplicate in /www/server/nginx/conf/nginx.conf:37 nginx: configuration file /www/server/nginx/conf/nginx.conf test failed
07-05
user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log crit; pid /www/server/nginx/logs/nginx.pid; worker_rlimit_nofile 100000; stream { log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time'; access_log /www/wwwlogs/tcp-access.log tcp_format; error_log /www/wwwlogs/tcp-error.log; include /www/server/panel/vhost/nginx/tcp/*.conf; } events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; # 修正 MIME 类型定义(移除重复项) types { font/truetype ttf; font/opentype otf; font/woff woff; font/woff2 woff2; } # 移除重复的 default_type # default_type application/font-sfnt; // 注释掉这行 default_type application/octet-stream; #include luawaf.conf; include proxy.conf; default_type application/octet-stream; server_names_hash_bucket_size 512; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn_zone $server_name zone=perserver:10m; server_tokens off; access_log off; server { listen 888; server_name phpmyadmin; index index.html index.htm index.php; root /www/server/phpmyadmin; location ~ /tmp/ { return 403; } #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } # 修正后的字体配置块 location ~* \.(ttf|otf|woff2?)$ { access_log /www/wwwlogs/font_access.log; error_log /www/wwwlogs/font_error.log; root /www/wwwroot/yinzhang.jinshelby.com; rewrite ^/xcx/font/(.*)$ /public/xcx/font/$ 1 break; # 修正:$ 1 # CORS 头 add_header Access-Control-Allow-Origin * always; add_header Access-Control-Allow-Methods 'GET, OPTIONS' always; add_header Access-Control-Allow-Headers 'Range' always; add_header Access-Control-Expose-Headers 'Content-Length,Content-Range' always; # 缓存 expires 1y; add_header Cache-Control "public, max-age=31536000, immutable"; # 字节范围支持(安卓必需) add_header Accept-Ranges bytes; if ($ request_method = 'OPTIONS') { # 修正:$ request_method return 204; } } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log /www/wwwlogs/access.log; } include /www/server/panel/vhost/nginx/*.conf; } 还是报错 : ginx version: nginx/1.20.2 nginx: [warn] duplicate extension "woff", content type: "font/woff", previous content type: "font/woff" in /www/server/nginx/conf/nginx.conf:29 nginx: [warn] duplicate extension "woff2", content type: "font/woff2", previous content type: "font/woff2" in /www/server/nginx/conf/nginx.conf:30 nginx: [emerg] "default_type" directive is duplicate in /www/server/nginx/conf/nginx.conf:41 nginx: configuration file /www/server/nginx/conf/nginx.conf test failed
07-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值