编者按
有很多客户问我们NGINX商业版与开源版本到底有什么区别,在官方网站有一个对比列表,但看完之后感觉好像还是没有get到其增强的精髓。为了更好的让大家了解NGINX Plus增强的能力及其应用场景,特编发《NGINX Plus增强能力系列专题》。本系列一共包含10篇内容,为了精简,我们没有包含那些显而易见的增强例如dashboard,而是更聚焦商业版本的关键能力与场景。本专题内容由数位专家参与。
NGINX Plus增强能力系列专题目录:
- 视频直播HLS与RTMP
- 代理缓存状态统计与清理
- 动态KV模块
- NGINX实现动态黑白名单访问控制
- 主动健康检查
- NGINX集群
- JWT认证
- API接口
- 会话级log记录
- http_upstream与stream_upstream
下面我们就进入《NGINX Plus增强能力系列专题》的第二篇“代理缓存状态统计与清理”,作者路瑞强。
路瑞强
F5 应用交付解决方案专家
多年的IT从业经验,其中有超过6年的软件研发经验,10年以上应用交付系统架构及解决方案经验。参与过大量用户的云计算、应用交付、应用优化、广域网加速、企业安全出口等领域在最终用户端的设计及实施工作。2013年2月加入F5,主要侧重方向为云计算、应用高可用、应用安全等。历次负责过区域客户、大企业客户、大型银行业务,最近5年负责大型银行客户的云计算及应用交付的设计与实现。
代理缓存状态统计与清理
1. 什么是反向代理?
反向代理功能,是客户端无感知的代理功能。也就是说客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。
反向代理的好处有:
1、 解决了网站服务器对外可见的问题;
2、节约了有限的IP地址资源,企业内所有的网站共享一个在internet中注册的IP地址,这些服务器分配私有地址,采用虚拟主机的方式对外提供服务;
3、 保护了真实的web服务器,web服务器对外不可见,外网只能看到反向代理服务器,而反向代理服务器上并没有真实数据,因此,保证了web服务器的资源安全;
4、 加速了对网站访问速度,减轻web服务器的负担,反向代理具有缓存网页的功能,如果用户需要的内容在缓存中,则可以直接从代理服务其中获取,减轻了web服务器的负荷,同时也加快了用户的访问速度。
2. Nginx OSS反向代理能做什么?
Nginx反向代理可以支持udp、tcp、http协议,详细支持列表请参考:http://nginx.org/en/docs/,在此篇文章里我们将主要从web server角度来看http、fastcgi、scgi、uwsgi的代理进行分析。
这四类通过以下四个模块来实现:
ngx_http_fastcgi_module
ngx_http_proxy_module
ngx_http_scgi_module
ngx_http_uwsgi_module
这四种代理的功能都包括了:构请求内容、建立连接并发送请求、接收上游响应、转发响应、SSL、缓存类指令、独有配置。
http反向代理 | fastcgi反向代理 | scgi反向代理 | uwsgi反向代理 | ||
构造请求内容 | 指定服务器 | proxy_pass | fastcgi_pass | scgi_pass | uwsgi_pass |
传递请求头部 | proxy_pass_request_headers | fastcgi_pass_request_headers | scgi_pass_request_headers | uwsgi_pass_request_headers | |
N+增强能力系列(2) | 代理缓存状态统计与清理 传递请求包体 | proxy_pass_request_body | fastcgi_pass_request_body | scgi_pass_request_body | uwsgi_pass_request_body | |
指定请求方法 | proxy_method | ||||
指定请求协议 | proxy_http_version | ||||
增或改请求头部 | proxy_set_header | ||||
设置请求包体 | proxy_set_body | ||||
缓存请求包体 | proxy_request_buffering | fastcgi_request_buffering | scgi_request_buffering | uwsgi_request_buffering | |
建立连接及发送请求 | 连接服务器超时时间 | proxy_connect_timeout | fastcgi_connect_timeout | scgi_connect_timeout | uwsgi_connect_timeout |
连接绑定地址 | proxy_bind | fastcgi_bind | scgi_bind | uwsgi_bind | |
使用TCP keepalive | proxy_socket_keepalive | fastcgi_socket_keepalive | scgi_socket_keepalive | uwsgi_socket_keepalive | |
忽略客户端关连接 | proxy_ignore_client_adourt | fastcgi_ignore_client_adourt | scgi_ignore_client_adourt | uwsgi_ignore_client_adourt | |
设置HTTP头部用到的哈希表 | proxy_headers_hash_bucket_size | ||||
设置HTTP头部用到的哈希表 | proxy_headers_hash_max_size | ||||
发送请求超时时间 | proxy_send_timeout | fastcgi_send_timeout | scgi_send_timeout | uwsgi_send_timeout | |
接收服务器响应 | 是否缓存服务器响应 | proxy_buffering | fastcgi_buffering | scgi_buffering | uwsgi_buffering |
存放服务器响应的目录 | proxy_temp_path | fastcgi_temp_path | scgi_temp_path | uwsgi_temp_path | |
写文件缓存大小 | proxy_temp_file_write_size | fastcgi_temp_file_write_size | scgi_temp_file_write_size | uwsgi_temp_file_write_size | |
临时文件最大大小 | proxy_max_temp_file_size | fastcgi_max_temp_file_size | scgi_max_temp_file_size | uwsgi_max_temp_file_size | |
接收响应头部缓存 | proxy_buffer_size | fastcgi_buffer_size | scgi_buffer_size | uwsgi_buffer_size | |
接收响应包体缓存 | proxy_buffers | fastcgi_buffers | scgi_buffers | uwsig_buffers | |
缓存完成前转发包体 | proxy_busy_buffers_size | fastcgi_busy_buffers_size | scgi_busy_buffers_size | uwsgi_busy_buffers_size | |
持久化包体文件 | proxy_store | fastcgi_store | scgi_store | uwsgi_store | |
设定包体文件权限 | proxy_store_access | fastcgi_store_access | scgi_store_access | uwsgi_store_access | |
读取响应超时时间 | proxy_read_timeout | fastcgi_read_timeout | scgi_read_timeout | uwsgi_read_timeout | |
读取响应限速 | proxy_limit_rate | fastcgi_limit_rate | scgi_limit_rate | uwsgi_limit_rate | |
转发响应 | 隐藏发向客户端的响应头部 | proxy_hide_header | fastcgi_hide_header | scgi_hide_header | uwsgi_hide_header |
禁用响应头部功能 | proxy_ignore_headers | fastcgi_ignore_headers | scgi_ignore_headers | uwsgi_ignore_headers | |
替换Set-Cookie中的域名 | proxy_cookie_domain | ||||
替换Set-Cookie中的URL | proxy_cookie_path | ||||
修改重定向响应中Location的值 | proxy_redirect | ||||
传递包头到客户端 | proxy_pass_header | fastcgi_pass_header | scgi_pass_header | uwsgi_pass_header | |
出错时更换服务器 | proxy_next_upstream | fastcgi_next_upstream | scgi_next_upstream | uwsgi_next_upstream | |
更换服务器超时 | proxy_next_upstream_timeout | fastcgi_next_upstream_timeout | scgi_next_upstream_timeout | uwsgi_next_upstream_timeout | |
更换服务器重试次数 | proxy_next_upstream_tries | fastcgi_next_upstream_tries | scgi_next_upstream_tries | uwsgi_next_upstream_tries | |
拦截服务器错误响应 | proxy_next_intercept_errors | fastcgi_next_intercept_errors | scgi_next_intercept_errors | uwsgi_next_intercept_errors | |
SSL | 配置证书 | proxy_ssl_certificate | uwsgi_ssl_certificate | ||
配置私钥 | proxy_ssl_certificate_key | uwsgi_ssl_certificate_key | |||
指定安全套件 | proxy_ssl_ciphers | uwsgi_ssl_ciphers | |||
指定吊销列表CRL文件 | proxy_ssl_crl | uwsgi_ssl_crl | |||
指定证书中的域名 | proxy_ssl_name | uwsgi_ssl_name | |||
指定私钥密码文件 | proxy_ssl_password_file | uwsgi_ssl_password_file | |||
指定SSL协议版本 | proxy_ssl_protocols | uwsgi_ssl_protocols | |||
传递SNI信息 | proxy_ssl_server_name | uwsgi_ssl_server_name | |||
是否重用SSL连接 | proxy_ssl_session_reuse | uwsgi_ssl_session_reuse | |||
设置可信任证书 | proxy_ssl_trusted_certificate | uwsgi_ssl_trusted_certificate | |||
是否验证服务器证书 | proxy_ssl_verify | uwsgi_ssl_verify | |||
设置验证证书链的深度 | proxy_ssl_verify_depth | uwsgi_ssl_verify_depth | |||
缓存 | 指定共享内存 | proxy_cache | fastcgi_cache | scgi_cache | uwsgi_cache |
缓存文件存放位置 | proxy_cache_path | fastcgi_cache_path | scgi_cache_path | uwsgi_cache_path | |
指定不缓存的请求 | proxy_cache_bypass | fastcgi_cache_bypass | scgi_cache_bypass | uwsgi_cache_bypass | |
开启子请求更新过期缓存 | proxy_cache_background_update | fastcgi_cache_background_update | scgi_cache_background_update | uwsgi_cache_background_update | |
定义缓存关键字 | proxy_cache_key | fastcgi_cache_key | scgi_cache_key | uwsgi_cache_key | |
使用range协议的偏移 | proxy_cache_max_range_offset | fastcgi_cache_max_range_offset | scgi_cache_max_range_offset | uwsgi_cache_max_range_offset | |
缓存的请求方法 | proxy_cache_methods | fastcgi_cache_methods | scgi_cache_methods | uwsgi_cache_methods | |
多少请求后再缓存 | proxy_cache_min_uses | fastcgi_cache_min_uses | scgi_cache_min_uses | uwsgi_cache_min_uses | |
缓存的响应及时间 | proxy_cache_valid | fastcgi_cache_valid | scgi_cache_valid | uwsgi_cache_valid | |
强制使用range协议 | proxy_force_ranges | fastcgi_force_ranges | scgi_force_ranges | uwsgi_force_ranges | |
有过期内容使用304 | proxy_cache_revalidate | fastcgi_cache_revalidate | scgi_cache_revalidate | uwsgi_cache_revalidate | |
返回过期的缓存内容 | proxy_cache_use_stale | fastcgi_cache_use_stale | scgi_cache_use_stale | uwsgi_cache_use_stale | |
指定不缓存的响应 | proxy_no_cache | fastcgi_no_cache | scgi_no_cache | uwsgi_no_cache | |
将HEAD方法转换为GET方法 | proxy_cache_convert_head | ||||
加锁减少回源请求 | proxy_cache_lock | fastcgi_cache_lock | scgi_cache_lock | uwsgi_cache_lock | |
回源请求锁的超时时间 | proxy_cache_lock_age | fastcgi_cache_lock_age | scgi_cache_lock_age | uwsgi_cache_lock_age | |
等待回源请求锁的超时时间 | proxy_cache_lock_timeout | fastcgi_cache_lock_timeout | scgi_cache_lock_timeout | uwsgi_cache_lock_timeout | |
独有配置 | uwsgi_modifier1 | ||||
uwsgi_modifier2 | |||||
uwsgi_param | uwsgi_param | uwsgi_param | |||
fastcgi_index | |||||
fastcgi_catch_stderr |
3. Plus增加了什么功能
Plus的功能增加,在这个四个模块中是一样的,这里以http proxy为例进行说明。
1) 存储的缓存数据精细程度不同。
开源版本只保存了缓存的对象。Plus版本在开原基础上保存了更多扩展信息。包括size、max_size、hit、stale、updating、revalidated、miss、expired和bypass。该数据可以通过api接口进行查看,
增加该功能后,会导致每条缓存占用的存储空间变大。每兆存储4000条(开源版本为8000条)。
2) Purge功能。
plus版本增加了缓存清除的功能,该功能在oss中可以使用第三方插件完成。
在NGINX Plus中,可以通过proxy_cache_pruge来配置,基本语法:
举例说明:
该功能可以在需要进行缓存刷新,而缓存的过期时间还没到的时候,用来达到刷新缓存的目的。该功能标准的nginx oss是不支持,配置的时候会报错:
但该功能可以通过第三方模块ngx_cache_purge来实现。需要通过自编译版本的nginx,通--add-module=指定添加模块到nginx中。
4. 场景使用
需要对缓存进行精细化管理的场景。通过缓存的访问数据来分析,可以评估plus的缓存功能帮助服务器节省的运算能力,同时根据缓存的使用情况,调整页面的部署和资源的调配。
5. 总结
在这一部分,NGINX plus相对于nginx oss在功能有两个优势。一个是缓存的精确管理数据;另一个是缓存清除,尽管该功能OSS可以通过第三方模块进行弥补,但Plus可以免除用户编译模块,并可以结合Keyval能力实现通过API的缓存清理。
NGINX PLUS增强模块与指令
NGINX PLUS介绍