nginx http-core enhancement: named location in subrequests + directive use_location

Maxim Dounin mdounin at mdounin.ru
Wed Apr 29 13:48:28 UTC 2015


Hello!

On Wed, Apr 29, 2015 at 09:18:11AM +0200, Sergey Brester wrote:

>  
> 
> 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.brester at 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`

Something like this was previously discussed more than once, and 
the short answer is "no".

> # HG changeset patch
> # User Serg G. Brester (sebres) <serg.brester at 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;
> +        }
> +    }
> +

And how it's expected to be processed in a named location if 
r->uri is "@..."?

Note that named locations are ones where requests are handled with 
their original URIs unmodified.  This, in particular, allows to 
use the original URI to select a file, or in a request to the 
upstream server, etc.  With what you are trying to do it doesn't 
look different from a static location.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx-devel mailing list