nginx总是从服务器获取

时间:2013-09-01 21:10:42

标签: php caching nginx

我正在尝试将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;

}

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

2 个答案:

答案 0 :(得分:5)

我也遇到了这个问题,我找到了

proxy_buffering off;

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

答案 1 :(得分:0)

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