Nginx常用模块学习---定制HTTP头信息定制ngx_http_headers_module设置响应头

Nginx常用模块学习—定制HTTP头信息定制ngx_http_headers_module设置响应头

前言

HTTP头信息,是实际业务中一个很重要的功能。例如,如果需要将请求结果在浏览器上缓存一段时间,或者在请求代理到后端服务器的过程中生成一个唯一的 ID进行识别。通过对 Nginx进行配置,可以轻松实现这些功能。
ngx_http_headers_module是在 Nginx编译时默认自带的模块,主要包含 add_header和 expires两个指令。

使用语法

expires

expires语法: expires [modified] time; expires epoch | max | off;
默认值: expires off;
环境: http、 server、 location、 if in location
用途:设置 Expires和 Cache-Control响应头字段,主要作用是控制缓存时间,如在浏览器上的缓存时间、 CDN的缓存时间。参数值可以是正数、负数或零;

示例如下:

[root@ecs-21d5 nginx]# grep expires  conf/nginx.conf 
    expires 	  -1;   	#输出响应头是cache-control:	no-cache
[root@ecs-21d5 nginx]# curl http://127.0.0.1:80/ -I
HTTP/1.1 200 OK
Server: nginx/1.17.4
Date: Wed, 23 Oct 2019 15:05:34 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 23 Oct 2019 14:56:26 GMT
Connection: keep-alive
ETag: "5db06a1a-264"
Expires: Wed, 23 Oct 2019 15:05:33 GMT
Cache-Control: no-cache
Accept-Ranges: bytes

如果要求在浏览器第一次访问后,数据在浏览器上缓存 1h,则配置如下:

[root@ecs-21d5 nginx]# grep -A 1 expires  conf/nginx.conf 
    expires 	  1h;   	# 输出响应头是cache-control: max-age=3600
				            # 表示缓存1h,max-age 的单位是秒
[root@ecs-21d5 nginx]# ./sbin/nginx -s stop
[root@ecs-21d5 nginx]# ./sbin/nginx
[root@ecs-21d5 nginx]# curl http://127.0.0.1:80/ -I
HTTP/1.1 200 OK
Server: nginx/1.17.4
Date: Wed, 23 Oct 2019 15:12:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 23 Oct 2019 14:56:26 GMT
Connection: keep-alive
ETag: "5db06a1a-264"
Expires: Wed, 23 Oct 2019 16:12:29 GMT
Cache-Control: max-age=3600
Accept-Ranges: bytes

[root@ecs-21d5 nginx]# 


此配置只能在 HTTP状态码是 200、 201、 204、 206、 301、 302、 303、 304、 307或 308时才会生效。

即只有当请求处于正常的返回状态时,才会发送缓存头,毕竟在浏览器上缓存一个错误状态不是什么好事情。

在 Nginx 1.7.9版本之后, expires指令可以使用变量来配置响应头,并根据响应头的 Content-Type来定义缓存时间,也就是可以根据不同的条件动态地调整缓存时间。

例如,如果 Content-Type是 application/ pdf,则添加 cache-control: max-age = 3600响应头信息;如果 Content-Type是 image/,则添加 ache-control: max-age = 36000响应头信息;如果没有匹配到对应的 Content-Type,则执行 default的配置 off,即不进行缓存,代码如下:

[root@ecs-21d5 nginx]# grep expires -A 4 conf/nginx.conf
    map $sent_http_content_type $expires{
		default			off;
		application/pdf	1h;
		~image			10h; 
    }
    expires 	  $expires;
    
    include       mime.types;
    default_type  application/octet-stream;

[root@ecs-21d5 nginx]# 


在上述配置中, map指令会根据响应头 Content_ Type的值对 $expires进行赋值(通过 $ sent_ http_ content_ type获取 Content-Type的值)。

add_ header

语法: add_ header name value [always];
默认值:无
环境: http、 server、 location、 if in location
用途:添加自定义的响应头。

