I want a file to be sent to /robots.txt, which I will indicate using a relative path on the server. Wrote the following rule:

location /robots.txt { alias /home/folder1/folder2/folder3/robots.txt; } 

It does not work, because it puts a slash at the end and goes to site.ru/robots.txt/

How to make so that a slash was not substituted in robots? For all other paths, slash is needed.

UPD I don't know what to think anymore. This construction works as it should:

 location /robotz.txt { alias /home/bla/bla/project/robots.txt; } 

But this adds a slash and gives 404 error:

 location /robots.txt { alias /home/bla/bla/project/robots.txt; } 
  • Try to just remove the robots.txt at the end of the alias. Alias ​​should point to the directory, not to the file - andreymal
  • As you recommend it will not work. Because it will be a link to a folder, not to a file in this folder. Below, we have already advised the option with root. Probably he is more correct and he even works. Yes, and alias works. But only if the path is not robots.txt. That is, this one is supposed to be interrupted by something. There is nothing in the configs about it and I don't know what to do or where to look) - Konstantin Komissarov
  • Well then location ~ ^/(robots\.txt)$ { alias /home/folder1/folder2/folder3/$1; } location ~ ^/(robots\.txt)$ { alias /home/folder1/folder2/folder3/$1; } - andreymal
  • $ grep -r robots /etc/nginx ? - aleksandr barakin
  • There is no mention of robots.txt in this folder :( - Konstantin Komissarov

2 answers 2

 location = /robots.txt { root /home/folder1/folder2/folder3; } 
  • There is even another problem, and I do not understand what. If I write in location /robots.txt, then a slash is added and nothing works. And if the address is different, everything works. In the configs, robots are not mentioned anywhere else. I do not know where to look. - Konstantin Komissarov

/ added by some other rule in the config. You need to see the full config file.

  • Well, I want the slash to be added to all addresses, so the rule should not be disabled. But for robots this rule does not apply. - Konstantin Komissarov
  • For clarifying messages, comments are best suited to the question. - Anton Sorokin