[英]Using prerender with proxy in nginx
I'm trying to use prerender.io to get an snapshot of angularjs pages. 我正在尝试使用prerender.io来获取angularjs页面的快照。 Currently I have an NodeJS instance for the web app and nginx
reverse proxy redirects requests from port 80
to 4000
. 目前我有一个用于Web应用程序的NodeJS实例, nginx
反向代理将请求从端口80
重定向到4000
。
According to prerender nginx manual ( https://gist.github.com/thoop/8165802 ) I can forward search-engine bot requests to the prerender url but because I already have a proxy for NodeJS application, I don't know how can I prerender try_files
directive. 根据prerender nginx手册( https://gist.github.com/thoop/8165802 ),我可以将搜索引擎机器人请求转发给prerender url,但因为我已经有了NodeJS应用程序的代理,我不知道怎么能我prerender try_files
指令。
My question is, how can I use both NodeJS application proxy and prerender directive? 我的问题是,我如何使用NodeJS应用程序代理和prerender指令?
I believe that the prerender example has the answer. 我相信prerender的例子有答案。 If prerender is set to 1, it uses rewrite and then proxy_pass. 如果prerender设置为1,则使用rewrite然后使用proxy_pass。
So you would change this: 所以你会改变这个:
if ($prerender = 0) {
rewrite .* /index.html break;
}
to this: 对此:
if ($prerender = 0) {
rewrite .* /index.html break;
proxy_pass http://[INTERNAL IP]:[PORT];
}
I would make further modifications since you are using Node and don't need some of the stuff set up for static files. 我会进一步修改,因为你使用Node并且不需要为静态文件设置一些东西。
Here is my final answer: 这是我的最终答案:
server {
listen 80;
server_name example.com;
location / {
try_files $uri @prerender;
}
location @prerender {
#proxy_set_header X-Prerender-Token YOUR_TOKEN;
set $prerender 0;
if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
set $prerender 1;
}
if ($args ~ "_escaped_fragment_") {
set $prerender 1;
}
if ($http_user_agent ~ "Prerender") {
set $prerender 0;
}
if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff)") {
set $prerender 0;
}
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
resolver 8.8.8.8;
if ($prerender = 1) {
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
set $prerender "service.prerender.io";
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://$prerender;
}
if ($prerender = 0) {
proxy_pass http://[INTERNAL IP]:[PORT];
}
}
}
I hope that helps. 我希望有所帮助。 One thing that I will add is that I wouldn't use a prerender engine. 我要补充的一件事是我不会使用预渲染引擎。 Spiders can and do index links and pages that use javascript and PDFs even. 蜘蛛可以做索引链接和使用javascript和PDF的页面。
Just my two cents. 只是我的两分钱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.