Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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.
location /robots.txt {
add_header Content-Type text/plain;
return 200 "User-agent: *\nDisallow: /\n";
}
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 questionAsk a Question
731 491 924 answers to any question