0

As title, i can't get nginx caching to works on mine reverse proxy. Here is mine nginx's server block configuration:

proxy_cache_path /opt/cache/nginx/cache2.xxx.net levels=1:2 keys_zone=test_zone:10m;

server {
    server_name cache2.xxx.net;
    listen 443 ssl http2; #listen for https connections, http2 enabled
    listen 80; # listen for http connections too

    # certificate's stuff
    ssl_certificate /etc/letsencrypt/live/xxx.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xxx.net/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;

    # main test location
    location  / {
        # Passed headers
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_set_header  X-Forwarded-Proto $scheme;
        proxy_set_header  User-Agent $http_user_agent;
        proxy_set_header  Host $host;
        proxy_set_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,X-Ajax-Referer';

        # Timeouts
        proxy_connect_timeout       20;
        proxy_send_timeout          20;
        proxy_read_timeout          20;
        send_timeout                20;                 

        # Caching directives
        proxy_cache test_zone;
        proxy_cache_background_update off;
        proxy_cache_convert_head on;
        proxy_cache_key "$host$request_uri$request_method";
        proxy_cache_lock off;
        proxy_cache_lock_age 5s;
        proxy_cache_lock_timeout 5s;
        proxy_cache_methods GET HEAD;
        proxy_cache_min_uses 1;
        proxy_cache_use_stale off;
        proxy_cache_valid 200 302 10m;


        if ($scheme = https) {
            add_header "Content-Security-Policy" "upgrade-insecure-requests";
        }

        proxy_pass http://XXX.XXX.XXX.XXX; # remote server, over http
        add_header Caching $upstream_cache_status; # Add an header to show the caching status
   }
}

This configuration seems to be ok. Then i made some test: The caching seems to be ok over HTTP traffic:

curl -D - -o /dev/null -k -s http://cache2.xxx.net/login

Gave me this result:

HTTP/1.1 200 OK
Server: xxx-nginx
Date: Fri, 03 Aug 2018 21:54:16 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Caching: HIT

Then i made another test, but this time over HTTPS (http2 version, but it doesn't make any difference, i also tryed without that), and here is the result:

curl -D - -o /dev/null -k -s https://cache2.xxx.net/login

HTTP/2 200
server: nginx
date: Fri, 03 Aug 2018 21:55:42 GMT
content-type: text/html; charset=UTF-8
content-security-policy: upgrade-insecure-requests

This time the Cacheing header is totally missing, this because the $upstream_cache_status variable has a null value. But why?

Another strange thing I noticed is that the HTTP request has a different "server" header of the HTTPS one.

  • 1
    What is xxx-nginx? What have you omitted to mention about this setup? You should not obfuscate anything if you can avoid doing so. – Michael Hampton Aug 3 '18 at 22:09
  • You should also remember that in HTTPS nothing is cacheable by default. Your application must explicitly set Cache-Control headers to allow for responses to be cached. – Michael Hampton Aug 3 '18 at 22:10
  • @MichaelHampton xxx-nginx is a modified nginx version name from the source code. The HTTPS is on the reverse proxy front-end, the backend traffic is over HTTP, so the server technically can store that. (For example proxy_store directive can store that, but i need proxy_cache) – Andrew Aug 3 '18 at 22:32

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.