Change single location based on server name in Nginx

Solution 1:

You will achieve a cleaner solution with a map and a try_files statement.

For example:

map $host $robots {
    ~^\w+\.\w+\.\w+$ /robots_disallow.txt;
    default          /robots.txt;
}

server {
    ...

    location = /robots.txt {
        root /srv;
        try_files $robots =404;
    }

    ...
}

See this document for details.