我有一个特殊的情况,当我发现访问者是一个爬虫时,我想将访问者重定向到“预渲染”服务器(以提供一个 SEO 优化的网站),否则就重定向到标准的 php fast-cgi 实例(通过 fastcgi 传递)。
我对 nginx 的经验很少,所以我无法完成我需要做的事情。
这是我当前配置的草稿
server {
server_name example.com;
root /vagrant/example.com/web;
index app.php;
location /phpmyadmin {
root /usr/share;
index index.php;
location ~* \.php {
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
location / {
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/(app|app_dev)\.php(/|$) {
#if $prerender is 1 should use the prerender proxy
set $prerender 0;
if ($http_user_agent ~* "googlebot|pinterest|vkshare|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot") {
set $prerender 1;
}
if ($args ~ "_escaped_fragment_") {
set $prerender 1;
}
if ($http_user_agent ~ "Prerender") {
set $prerender 0;
}
proxy_set_header Authorization "Basic XXXXXXXXXXX"; #prerender server needs authentication
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
if ($prerender = 1) {
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://localhost:3456;
break;
}
#otherwise should use the fastcgi proxy
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
请注意,在我的开发环境中,我还有一个位置块来启用 phpmyadmin。之前的配置无法按预期工作,因为它始终使用 fastcgi_pass。
有什么建议吗?
提前致谢。