I have created 2 separate robot.txt files: robots.txt
and disabled-robots.txt
I have one server block with alias in my nginx config. What I am trying to achieve is:
- example.com/robots.txt => robots.txt
- v2.example.com/robots.txt => disabled-robots.txt
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name v2.example.com example.com;
location = /robots.txt {
// I think I need to update here, but confused about what I should put
// if ($host == v2.example.com) {
// show disabled-robots.txt
// } else {
// show robots.txt
// }
}
}
All the examples I have found on StackOverflow shows 2 separate server blocks, however I want to keep my one server block.
CodePudding user response:
Rather than use the if
inside a location
(which has problems in this context), use a map
with try_files
instead.
For example:
map $host $which_robot {
default /robots.txt;
v2.example.com /disabled-robots.txt;
}
server {
...
location = /robots.txt {
root /path/to/directory;
try_files $which_robot =404;
}
}
Note that map
must be declared outside the server
block. See the map documentation.