启用NGINX第三方代理缓存



我试图通过缓存响应和避免我们从API端点获得的429响应来避免过于频繁地撞击第三方API。

为了完成这个,我设置了一个运行Ubuntu 20.04的Linode服务器。

配置文件etc/nginx/conf.d/nginx.conf如下所示

server {
server_name myserver-name-proxy.server.com;
access_log /var/log/access.log main;
error_log /var/log/error.log info;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache my_cache;
proxy_ignore_headers Cache-Control;
proxy_cache_methods GET HEAD POST;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
proxy_pass https://www.thirdpartyserver.com;
proxy_ssl_session_reuse on;
proxy_ssl_server_name on;
proxy_set_header X-Forwarded-Proto https;
proxy_buffering on;
proxy_cache_key $scheme$proxy_host$request_uri;
}
default_type application/json;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myserver-name-proxy.server.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myserver-name-proxy.server.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = myserver-name-proxy.server.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

server_name myserver-name-proxy.server.com;
listen 80;
listen [::]:80;
return 404; # managed by Certbot
}

则配置文件etc/nginx/conf.d/nginx.conf

user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
worker_connections  1024;
}

http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent $request_time "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_cache_status" "$http_x_cache_status"';
proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=24h use_temp_path=off;
access_log  /var/log/nginx/access.log  main;
sendfile        on;
#tcp_nopush     on;
keepalive_timeout  65;
#gzip  on;
include /etc/nginx/conf.d/*.conf;
}

没有错误,但是当我tail访问日志文件或检查头时,只有MISS从NGINX返回作为$http_x_cache_status

到目前为止,我已经尝试将proxy_cache_path更改为新文件夹。当重新启动时,这个文件夹是由NGINX服务器创建的,但没有写入任何内容,加上一些其他的事情,如切换goff后台更新,缓存锁定等。

我能看到这和所有的教程之间的唯一区别是,我使用SSL和点击https://结束并在设置中使用proxy_ssl_session_reuseproxy_ssl_server_name

有两个问题。

首先,上游服务器的响应没有返回任何过期头,所以NGINX不会缓存这些项。我还设置了proxy_ignore_headers Cache-Control;

为了确保NGINX在这些请求上设置缓存,你需要包含proxy_cache_valid

这对一个端点有效,但我的另一个端点仍然失败。

第二个原因是因为源/上游服务器响应Set-Cookie报头,这意味着响应没有被缓存。为了解决这个问题,我还需要将Set-Cookie添加到proxy_ignore_headers

我的最终规则集是

location / {
proxy_pass https://www.thirdpartyserver.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache nft_cache;
proxy_ignore_headers Cache-Control Set-Cookie;
proxy_cache_methods GET HEAD POST;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504 http_429;
proxy_cache_background_update on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
proxy_ssl_session_reuse on;
proxy_ssl_server_name on;
proxy_ssl_verify off;
proxy_set_header X-Forwarded-Proto https;
proxy_buffering on;
proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid 10080m;

我所做的另一个更新是针对缓存POST请求的服务器。因为URL永远不会改变,所以你应该更新proxy_cache_key以包含$request_body

proxy_cache_key $scheme$proxy_host$request_uri$request_body;

这意味着您可以使用不同的请求体到达相同的post端点,并且知道您将捕获正确的响应(这是使用GraphQL端点完成的,而不是用于发送表单)。

——编辑——

我注意到在一些POST请求中缓存再次被跳过。事实证明,这是因为proxy_buffer的大小不足以包含请求。我还必须包含

proxy_buffers 8 32k;
proxy_buffer_size 64k;

最新更新