From 2b2354a1fdc11c6a233cdeeea4c2db68dda657c1 Mon Sep 17 00:00:00 2001 From: "YoungJoo.Kim" Date: Mon, 26 Sep 2022 23:35:38 +0900 Subject: [PATCH 1/3] Bugfix: fixed issues/248 Shared memory (lock|unlock) is set when using the ngx_http_vhost_traffic_status_display_get_size() function --- src/ngx_http_vhost_traffic_status_display.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ngx_http_vhost_traffic_status_display.c b/src/ngx_http_vhost_traffic_status_display.c index 17b35fc..4f4c01f 100644 --- a/src/ngx_http_vhost_traffic_status_display.c +++ b/src/ngx_http_vhost_traffic_status_display.c @@ -524,15 +524,25 @@ ngx_http_vhost_traffic_status_display_get_size(ngx_http_request_t *r, ngx_int_t format) { ngx_uint_t size, un; + ngx_slab_pool_t *shpool; + ngx_http_vhost_traffic_status_loc_conf_t *vtscf; ngx_http_vhost_traffic_status_shm_info_t *shm_info; + vtscf = ngx_http_get_module_loc_conf(r, ngx_http_vhost_traffic_status_module); + shpool = (ngx_slab_pool_t *) vtscf->shm_zone->shm.addr; + shm_info = ngx_pcalloc(r->pool, sizeof(ngx_http_vhost_traffic_status_shm_info_t)); if (shm_info == NULL) { return NGX_ERROR; } + /* Caveat: Do not use duplicate ngx_shmtx_lock() before this function. */ + ngx_shmtx_lock(&shpool->mutex); + ngx_http_vhost_traffic_status_shm_info(r, shm_info); + ngx_shmtx_unlock(&shpool->mutex); + /* allocate memory for the upstream groups even if upstream node not exists */ un = shm_info->used_node + (ngx_uint_t) ngx_http_vhost_traffic_status_display_get_upstream_nelts(r); From d0d31341e6dbdf3dc99d4eac968a532218c3e26e Mon Sep 17 00:00:00 2001 From: u5surf Date: Wed, 28 Sep 2022 03:30:13 +0900 Subject: [PATCH 2/3] Chore: add cpanm --notest in CI * To reduce the ci build time --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1e2b49a..966376b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,7 +71,7 @@ jobs: - name: 'prepare cpanm' run: | sudo apt install -y cpanminus - sudo cpanm install Test::Nginx::Socket + sudo cpanm --notest Test::Nginx::Socket > build.log 2>&1 || (cat build.log && exit 1) - name: 'prepare promtool' run: | sudo apt-get update && sudo apt-get install -y curl From e4f366c9f17ddef7d48b8e43ec07bb39543be336 Mon Sep 17 00:00:00 2001 From: Heikki Orsila Date: Mon, 30 Nov 2020 13:20:32 +0200 Subject: [PATCH 3/3] Implement vhost_traffic_status_upstream_no_cache filter This enables monitoring upstream backend events that are configured not to use cache. Add the following line to nginx config to create a group: vhost_traffic_status_filter_upstream_no_cache "NO_CACHE"; --- README.md | 27 +++++++++++++++++++ src/ngx_http_vhost_traffic_status_module.c | 23 +++++++++++++++- src/ngx_http_vhost_traffic_status_module.h | 3 +++ src/ngx_http_vhost_traffic_status_shm.c | 31 ++++++++++++++-------- src/ngx_http_vhost_traffic_status_shm.h | 3 ++- 5 files changed, 74 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6729d9d..6d36ab2 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ Table of Contents * [vhost_traffic_status_filter_by_set_key](#vhost_traffic_status_filter_by_set_key) * [vhost_traffic_status_filter_check_duplicate](#vhost_traffic_status_filter_check_duplicate) * [vhost_traffic_status_filter_max_node](#vhost_traffic_status_filter_max_node) + * [vhost_traffic_status_filter_upstream_no_cache](#vhost_traffic_status_filter_upstream_no_cache) * [vhost_traffic_status_limit](#vhost_traffic_status_limit) * [vhost_traffic_status_limit_traffic](#vhost_traffic_status_limit_traffic) * [vhost_traffic_status_limit_traffic_by_set_key](#vhost_traffic_status_limit_traffic_by_set_key) @@ -1479,6 +1480,32 @@ http { In the above example, the `/^uris.*/` and `/^client::ports.*/` group string patterns are limited to a total of 16 nodes. The other filters like `country::.*` are not limited. +### vhost_traffic_status_filter_upstream_no_cache + +| - | - | +| --- | --- | +| **Syntax** | **vhost_traffic_status_filter_upstream_no_cache** *name* | +| **Default** | - | +| **Context** | http, server, location | + +`Description:` Create a group name that counts requests that have an upstream +but should not be cached. The group is visible under serverZones. +Note: This is event is not "cache miss" or "bypass cache". Example: +`$ edit nginx.conf` + +```Nginx +http { + ... + vhost_traffic_status_zone; + ... + server { + server_name example.org; + ... + vhost_traffic_status_filter_upstream_no_cache NO_CACHE; + } +} +``` + ### vhost_traffic_status_limit | - | - | diff --git a/src/ngx_http_vhost_traffic_status_module.c b/src/ngx_http_vhost_traffic_status_module.c index bbb41eb..66cf9d8 100644 --- a/src/ngx_http_vhost_traffic_status_module.c +++ b/src/ngx_http_vhost_traffic_status_module.c @@ -210,6 +210,13 @@ static ngx_command_t ngx_http_vhost_traffic_status_commands[] = { offsetof(ngx_http_vhost_traffic_status_loc_conf_t, bypass_stats), NULL }, + { ngx_string("vhost_traffic_status_filter_upstream_no_cache"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_conf_set_str_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_vhost_traffic_status_loc_conf_t, no_cache_server_zone), + NULL }, + ngx_null_command }; @@ -265,12 +272,21 @@ ngx_http_vhost_traffic_status_handler(ngx_http_request_t *r) return NGX_DECLINED; } - rc = ngx_http_vhost_traffic_status_shm_add_server(r); + rc = ngx_http_vhost_traffic_status_shm_add_server(r, NULL); if (rc != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "handler::shm_add_server() failed"); } + if (ngx_strlen(vtscf->no_cache_server_zone.data) > 0) { + rc = ngx_http_vhost_traffic_status_shm_add_server( + r, &vtscf->no_cache_server_zone); + if (rc != NGX_OK) { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "handler::shm_add_server(NO_CACHE) failed"); + } + } + rc = ngx_http_vhost_traffic_status_shm_add_upstream(r); if (rc != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, @@ -889,6 +905,8 @@ ngx_http_vhost_traffic_status_create_loc_conf(ngx_conf_t *cf) * conf->histogram_buckets = { NULL, ... }; * conf->bypass_limit = 0; * conf->bypass_stats = 0; + * + * conf->no_cache_server_zone = { 0, NULL }; */ conf->shm_zone = NGX_CONF_UNSET_PTR; @@ -1019,6 +1037,9 @@ ngx_http_vhost_traffic_status_merge_loc_conf(ngx_conf_t *cf, void *parent, void ngx_conf_merge_value(conf->bypass_limit, prev->bypass_limit, 0); ngx_conf_merge_value(conf->bypass_stats, prev->bypass_stats, 0); + ngx_conf_merge_str_value(conf->no_cache_server_zone, + prev->no_cache_server_zone, ""); + name = ctx->shm_name; shm_zone = ngx_shared_memory_add(cf, &name, 0, diff --git a/src/ngx_http_vhost_traffic_status_module.h b/src/ngx_http_vhost_traffic_status_module.h index a4b035b..7803b0c 100644 --- a/src/ngx_http_vhost_traffic_status_module.h +++ b/src/ngx_http_vhost_traffic_status_module.h @@ -297,6 +297,9 @@ typedef struct { ngx_flag_t bypass_limit; ngx_flag_t bypass_stats; + /* Controls name of "no cache" server zone */ + ngx_str_t no_cache_server_zone; + ngx_rbtree_node_t **node_caches; } ngx_http_vhost_traffic_status_loc_conf_t; diff --git a/src/ngx_http_vhost_traffic_status_shm.c b/src/ngx_http_vhost_traffic_status_shm.c index 5ae5ed0..b1683b0 100644 --- a/src/ngx_http_vhost_traffic_status_shm.c +++ b/src/ngx_http_vhost_traffic_status_shm.c @@ -354,7 +354,8 @@ ngx_http_vhost_traffic_status_shm_add_filter_node(ngx_http_request_t *r, ngx_int_t -ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r) +ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r, + const ngx_str_t *no_cache_server_zone) { unsigned type; ngx_int_t rc; @@ -366,17 +367,25 @@ ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r) cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); - if (vtscf->filter && vtscf->filter_host && r->headers_in.server.len) { - /* set the key by host header */ - dst = r->headers_in.server; - + if (no_cache_server_zone != NULL && + ngx_strlen(no_cache_server_zone->data) > 0) { + if (r->upstream == NULL || r->upstream->cache_status != 0) { + return NGX_OK; + } + dst.len = no_cache_server_zone->len; + dst.data = no_cache_server_zone->data; } else { - /* set the key by server_name variable */ - dst = cscf->server_name; - if (dst.len == 0) { - dst.len = 1; - dst.data = (u_char *) "_"; - } + if (vtscf->filter && vtscf->filter_host && r->headers_in.server.len) { + /* set the key by host header */ + dst = r->headers_in.server; + } else { + /* set the key by server_name variable */ + dst = cscf->server_name; + if (dst.len == 0) { + dst.len = 1; + dst.data = (u_char *) "_"; + } + } } type = NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_NO; diff --git a/src/ngx_http_vhost_traffic_status_shm.h b/src/ngx_http_vhost_traffic_status_shm.h index bbc9d97..4ba4e4c 100644 --- a/src/ngx_http_vhost_traffic_status_shm.h +++ b/src/ngx_http_vhost_traffic_status_shm.h @@ -19,7 +19,8 @@ typedef struct { } ngx_http_vhost_traffic_status_shm_info_t; -ngx_int_t ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r); +ngx_int_t ngx_http_vhost_traffic_status_shm_add_server( + ngx_http_request_t *r, const ngx_str_t *no_cache_server_zone); ngx_int_t ngx_http_vhost_traffic_status_shm_add_filter(ngx_http_request_t *r); ngx_int_t ngx_http_vhost_traffic_status_shm_add_upstream(ngx_http_request_t *r);