0

I'm trying to match a location url to this format /v1/images/{path1}/fetch?imageUrl={imageUrl} and I managed to do so with this snippet of code

location ~ ^/v1/images/(?<path1>[^/]+)/fetch {
    if ($args ~* "imageUrl=.*") {
      set $path1 $arg_path1;
      set_sha1 $variable $arg_imageUrl;
      set $imageUrl "https://testing.com/test/images/$variable/$path1.$image_ext";
      return 200 $imageUrl;
      add_header Content-Type text/plain;
      proxy_pass $imageUrl;
      access_log /etc/nginx/conf.d/log_file.log traffic;
    }
  }

But, I'm supposed to have a value for the path1 variable here returned in the response, but I don't get anything. I'm not sure why it's not being returned here, as it's part of the request too. Am I doing something wrong ?

Esam Olwan
  • 105
  • 3
  • 1
    You are overwriting the `$path1` variable. This variable is created by the regular expression, then you overwrite it with the value of `$arg_path1` (which does not exist). Remove the `set $path1 $arg_path1;` line. – Richard Smith Mar 25 '23 at 10:02

1 Answers1

0

It seems that your Nginx configuration is missing the part that returns the value of the path1 variable in the response. You can add the add_header directive to include the path1 variable in the response header.

Example:

location ~ ^/v1/images/(?<path1>[^/]+)/fetch {
  if ($args ~* "imageUrl=.*") {
    set $path1 $arg_path1;
    set_sha1 $variable $arg_imageUrl;
    set $imageUrl "https://testing.com/test/images/$variable/$path1.$image_ext";
    add_header X-Path1 $path1; # add this line to include path1 in the response header
    return 200 $imageUrl;
    add_header Content-Type text/plain;
    proxy_pass $imageUrl;
    access_log /etc/nginx/conf.d/log_file.log traffic;
  }
}

Hawshemi
  • 302
  • 5