• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Question Using Nginx Fastcgi_cache with Nginx as proxy?

Laurence@

Regular Pleskian
Simple question, can it be done. :)

We want to offer some full page caching. Varnish with Docker is not an option as we use CloudLinux and Docker doesn't support it. As a standard hosting service we need .htaccess to work as that is what everyone is familiar with. So running FPM under the Nginx handler is no go. My senior sysadmin says Nginx fastcgi_cache just won't work in a proxy environment. Can it be done?
 
You may use proxy_cache, it provides the same capabilities in proxy environment as fastcgi_cache in FPM.
 
Okay, looked into it. Found this:

Resolved - Nginx proxy_cache_path

Seems proxy_cache requires a different cache location for each domain. How would this be automated? That thread indicates the rules go in:

/etc/nginx/conf.d/proxy_cache.conf

But the location set there is global:

Reverse Proxy with Caching | NGINX

http {
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m
inactive=24h max_size=1g;
server {
location / {
proxy_pass http://1.2.3.4;
proxy_set_header Host $host;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
}
}
}

We would need the proxy_cache_path at /var/cache<vhost>/nginx.
 
Proxy_cache doesn't require the separate cache location for each domain but this is desired behavoir.
You may place cache path definition to the very beginning of virtual host file (before 'server' directive), it will be in http context (as on your example above) and define unique zone name and path for each vhost.
Example:

Code:
http {
  proxy_cache_path /data/nginx/cache/domain1.com levels=1:2 keys_zone=domain1.com:10m inactive=24h max_size=1g;
  server {
    server_name domain1.com;
    location / {
      proxy_pass http://1.2.3.4;
      .....
      proxy_cache domain1.com;
      ....
    }
  }
  proxy_cache_path /data/nginx/cache/domain2.com levels=1:2 keys_zone=domain2.com:10m inactive=24h max_size=1g;
  server {
    server_name domain2.com;
    location / {
      proxy_pass http://5.6.7.8;
      .....
      proxy_cache domain2.com;
      ....
    }
  }
}
 
Last edited:
Thanks Igor. But how can this be automated. Editing a file every time a domain is added is going to unsustainable.
 
I have added
proxy_cache_path /var/nginx/cache/<?php echo $VAR->domain->asciiName ?> levels=1:2 keys_zone=<?php echo $VAR->domain->asciiName ?>:10m inactive=24h max_size=10m;

to
/usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php above the server { directive I get the bellow error:

2017-12-03 12:04:06] ERR [util_exec] proc_close() failed ['/usr/local/psa/admin/bin/nginx-config' '-t'] with exit code [1]
[2017-12-03 12:04:06] ERR [panel] Apache config (15122990450.99090900) generation failed: Template_Exception: nginx: [emerg] the same path name "/var/nginx/cache/DOMAIN.net" used in /etc/nginx/plesk.conf.d/vhosts/DOMAIN.net.conf:6 and in /etc/nginx/plesk.conf.d/vhosts/DOMAIN.net.conf:64
nginx: configuration file /etc/nginx/nginx.conf test failed

file: /usr/local/psa/admin/plib/Template/Writer/Webserver/Abstract.php
line: 75
code: 0
nginx: [emerg] the same path name "/var/nginx/cache/DOMAIN.net" used in /etc/nginx/plesk.conf.d/vhosts/DOMAIN.net.conf:6 and in /etc/nginx/plesk.conf.d/vhosts/DOMAIN.net.conf:64
nginx: configuration file /etc/nginx/nginx.conf test failed

Which are the template files that need to hold the proxy_cache_path and proxy_cache directives on which line?
 
Last edited:
@Andrei

You should not add the proxy_cache_path directive in the Nginx custom template.

I am pretty sure the error notifications will disappear if you remove the proxy_cache_path parts from your custom Nginx template and move the relevant proxy_cache_path entries to a separate file in the /etc/nginx/conf.d directory.

A small tip: before creating a custom template, do a small test by creating a test domain and adding some directives (manually) in that domain and run nginx -t command.

Also note that it might not really be necessary to use custom Nginx templates:

- it is possible to do everything in the http block, which implies that one can simply add one or more .conf files in the /etc/nginx/conf.d directory
- one can use the proxy_cache_key directive to make a clear distinction between the Nginx caches of the various domains

However, it is not recommended to put everything in a http block (read: in .conf files located in /etc/nginx.conf.d), since that is for the (very) advanced Nginx user.

Hope the above helps a bit.

Regards........
 
Thanks for your reply.

I want to activate for each domain the caching option, so this why I want to create separate proxy_cache_path for each domain and also want it to be automatic: I add a new domain -> caching is on by default.
 
Back
Top