PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, June 24, 2022

[FIXED] Why doesn't my Nginx configuration cache the response?

 June 24, 2022     nginx, nginx-reverse-proxy, reverse-proxy     No comments   

Issue

I have the following Nginx configuration, which we can see from the output of nginx -T is syntactically correct. I bolded some relevant parts of the output below:

$ sudo nginx -T
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
events {}

http {
    proxy_cache_path /tmp/cache keys_zone=one:10m levels=1:2 inactive=2M max_size=100g;

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

    # Static file server
    server {
        listen 127.0.0.1:8080;

        root /opt/nginx-test-data;

        location / {
        }
    }

    # Reverse proxy that talks to server defined above
    server {
        listen 127.0.0.1:8081;
        proxy_cache_min_uses 1;

        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_cache one;
        }
    }
}

I know in normal practice that the server and proxy server are on different hosts. At the moment I am just trying to learn how to configure an Nginx proxy server with content caching, since Nginx is new to me.

I have the following 2MB file of random bytes:

$ ls -lh /opt/nginx-test-data/random.bin 
-rw-rw-r-- 1 shane shane 2.0M Jun 21 11:39 /opt/nginx-test-data/random.bin

When I curl the reverse proxy server, I get a 200 response:

$ curl --no-progress-meter -D - http://localhost:8081/random.bin --output local-random.bin
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Mon, 21 Jun 2021 15:50:07 GMT
Content-Type: text/plain
Content-Length: 2000000
Connection: keep-alive
Last-Modified: Mon, 21 Jun 2021 15:39:54 GMT
ETag: "60d0b2ca-1e8480"
Accept-Ranges: bytes

However, my cache directory is empty:

$ sudo ls -a /tmp/cache/
.  ..

I checked both /var/log/nginx/access.log and /var/log/nginx/error.log, and there were no errors logged.

What have I done wrong so that there are no entries in my cache directory after making a request to the reverse proxy server?


Solution

It turns out that I needed to add a proxy_cache_valid directive (though it is not clear to me why this is necessary - I assumed simply using proxy_cache in a location would turn on caching on its own).

My nginx.conf that worked (note the new line in bold):

events {}

http {
    proxy_cache_path /tmp/cache keys_zone=one:10m levels=1:2 inactive=2M max_size=100g;

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

    # Static file server
    server {
        listen 127.0.0.1:8080;

        root /opt/nginx-test-data;

        location / {
        }
    }

    # Reverse proxy that talks to server defined above
    server {
        listen 127.0.0.1:8081;
        proxy_cache_min_uses 1;

        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_cache one;
            proxy_cache_valid 200 10m;
        }
    }
}


Answered By - Shane Bishop
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing