我有一个前端渲染应用程序的角度需要可被我们下面的机器人抓取。我让它在没有缓存的情况下工作但是当想要添加缓存时,conf文件无效。我已经尝试了太长时间来搜索这个问题并且无法提出解决方案。您似乎无法在if语句中设置proxy_cache。那么我在这里有什么选择,或者我是以错误的方式来做这件事的?
server {
server_name IP;
add_header X-Cache $upstream_cache_status;
location / {
access_log off;
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;
}
if ($prerender = 1) {
proxy_cache one;
proxy_cache_valid 200 30m;
proxy_cache_valid 404 1m;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
proxy_hide_header X-powered-by;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
expires 10m;
proxy_http_version 1.1;
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://localhost:3000; #my prerender node server
}
if ($prerender = 0) {
root /my/static/stuff;
expires max;
}
}
}
答案 0 :(得分:0)
几乎忘了我问了这个问题,直到有人投了它。 这就是我最终解决它的原因(由于nginx中if情况的限制)。
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=prerender:60m max_size=200m inactive=30000m;
upstream prerender_upstream {
server 127.0.0.1:3000;
keepalive 64;
}
你的siteconf中的
server {
server_name IP;
add_header X-Cache $upstream_cache_status;
location /prerender/ {
access_log off;
proxy_cache prerender;
proxy_cache_valid 200 60s;
proxy_cache_valid 404 60s;
proxy_pass http://prerender_upstream/;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
proxy_hide_header X-powered-by;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
expires 60s;
proxy_http_version 1.1;
}
location / {
access_log off;
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;
}
if ($prerender = 1) {
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://$host/prerender/http://$host$request_uri;
}
if ($prerender = 0) {
root /my/static/stuff;
expires max;
}
}
}
效果很好。希望这有助于某人。