当前位置: 代码网 > 服务器>服务器>Nginx > nginx中gzip_types匹配content-type的方式

nginx中gzip_types匹配content-type的方式

2024年05月26日 Nginx 我要评论
1.背景我们系统中有一个功能,可以配置content-type类型来决定是否打开gzip压缩。这个配置与nginx官方的gzip_type不同的地方在于,nginx官方的是写死在配置文件中的,所有请求

1.背景

我们系统中有一个功能,可以配置content-type类型来决定是否打开gzip压缩。

这个配置与nginx官方的gzip_type不同的地方在于,nginx官方的是写死在配置文件中的,所有请求都生效;我们自研的是,不同用户的gzip_type可以不同。

最近发现一个问题

content-type配置为:image/jpeg,但是后端响应的content-type为"

image/jped;charset:utf-8"时,由于代码中是将配置的content-type与响应头中字符串作精确比较,因此,上述场景,并不能正确打开gzip功能。

nginx对此是如何处理的呢?

后端响应的content-type保持为image/jped;charset:utf-8。

1、配置gzip_type 如下,gzip生效:

gzip_type image/jpeg;

2、配置gzip_type如下,gzip不生效:(nginx官方文档中也没有提及下面的配置方法)

gzip_type "image/jpeg;charset:utf-8";

2.nginx处理流程

在进行header_filter时,对content-type做了校验:

static ngx_int_t
ngx_http_gzip_header_filter(ngx_http_request_t *r)
{
    ngx_table_elt_t       *h;
    ngx_http_gzip_ctx_t   *ctx;
    ngx_http_gzip_conf_t  *conf;

    conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);

    if (!conf->enable
        || (r->headers_out.status != ngx_http_ok
            && r->headers_out.status != ngx_http_forbidden
            && r->headers_out.status != ngx_http_not_found)
        || (r->headers_out.content_encoding
            && r->headers_out.content_encoding->value.len)
        || (r->headers_out.content_length_n != -1
            && r->headers_out.content_length_n < conf->min_length)
            // ngx_http_test_content_type中对content_type做了校验
        || ngx_http_test_content_type(r, &conf->types) == null
        || r->header_only)
    {
        return ngx_http_next_header_filter(r);
    }
    ...
    }

ngx_http_test_content_type定义如下:

void *
ngx_http_test_content_type(ngx_http_request_t *r, ngx_hash_t *types_hash)
{
    u_char      c, *lowcase;
    size_t      len;
    ngx_uint_t  i, hash;

    if (types_hash->size == 0) {
        return (void *) 4;
    }

    if (r->headers_out.content_type.len == 0) {
        return null;
    }

    len = r->headers_out.content_type_len;

    if (r->headers_out.content_type_lowcase == null) {

        lowcase = ngx_pnalloc(r->pool, len);
        if (lowcase == null) {
            return null;
        }

        r->headers_out.content_type_lowcase = lowcase;

        hash = 0;

        for (i = 0; i < len; i++) {
            c = ngx_tolower(r->headers_out.content_type.data[i]);
            hash = ngx_hash(hash, c);
            lowcase[i] = c;
        }

        r->headers_out.content_type_hash = hash;
    }

    return ngx_hash_find(types_hash, r->headers_out.content_type_hash,
                         r->headers_out.content_type_lowcase, len);
}

可以看出,将content-type头域内容转换为了小写,并使用了r->headers_out.content_type_len长度的内容,与配置的types进行比较。

但是针对第1种情况,配置和实际响应头明明是不相等的啊,是这么匹配成功的?

使用gdb打断点,发现

  • r->headers_out.content_type为:
  • {len = 24, data = “image/jpeg;charset=utf-8”}
  • 但是,
  • r->headers_out.content_type_len却是10!这个与上面为什么不一致呢?

找到设置content_type的地方:

static ngx_int_t
ngx_http_set_content_type_header(ngx_http_request_t *r,
    ngx_http_lua_header_val_t *hv, ngx_str_t *value)
{
    ngx_uint_t          i;

	// 此时,r->headers_out.content_type_len与 value->len 还是相等的
    r->headers_out.content_type_len = value->len;


#if 1

    for (i = 0; i < value->len; i++) {
        if (value->data[i] == ';') {
        	// 找到第一个分号,然后修改了r->headers_out.content_type_len
            r->headers_out.content_type_len = i;
            break;
        }
    }
#endif

    r->headers_out.content_type = *value;
    r->headers_out.content_type_hash = hv->hash;
    r->headers_out.content_type_lowcase = null;

    value->len = 0;

    return ngx_http_set_header_helper(r, hv, value, null, 1);
}

可以看出,nginx只使用了content-type响应头中第一个分号前的内容进行匹配。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com