Can block spider access to all files, include .php file?

I had found if I blocked all files, include .php file, I can’t open my
website.

location ~* ..$ {
if ($http_user_agent ~* baidu|spider) {
rewrite ^/ http://www.mydomain.com/robots.txt;
}
}

If I setup this rule into my nginx.conf file, I can’t open my website.
So, how can I block spider access to all files?

Posted at Nginx Forum: