【问题标题】:Nginx Caching 301 ResponseNginx 缓存 301 响应
【发布时间】:2021-10-21 10:49:37
【问题描述】:

我有一个 nginx 反向代理,它将所有请求转发到服务器,然后将 (301) 重定向到 aws 服务。请求返回的 pdb 文件可能非常大,通常可能会多次调用同一个请求,因此缓存响应会很好。

我的问题是,似乎不是缓存“文件”,而是缓存重定向本身而不是响应,因为缓存文件远不及实际文件的大小,并且请求时间的增益是比我尝试不使用重定向要少得多。

我想知道是否有办法避免这种行为并每次都缓存响应。

这是我的配置

user nginx;
worker_processes  1;
events {
  worker_connections  10240;
}
http {

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile            on;
  tcp_nopush          on;
  tcp_nodelay         on;
  keepalive_timeout   65;
  types_hash_max_size 4096;

  include             /etc/nginx/mime.types;
  default_type        application/octet-stream;

  proxy_cache_path /var/cache/nginx keys_zone=one:10m use_temp_path=off;
  server {
      listen       80;
      listen       [::]:80;
      server_name  _;
      root         /usr/share/nginx/html;

      # Load configuration files for the default server block.
      include /etc/nginx/default.d/*.conf;

    location / {
      proxy_pass http://server.org/;
      proxy_redirect off;
      proxy_cache one;
      proxy_http_version     1.1;
      proxy_intercept_errors on;
      proxy_cache_key $scheme$request_uri;
      proxy_cache_valid any 24h;
    }
  }
}

【问题讨论】:

    标签: http nginx caching https nginx-reverse-proxy


    【解决方案1】:

    缓存重定向是预期的行为,因为目标可能会更改,并且服务器在返回重定向时没有目标内容的副本。它只有重定向的副本,但没有实际内容。

    所以,你首先需要让 nginx 跟随重定向:

    https://serverfault.com/questions/423265/how-to-follow-http-redirects-inside-nginx

    https://gist.github.com/JonasGao/89370dc9cf84f4bbc1131fce2e700635 .

    或者,您可能希望上游服务器返回内容而不是重定向。由于您可以控制上游服务器,我认为这是更好的选择。

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      相关资源
      最近更新 更多