例如,可以用来添加 Cache-Control响应头,以达到 expires指令的效果,不过它不如 expires那样简洁、明了,

示例如下:

[root@ecs-21d5 nginx]# grep add_header conf/nginx.conf
	add_header	Cachae-Control no-cache;		# 等同于 expires -1;
[root@ecs-21d5 nginx]# sbin/nginx -s stop
[root@ecs-21d5 nginx]# sbin/nginx
[root@ecs-21d5 nginx]# 
[root@ecs-21d5 nginx]# 
[root@ecs-21d5 nginx]# curl http://127.0.0.1:80/ -I
HTTP/1.1 200 OK
Server: nginx/1.17.4
Date: Wed, 23 Oct 2019 15:44:52 GMT
Content-Type: text/html
Content-Length: 618
Last-Modified: Wed, 23 Oct 2019 15:26:29 GMT
Connection: keep-alive
ETag: "5db07125-26a"
Cachae-Control: no-cache
Accept-Ranges: bytes

[root@ecs-21d5 nginx]# 
[root@ecs-21d5 nginx]# curl http://127.0.0.1:80/aaa.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.17.4
Date: Wed, 23 Oct 2019 15:46:04 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

[root@ecs-21d5 nginx]# 


在默认情况下, add_ header只能在 HTTP状态码是 200、 201、 204、 206、 301、 302、 303、 304、 307或 308时输出响应头,如果出现 404、 500等异常状态码则无法输出响应头。

但 Nginx 1. 7. 5以上的版本新增了 always参数,使之可以在任何 HTTP状态下输出响应头,示例如下:响应头信息输出如下:测试结果表明,即使发生了 500错误,加入 always参数后依然可以输出 add_header响应头信息。

[root@ecs-21d5 nginx]# vim conf/nginx.conf
[root@ecs-21d5 nginx]# grep add_header conf/nginx.conf
	add_header	Cachae-Control no-cache always;		# 等同于 expires -1;
[root@ecs-21d5 nginx]# sbin/nginx -s reload
[root@ecs-21d5 nginx]# curl http://127.0.0.1:80/a.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.17.4
Date: Wed, 23 Oct 2019 15:48:42 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
Cachae-Control: no-cache

[root@ecs-21d5 nginx]# curl http://127.0.0.1:80/ -I
HTTP/1.1 200 OK
Server: nginx/1.17.4
Date: Wed, 23 Oct 2019 15:48:50 GMT
Content-Type: text/html
Content-Length: 618
Last-Modified: Wed, 23 Oct 2019 15:26:29 GMT
Connection: keep-alive
ETag: "5db07125-26a"
Cachae-Control: no-cache
Accept-Ranges: bytes

[root@ecs-21d5 nginx]#

实战经验

添加请求头有很多需要注意的地方,下面是在长期使用中总结的一些经验,供参考。

  • 关于指令 expires 1h

expires 1h表示在浏览器上的缓存时间是 1h,但如果缺少 Last-Modified响应头(文末外链说明详解),大部分浏览器不会使用缓存。在业务代码生成过程中,部分代码可能没有使用规范的框架输出数据,因此可能会缺少 Last-Modified信息,无法在浏览器上进行缓存,但请求仍会回源到服务器上,导致带宽压力增大。因此负责网络带宽和用户体验的技术人员要对使用规范进行说明和管理,避免出现缓存效果不理想的情况。

  • 关于指令 add_ header

如果使用 always参数,那么即使在业务出现异常时,也会输出 add_ header响应头信息,因此要慎重使用此参数。另外,如果后端服务器在返回响应体时返回了一个和 add_ header相同的响应头,则会导致两个响应头重复输出到浏览器上,从而引起不必要的 Bug,示例如下:

Last-Modified参考外链:

http://harttle.land/2017/04/04/using-http-cache.html

http://blog.csdn.net/eroswang/article/details/8302191

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章