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 does 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 affect.

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/{app-name}.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’s article.