V
V
Vadim Kaznin2020-01-19 17:59:39
Nginx
Vadim Kaznin, 2020-01-19 17:59:39

How to make dynamic robots on nginx?

I'm trying to make a dynamic robots.txt (for subdomains) on a server with nginx, for this you need the php script to run when accessing the site.ru/robots.txt address. I thought that you can just set up an alias in the nginx config, but alias only works with static, so the construction is:

location /robots.txt {
       alias /var/www/www-root/data/www/site.ru/robots.txt.php;
}

Returns only the contents of robots.txt.php.
Is there any other way to make robots.txt dynamic?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vadim Kaznin, 2020-01-19
@Deymos

I solved it like this:
rewrite ^\/robots\.txt /robots.php?path=$1 last;
in the location / { section
.
.
}
This way the robots.php script will work.

D
Dmitry, 2020-01-19
@q2digger

location /robots.txt {
      add_header Content-Type text/plain;
      return 200 "User-agent: *\nDisallow: /\n";
  }

B
Boris Korobkov, 2020-01-19
@BorisKorobkov

location = /robots.txt {
        fastcgi_pass    unix:/run/php/php7.4-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question