dragonfly
dragonfly

Reputation: 133

nginx always fetches from server

I am trying to setup nginx as a caching reverse proxy, however it would appear that every request is been sent to the backend server, and nothing is been cached. i.e. the server logs on the backend show all the same file accesses.

Most of the files are either php with arguments passed on the url or images, all of which are been fetched all the time from the backend and never cached. Everything on this site can be cached.

My 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;

   }
}

and my nginx.conf file

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;

}

Can anybody tell me what I've got wrong??

Upvotes: 1

Views: 613

Answers (2)

Steven Zerbe
Steven Zerbe

Reputation: 51

I ran into this problem as well, and I found

proxy_buffering off;

Will cause nginx to bypass cache and not save the file to disk. Remove that line and then it works.

Upvotes: 5

digitalPBK
digitalPBK

Reputation: 2877

Your upstream server responses must be settings Cookies, please see https://stackoverflow.com/a/10995522/482926

Upvotes: 0

Related Questions