• 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.

Resolved Configure Nginx to cache PHP generated files from PHP-FPM engine

Pejmanb

New Pleskian
Guys,

Happy new year
This is my first post in this forum.

Today i have found a problem in configuring nginx for caching html files that are generating from Apache FPM engine.

I first used this code and added it to /var/www/vhosts/system/DOMAINNAME/conf/vhost_nginx.conf.


fastcgi_cache_path /tmp/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {

server_name "DOMAINNAME";
root "/var/www/vhosts/DOMAINNAME/httpdocs";



set $no_cache 0;

if ($request_uri ~* "/(admin/)")
{
set $no_cache 1;
}

location ~ ^/(index)\.php(/|$) {
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m; # Only cache 200 responses, cache for 60 minutes
fastcgi_cache_methods GET HEAD; # Only GET and HEAD methods apply
add_header X-Fastcgi-Cache $upstream_cache_status;
fastcgi_cache_bypass $no_cache; # Don't pull from cache based on $no_cache
fastcgi_no_cache $no_cache; # Don't save to cache based on $no_cache

# Regular PHP-FPM stuff:
include fastcgi.conf; # fastcgi_params for nginx < 1.6.1
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:///var/www/vhosts/system/DOMAINNAME/php-fpm.sock;
fastcgi_index index.php;

}

}


but nginx throws an error that this code is not allowed in this directive.


Then i tried to insert it in this path /etc/nginx/conf.d

It works and ngnix can load the configuration but when i check the header i see nginx didn't cache any html files.
Can you help me to solve it? how can i setup nginx to cache output of dynamic php files ?
 
Hello Pejmanb,

Merry Christmas and Happy New Year.

To enable caching static files by nginx make sure to enable nginx support by Plesk /usr/local/psa/admin/bin/nginxmng -e then set `Serve static files directly by nginx` under Subscriptions - example.com - web server settings.
Also you can specify required directives under `Additional nginx directives` at Subscriptions - example.com - web server settings.
 
Hello ipilipenko,

Thanks for reply and Happy your new year too.

I did it and static files are serving directly via Nginx but i want to cache HTML files that are the output of PHP files just like Varnish.
I used code above but caching is not working yet.
Do you know where should i exactly insert the code for configuring Nginx ? the path i mean.
 
@Pejman,

You have 2 basic options (Redis & Memcache) to achieve what you want, one of them is a basic (and default) Nginx module: memcache module for Nginx.

Both of the options are not supported by the Nginx compilation, as provided with Plesk.

However, note that it is not optimal and/or not meaningful to have Nginx do the caching in a Apache + Nginx stack, where Nginx is a pure (reverse) proxy.

In essence, Nginx can benefit the most from caching if Nginx will be used as a stand-alone (lightweight) webserver (and not as a reverse proxy).

As long as you have dynamic pages (php for example) to be rendered, you still have to use Apache to some degree (even though it is possible to let Nginx handle all requests, it does not really work well in the stack provided by Plesk).

The latter simply implies that you will do best to leave the caching to Apache + FPM (sub)stack and leave Nginx to be a reverse proxy.

The interesting part about this is that you can easily add Redis to the Apache + FPM (sub)stack (note that Redis should be preferred over Memcache, since Memcache has limitations).

With some simple lines of code, you can control which php pages (or components thereof) should be passed to Redis.

The most interesting part about the whole approach is that the performance of your Apache + FPM + Redis, with a Nginx reverse proxy, is lightning fast and without any change in Nginx binaries (i.e. you can keep the default Nginx binary provided with Plesk) and without any change in configuration files (that can become very messy, with performance penalties).

Finally note that I have compiled a redis module for PHP 7.0.1, you can find it on: http://talk.plesk.com/threads/apcu-in-php-7-0.336446/#post-794256

This redis module for Apache allows you to use the phpredis style of commands in your php files.

Anyway, hope the above helps!

Regards....
 
Hello ipilipenko

As you explain above, and i'm not a programmer. I have 2 question as below:
1. To enable caching static files by nginx make sure to enable nginx support by Plesk /usr/local/psa/admin/bin/nginxmng -e
When i 've login by FTP to /usr/local/psa/admin/bin/ , I can see the folder nginxmng and the question is, What is the meaning of nginxmng-e ? OR What i should to do with folder nginxmng ?

2. Also you can specify required directives under `Additional nginx directives` at Subscriptions - example.com - web server settings.
Now, I've Debian 8.3‬ and Plesk version:12.5.30 Update #32
As i need to Configure Nginx to cache PHP generated files from PHP-FPM engine
And my static file is in /var/www/vhosts/example.com/httpdocs/wp-content/uploads/avatars ,
What is the best script to put into `Additional nginx directives` ?

Regards,
 
Sorry again, For more information. I've static file as the image of member more than 5 million images.
 
Hi cookkoo,

pls. be informed about some basic linux knowledge:

1. The "binary" ( executable, similar to windows *.exe - files ) nginxmng is a script, which enables, disables, or just displays the status of the proxy usage in Plesk. Files located in "bin" or "sbin" - folders always contain executables for linux. Pls. see as well the following KB - article:


You have TWO options to execute this binary, either over the Plesk Control Panel ( Tools & Settings > Services Management ), or over the command line ( you have to login as user "root", with a ssh - client - for example "putty", if you use windows => http://lmgtfy.com/?q="putty" ). Be noted, that you can't execute binaries over a FTP - client.


2. You asked for
What is the best script to put into `Additional nginx directives` ?

... well... there is no script for that. You are searching for a working "nginx directive", which really depend on your web-content. In your case, you use wordpress and several plugins, so your configuration is unique and there is no general solution. You really got lots of hints, solutions and work-arounds in the thread: NGiNX - browser caching for static resources
Is there a reason, that you didn't follow the suggestions to achieve your goal? And is there a reason, why you don't continue at this thread with your very same issue?
 
Back
Top