1

我正在尝试将 nginx 设置为缓存反向代理,但是似乎每个请求都已发送到后端服务器,并且没有任何内容被缓存。即后端的服务器日志显示所有相同的文件访问。

大多数文件要么是在 url 上传递参数的 php,要么是图像,所有这些文件都是从后端一直获取的,从不缓存。该站点上的所有内容都可以缓存。

我的 conf.d/default.conf

upstream xxxx  {
      server xxxx.com;
}

#
# The default server
#
server {
    listen   80 default_server;
    server_name  _;

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

    root   /usr/share/nginx/html;
    index  index.html index.htm;


    location / {
    ## send request back to xxxx ##
     proxy_pass  http://xxxx;
     proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

#     expires 24h;
#    add_header        Cache-Control public;
     proxy_ignore_headers Cache-Control Expires;

     proxy_redirect off;
     proxy_buffering off;

     proxy_cache               one;
     proxy_cache_key         backend$request_uri;
     proxy_cache_valid       200 301 302 1440m;
     proxy_cache_valid       404 1m;
     proxy_cache_valid       any 1440m;
     proxy_cache_use_stale   error timeout invalid_header updating;

     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   }
}

和我的 nginx.conf 文件

user              nginx;
worker_processes  8;
worker_rlimit_nofile 8192;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;

events {
    worker_connections  2048;
}

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 "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    server_names_hash_bucket_size 64;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    off;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_comp_level 9;
    gzip_proxied any;

    proxy_buffering on;
    proxy_cache_path /usr/local/nginx/proxy levels=1:2 keys_zone=one:1024m inactive=7d max_size=700g;
    proxy_temp_path /tmp/nginx/proxy;

    proxy_buffer_size 4k;
    proxy_buffers 100 8k;
    proxy_connect_timeout      60;
    proxy_send_timeout         60;
    proxy_read_timeout         60;
    include /etc/nginx/conf.d/*.conf;

}

谁能告诉我我做错了什么??

4

2 回答 2

5

我也遇到了这个问题,我发现

proxy_buffering off;

将导致 nginx 绕过缓存而不将文件保存到磁盘。删除该行,然后它就可以工作了。

于 2014-08-21T17:30:17.093 回答
0

您的上游服务器响应必须是设置 Cookie,请参阅 https://stackoverflow.com/a/10995522/482926

于 2014-04-11T18:35:27.540 回答