[PATCH] implement a $location variable

António P. P. Almeida appa at perusio.net
Fri Jan 4 01:58:43 UTC 2013


On 4 Jan 2013 02h22 CET, david at gwynne.id.au wrote:

> here's a diff that provides $location for use in not regex location
> blocks.
>
> we're using it to provide easy to use mass hosting of drupals:
>
> xdlg at argon nginx$ cat drupal-controller.conf
> root /var/www/apps/drupal;
> try_files /index.php =404;
> include fastcgi_params;
> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
> fastcgi_param SCRIPT_NAME $location$fastcgi_script_name;
> fastcgi_intercept_errors on;
> fastcgi_pass localhost:9000;
>
> which is used in server blocks like this:
>
> 	server {
> 		listen			80;
> 		server_name		www.example.com;
>
> 		location / { include drupal-controller.conf; }
> 		location /foo { include drupal-controller.conf; }
> 		location /bar { include drupal-controller.conf; }
> 		location /baz { include drupal-controller.conf; }
> 	}
>
> i cannot otherwise find a nice way to use the locations name as a
> parameter without specifying the value as a variable again within
> the location.

I fail to see the need for a $location variable in a Drupal Nginx
config. Can you elaborate why? The multiple inclusion is only needed
if you redefine any of the fastcgi_param(s) in each location.
 
Have you each site installed in a subdir? Is that the case?


I think this will probably work in your case:

location ~ ^(<current_location_base>[^/]*)/.*$ {
    include drupal-controller.conf;
    fastcgi_param SCRIPT_NAME $current_location_base/$fastcgi_script_name;
}

where drupal-controller.conf is: 

        root /var/www/apps/drupal;
        try_files /index.php =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_pass localhost:9000;

Try it out.

--- appa



> --- src/http/ngx_http_variables.c.orig Tue Jul 3 03:41:52 2012
> +++ src/http/ngx_http_variables.c	Fri Jan  4 10:49:50 2013
> @@ -65,6 +65,8 @@ static ngx_int_t ngx_http_variable_request_filename(ng
> ngx_http_variable_value_t *v, uintptr_t data);
> static ngx_int_t ngx_http_variable_server_name(ngx_http_request_t *r,
> ngx_http_variable_value_t *v, uintptr_t data);
> +static ngx_int_t ngx_http_variable_location(ngx_http_request_t *r,
> +    ngx_http_variable_value_t *v, uintptr_t data);
> static ngx_int_t ngx_http_variable_request_method(ngx_http_request_t *r,
> ngx_http_variable_value_t *v, uintptr_t data);
> static ngx_int_t ngx_http_variable_remote_user(ngx_http_request_t *r,
> @@ -206,6 +208,10 @@ static ngx_http_variable_t  ngx_http_core_variables[] 
>
> { ngx_string("server_name"), NULL, ngx_http_variable_server_name, 0,
> 0, 0 },
>
> +    { ngx_string("location"), NULL,
> +      ngx_http_variable_location, 0,
> +      NGX_HTTP_VAR_NOCACHEABLE, 0 },
> + { ngx_string("request_method"), NULL,
> ngx_http_variable_request_method, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
> @@ -1382,6 +1388,28 @@
> ngx_http_variable_server_name(ngx_http_request_t *r,
> v->no_cacheable = 0;
> v->not_found = 0;
> v->data = cscf->server_name.data;
> +
> +    return NGX_OK;
> +}
> +
> +
> +static ngx_int_t
> +ngx_http_variable_location(ngx_http_request_t *r,
> +    ngx_http_variable_value_t *v, uintptr_t data)
> +{
> +    ngx_http_core_loc_conf_t  *clcf;
> +
> +    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
> +
> +    if (clcf->regex) {
> +        v->not_found = 1;
> +    } else {
> +        v->len = clcf->name.len;
> +        v->valid = 1;
> +        v->no_cacheable = 0;
> +        v->not_found = 0;
> +        v->data = clcf->name.data;
> +    }
>
> return NGX_OK;
> }
>
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel



More information about the nginx-devel mailing list