我使用nginx作为反向代理,这是我的配置的一个片段,它将getActivity().startPostponedEnterTransition()
调用下的任何内容路由到内部API服务器:
/api
但是现在我想允许规则缓存一个特定的反向代理路由(比如 # API server
location /api {
if ($request_method = OPTIONS) {
return 204;
}
# Proxy settings
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://my.api-server.com
# Error handlers
error_page 504 /errors/504.json;
# CORS headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Allow' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Referrer, User-Agent, Authorization, X-Impersonate-As, X-Request-Context' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Expose-Headers' 'Authorization, X-Request-Id, X-Server-Time' always;
add_header 'Access-Control-Max-Age' 3628800;
}
,TTL = 30分钟),我尝试了这个:
/api/foo/bar
但是,这不起作用。这样做的最佳方式是什么?
编辑:
完整服务器配置:
location /api {
if ($request_method = OPTIONS) {
return 204;
}
if ($request_uri ~* /api/foo/bar) {
proxy_cache_valid 200 301 302 30m;
add_header X-Cache $upstream_cache_status;
}
....
答案 0 :(得分:2)
有几件事需要研究:
使用Cookie时强制缓存:
proxy_ignore_headers Cache-Control Expires Set-Cookie;
使用其他请求方法缓存(选择您自己的方法):
proxy_cache_methods GET HEAD POST PUT DELETE PATCH OPTIONS;
===
编辑:
尝试以下设置:
在/etc/nginx/nginx.conf
proxy_cache_path /dev/shm levels=1:2 keys_zone=mcache:16m inactive=600s max_size=512m;
proxy_cache_methods GET HEAD;
proxy_cache_min_uses 1;
proxy_cache_key "$request_method$host$request_uri";
proxy_cache_use_stale timeout updating;
proxy_ignore_headers Cache-Control Expires Set-Cookie;
在您的位置栏上:
location /api/foo/bar {
proxy_cache_bypass 0;
proxy_no_cache 0;
proxy_cache mcache; # mcache=RAM
proxy_cache_valid 200 301 302 30m;
proxy_cache_valid 403 404 5m;
proxy_cache_lock on;
proxy_cache_use_stale timeout updating;
add_header X-Proxy-Cache $upstream_cache_status;
}
确保重新加载Nginx(或停止+开始)
service nginx reload
用卷曲测试:
curl -X GET -I https://yoururl
答案 1 :(得分:-2)
确保指定proxy_cache_path(或者只是未包含在您的示例中?)