1

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.

  • I use a slightly different set and it works well for me. I have 6 domains here. I have Nginx as a reverse proxy with Apache in the background. Apache does the php stuff and Nginx caches all the sites. I also prime the cache with a script to load everything. My cache is in a ram drive so it's super fast but it disappears after reboot when I prime it again. – Admiral Noisy Bottom Apr 28 at 6:09
  • You could ask Nginx to manage the cache for you. See this document. – Richard Smith Apr 28 at 6:25
  • Richard, yes I could and that may be more efficient. I haven't yet looked into how I would have nginx handling caching php pages instead of php generating html files and then serving them - this is one option. Although, I'd prefer one in-line with my current solution of PHP generating the files and then nginx serving HTML files directly instead of hitting PHP, when available. – NAMAssist Apr 28 at 8:34
1

You are overthinking this. The most basic job of nginx is serving static content. For already generated content you only need this:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name .example.com;
    root /home/sys/example.com/;
}

Of course, you need to provide your certificates for SSL to work. index defaults to index.html, if you don't need anything else you can omit it. The add_header directives are also optional.

| improve this answer | |
  • I've up voted your answer because "overthinking" things is a common mistake and can create more problems than it solves. – Admiral Noisy Bottom Apr 28 at 6:47
  • This answer does not work for wildcard subdomains as per my example where the content needs to be generated first (I will update my question to clarify more). 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 and access that directly, instead. – NAMAssist Apr 28 at 8:29
  • Your question was not clear about when the html is generated by php. Usually this is done only when content has changed, outside of the web environment. I'll leave the answer as it is, for other people who might be looking for a similar problem. – Gerald Schneider Apr 28 at 8:50
  • I see. Well, I've updated the question - unfortunately I don't think I'll get the answer I was looking for now though. Is it OK to repost a similar question (rephrased, of course, to make better sense this time) - I'm new to Serverfault... – NAMAssist Apr 29 at 14:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.