Hi,
enclosed you will find an attached changeset, that: - allows to fast use of named location in sub requests, such as auth_request, etc. Currently no named location was possible in any sub requests, real (or internal) locations only. # now you can use named location in sub requests: # auth_request /auth_loc/; auth_request @auth_loc; - in addition, a second mini-commit (37d7786e7015) with new directive "use_location" as alias or replacement for "try_files" with no file argument and without checking the existence of file(s): # try_files "" @loc use_location @loc It was allready more times discussed (goto location, etc.). PS. If someone needs a git version of it: https://github.com/sebres/nginx/commits/hg-sb-mod [1] Regards, sebres. Links: ------ [1] https://github.com/sebres/nginx/commits/hg-sb-mod
# HG changeset patch # User Serg G. Brester (sebres) <serg.bres...@sebres.de> # Date 1430227790 -7200 # Tue Apr 28 15:29:50 2015 +0200 # Node ID 37d7786e7015f8a784e6a4dc3f88f8a7573a4c08 # Parent 96e22e4f1b03ff15a774c6ed34d74b897af32c55 http-core: new directive "use_location" as replacement or alias for "try_files" with no file argument and without checking the existence of file(s): `use_location @loc` replaces `try_files "" @loc` diff -r 96e22e4f1b03 -r 37d7786e7015 src/http/ngx_http_core_module.c --- a/src/http/ngx_http_core_module.c Mon Apr 27 18:51:18 2015 +0300 +++ b/src/http/ngx_http_core_module.c Tue Apr 28 15:29:50 2015 +0200 @@ -660,6 +660,13 @@ static ngx_command_t ngx_http_core_comm 0, NULL }, + { ngx_string("use_location"), + NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_core_try_files, + NGX_HTTP_LOC_CONF_OFFSET, + 0, + NULL }, + { ngx_string("post_action"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF |NGX_CONF_TAKE1, # HG changeset patch # User Serg G. Brester (sebres) <serg.bres...@sebres.de> # Date 1430228073 -7200 # Tue Apr 28 15:34:33 2015 +0200 # Node ID 43135346275c76add5bf953024a3d244f04184ba # Parent 37d7786e7015f8a784e6a4dc3f88f8a7573a4c08 http-core: allow to fast use of named location in (internal) sub requests, ex.: auth_request, etc.; diff -r 37d7786e7015 -r 43135346275c src/http/ngx_http_core_module.c --- a/src/http/ngx_http_core_module.c Tue Apr 28 15:29:50 2015 +0200 +++ b/src/http/ngx_http_core_module.c Tue Apr 28 15:34:33 2015 +0200 @@ -22,6 +22,7 @@ typedef struct { static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r); +static ngx_int_t ngx_http_core_find_named_location(ngx_http_request_t *r, ngx_str_t *name); static ngx_int_t ngx_http_core_find_static_location(ngx_http_request_t *r, ngx_http_location_tree_node_t *node); @@ -1542,6 +1543,16 @@ ngx_http_core_find_location(ngx_http_req noregex = 0; #endif + /* already internal - check is resp. can be named location - search it */ + + if (r->internal && r->uri.len >= 1 && r->uri.data[0] == '@') { + + if (ngx_http_core_find_named_location(r, &r->uri) == NGX_OK) { + + return NGX_OK; + } + } + pclcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); rc = ngx_http_core_find_static_location(r, pclcf->static_locations); @@ -1600,6 +1611,51 @@ ngx_http_core_find_location(ngx_http_req /* + * NGX_OK - named location was found (set in r->loc_conf) + * NGX_DECLINED - not found + */ + +static ngx_int_t +ngx_http_core_find_named_location(ngx_http_request_t *r, ngx_str_t *name) +{ + ngx_http_core_srv_conf_t *cscf; + ngx_http_core_loc_conf_t **clcfp; + + cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); + + if (cscf->named_locations) { + + for (clcfp = cscf->named_locations; *clcfp; clcfp++) { + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "test named location: \"%V\"", &(*clcfp)->name); + + if (name->len != (*clcfp)->name.len + || ngx_strncmp(name->data, (*clcfp)->name.data, name->len) != 0) + { + continue; + } + + if (r->args.len) { + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "using location: %V \"%V?%V\"", + name, &r->uri, &r->args); + } else { + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "using named location: %V", name); + } + + r->loc_conf = (*clcfp)->loc_conf; + + return NGX_OK; + } + } + + return NGX_DECLINED; +} + + +/* * NGX_OK - exact match * NGX_DONE - auto redirect * NGX_AGAIN - inclusive match @@ -2644,8 +2700,6 @@ ngx_http_internal_redirect(ngx_http_requ ngx_int_t ngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name) { - ngx_http_core_srv_conf_t *cscf; - ngx_http_core_loc_conf_t **clcfp; ngx_http_core_main_conf_t *cmcf; r->main->count++; @@ -2668,44 +2722,25 @@ ngx_http_named_location(ngx_http_request return NGX_DONE; } - cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); - - if (cscf->named_locations) { - - for (clcfp = cscf->named_locations; *clcfp; clcfp++) { - - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, - "test location: \"%V\"", &(*clcfp)->name); - - if (name->len != (*clcfp)->name.len - || ngx_strncmp(name->data, (*clcfp)->name.data, name->len) != 0) - { - continue; - } - - ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, - "using location: %V \"%V?%V\"", - name, &r->uri, &r->args); - - r->internal = 1; - r->content_handler = NULL; - r->uri_changed = 0; - r->loc_conf = (*clcfp)->loc_conf; - - /* clear the modules contexts */ - ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module); - - ngx_http_update_location_config(r); - - cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); - - r->phase_handler = cmcf->phase_engine.location_rewrite_index; - - r->write_event_handler = ngx_http_core_run_phases; - ngx_http_core_run_phases(r); - - return NGX_DONE; - } + if (ngx_http_core_find_named_location(r, name) == NGX_OK) { + + r->internal = 1; + r->content_handler = NULL; + r->uri_changed = 0; + + /* clear the modules contexts */ + ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module); + + ngx_http_update_location_config(r); + + cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); + + r->phase_handler = cmcf->phase_engine.location_rewrite_index; + + r->write_event_handler = ngx_http_core_run_phases; + ngx_http_core_run_phases(r); + + return NGX_DONE; } ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
_______________________________________________ nginx-devel mailing list nginx-devel@nginx.org http://mailman.nginx.org/mailman/listinfo/nginx-devel