Original Post: (see update below)
I'd like to deliver html files, bypassing PHP altogether. PHP generates them and stores them in a directory, and if available, I want to serve them to the visitor.
My question is, how might I efficiently do this. The try_files method works, but is it really all that efficient? Every request to the site needs to check if the file exists first.
Here's my current proposed solution with try_files, but of course I'd like something more efficient. It directs to "/home/sys/example.com/cachepages/cats/re/red-cats.html" when the $http_host is "red-cats.example.com". I didn't provide an example for $mypathdogs, I just wanted to show that there could be different url paths pointing to different folders.
Here's the example code:
map $http_host $mypathcats {
default "nonexistent";
"~^(?<name1>.{2})(?<name2>.*)\.example\.com$" cachepages/cats/$name1/$name1$name2.html;
}
map $http_host $mypathdogs {
...another path here to cachepages/dogs/ files.
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name .example.com;
root /home/sys/example.com/;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $mypathcats $mypathdogs $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Update:
Updated to elaborate on my question. The html pages are created by PHP, so the first time they need to be accessed through PHP, and then subsequent visits will find the generated html file (if it's available) and access that directly, otherwise it will fall back to PHP which will then try to generate it. This is how my example works - it looks for a generated html file, if it doesn't exist yet it goes to PHP (which generates it so the next nginx request will find the html file and serve that instead).
Ideally I need a working sample code, something more efficient than my try_files approach.