np3228
np3228

Reputation: 103

NGINX try_files can not find the "$1" in directory

I am hitting a problem with NGINX try_files and $1

I want NGINX to serve a file if it is already in a folder. If it is not in a folder, then the request should be sent to Django.

Here is the file in NGINX:

ls /app/processed/instagramwhite.svg 
/app/processed/instagramwhite.svg

Here is the nginx location config:

location ~* ^\/media\/(.*\.svg)$ {
    # return 200 $1; # This statement returns the name of the file when enabled, so regex works
    root /app/processed/;
    try_files $1 @djangoapp; # This always calls django
  }

Here is how I send the request:

curl -s https://my_domain_here.com/media/instagramwhite.svg

I expect NGINX to return the file without going to django since the file name from the parameter is in the folder. Instead it invokes django for every request.

What am I doing wrong? Why does try_files keep calling django even when the file is there?

Thanks in advance.

Upvotes: 2

Views: 985

Answers (1)

np3228
np3228

Reputation: 103

Here is the solution:

location ~* ^\/media\/(.*\.svg)$ {
    # return 200 $1;
    root /app/processed/;
    try_files /$1 @djangoapp;
  }

You have to add "/" in front of "$1" to make it work. So "/$1" works but "$1" does not.

Upvotes: 2

Related Questions