大网数据广告活动
服务器运维问答

Algorithm behind nginx etag generation

· 52CE 我爱测

What is the algorithm used to generate etags in Nginx? They look something like "554b73dc-6f0d" now. Are they generated from timestamp only?

问题原文

原帖链接 · Vladislav Rastrusny · CC BY-SA 3.0

What is the algorithm used to generate etags in Nginx? They look something like "554b73dc-6f0d" now.

Are they generated from timestamp only?

已采纳解答

原帖链接 · Cameron Kerr · CC BY-SA 4.0

From the source code: http://lxr.nginx.org/ident?_i=ngx_http_set_etag

1803 ngx_int_t
1804 ngx_http_set_etag(ngx_http_request_t *r)
1805 {
1806     ngx_table_elt_t           *etag;
1807     ngx_http_core_loc_conf_t  *clcf;
1808 
1809     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1810 
1811     if (!clcf->etag) {
1812         return NGX_OK;
1813     }
1814 
1815     etag = ngx_list_push(&r->headers_out.headers);
1816     if (etag == NULL) {
1817         return NGX_ERROR;
1818     }
1819 
1820     etag->hash = 1;
1821     ngx_str_set(&etag->key, "ETag");
1822 
1823     etag->value.data = ngx_pnalloc(r->pool, NGX_OFF_T_LEN + NGX_TIME_T_LEN + 3);
1824     if (etag->value.data == NULL) {
1825         etag->hash = 0;
1826         return NGX_ERROR;
1827     }
1828 
1829     etag->value.len = ngx_sprintf(etag->value.data, "\"%xT-%xO\"",
1830                                   r->headers_out.last_modified_time,
1831                                   r->headers_out.content_length_n)
1832                       - etag->value.data;
1833 
1834     r->headers_out.etag = etag;
1835 
1836     return NGX_OK;
1837 }

You can see on lines 1830 and 1831 that the input is the last modified time and the content length.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-05-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。