0

I need to add a noindex tag to multiple specific uploaded files, but they all belong to different year and month upload folders like this for example:

  • /var/www/public/app/uploads/2021/06/file-1.pdf
  • /var/www/public/app/uploads/2018/11/file-2.pdf
  • /var/www/public/app/uploads/2011/07/file-3.pdf

I did this initially, but did not work:

location ~ "^(.*)/app/uploads/(.*)$" {
    location ~ ^/(file-1.pdf|file-2.pdf|file-3.pdf)$ {
        add_header X-Robots-Tag "noindex";
    }
}

I don't have access yet to test, but this is what I'm planning to do instead:

location ~ "/var/www/public/app/uploads/(.*)$" {
    location ~ ^/(file-1.pdf|file-2.pdf|file-3.pdf)$ {
        add_header X-Robots-Tag "noindex";
    }
}

I wonder if there is a better approach, or if this will even work. Right now there are 27 specific files I have to do this to so I'm not sure if the file-1.pdf|file-2.pdf|file-3.pdf is my best option.

Any help is appreciated, thanks.

1 Answer 1

1

First of all, you are definitely use the location directive incorrectly. Assuming your server root directory is /var/www/public/app and your sample request is http://example.com/uploads/2021/06/file-1.pdf the normalized URI (which is subject to check for location or rewrite directives) will be /uploads/2021/06/file-1.pdf. An example of location that will catch those requests and add a required header is

location ~ ^/uploads/(?<file>.*) {
    if ($file ~ ^(?:2021/06/file-1\.pdf|2018/11/file-2\.pdf|2011/07/file-3\.pdf))$ {
        add_header X-Robots-Tag "noindex";
    }
}

Here (?<file>.*) is so-called named capture group for later usage. Using two nested locations is possible too:

location /uploads/ {
    location ~ /(?:2021/06/file-1\.pdf|2018/11/file-2\.pdf|2011/07/file-3\.pdf)$ {
        add_header X-Robots-Tag "noindex";
    }
}

You can also use map directive (although I don't know if this could be called "a better approach"):

map $file $robots {
    2021/06/file-1.pdf    noindex;
    2018/11/file-2.pdf    noindex;
    2011/07/file-3.pdf    noindex;
    # default value will be an empty string
}

server {
    ...
    location ~ ^/uploads/(?<file>.*) {
        add_header X-Robots-Tag $robots;
    }
    ...
}

or without regexes:

map $uri $robots {
    /uploads/2021/06/file-1.pdf    noindex;
    /uploads/2018/11/file-2.pdf    noindex;
    /uploads/2011/07/file-3.pdf    noindex;
    # default value will be an empty string
}

server {
    ...
    location /uploads/ {
        add_header X-Robots-Tag $robots;
    }
    ...
}

The add_header directive won't add a header to response if its second parameter will be an empty string.

You should also take in attention this documentation excerpt:

These directives are inherited from the previous configuration level if and only if there are no add_header directives defined on the current level.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah thank you! I thought I needed to use the whole directory for it — the first solution worked well for me except it's missing a closing parenthesis for the if condition. I would have preferred to use the map since it's more manageable but it thows out a nginx: [emerg] unknown "file" variable for me.
Oh, sorry, it is a regular copy-paste error, of course we should check an $uri variable in the second map example. I agreed it looks more manageable than the other examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.