Add headers to certain directories in Nginx under RunCloud

Sometimes we wants to add custom HTTP headers to certain directories and their files.

For example: I wanted to add X-Robots-Tag header to some some private directories so it do not get indexed.

I’ve tried many methods in the internet but none of them worked they way I wanted it to be. For example if the directory’s non-exists files get passed to PHP (like WordPress or Laravel) then add_header does not come in effect.

The ONLY way to make sure add_header will be included in the directories is by using ngx_http_map_module. Here’s how to do it when server is managed by RunCloud.

Open /etc/nginx-rc/main-extra.conf and add the following lines:

map $request_uri $robot_header {
    default "";
    ~.*/(?:nsa|fbi|secret)/.* "noindex, nofollow";
}

Now open /etc/nginx-rc/extra.d/<span style="color: #ff6600;">{app-name}</span>.headers.extra.conf and the following line:

add_header X-Robots-Tag $robot_header always;

In this example when a request url contains /nsa/ or /fbi/ or /secret/ nginx will add the X-Robots-Tag headers to that request. This will also works even if the request is handled by PHP or other language. The always parameter is used to tell Nginx add the header regardless of response code (even if it’s 404, 403 or others).

Note: The way I added the codes follows the RunCloud’s guidelines.

Credits: Allen Luce on stackoverflow.

Also remember: When you use add_header in a location block in Nginx; it removes all “parent” add_header directives. Read more about this on peterbe.com’s article.