Score:0

Multiple nested HTML include directives with a nginx server

us flag

I have index.html:

<!--#include virtual="/includes/Framework.inc"-->

Inside Framework.inc I have:

<!--#include file="/includes/HTML.inc"-->

However, when I open the page for index.html, after viewing the source code, I see the nested include did not happen and see the full include directive:

<!--#include file="/includes/HTML.inc"-->

How can we make sure nginx supports multiple levels of HTML includes?

Score:2
gr flag

Nginx does support nested SSI includes. However the SSI module processes only the text/html MIME type content by default, and your .inc files are most likely returned with something else in the Content-Type response HTTP header. You can adjust your global MIME types to include .inc extension or specify additional MIME types to process with the ssi_types directive, however I'd rather define an additional location for the /includes/ directory content:

location /includes/ {
    internal;
    ssi on;
    types {
        text/html  html htm inc;
    }
}
MightyMouse avatar
us flag
Hello Ivan. I have a follow-up question on this. Can we do a SSI on PHP files? The last node/include tries to include test.php, which contains <?php echo(12345); ?>. But all we see in the source code is the content of test.php. I did try to tweak the types: ssi on; types { text/html html htm inc php; }
Ivan Shatsky avatar
gr flag
You are not limited to static content, SSI can be applied to any location, even those that have an HTTP `proxy_pass` or FCGI `fastcgi_pass` content handlers. However if you want to include content from those locations, you should do it is using a `virtual` subrequest, and (obviously) SSI should be turned on for those locations.
MightyMouse avatar
us flag
oh la la, new topics there. lol virtual subrequest eh?..interesting monster whatever that is...
Ivan Shatsky avatar
gr flag
This is a static file inclusion (using a filename): `<!--#include file="HTML.inc"-->`. This is an inclusion of subrequest response (using an URI): `<!--#include virtual="/test.php"-->`. Subrequests is an internal nginx feature, widely used by a couple of modules (the most known example maybe an `auth_request_module`); the same internal nginx API is used by SSI `virtual` includes. `/test.php` here is an URI that should go through the default PHP handler location (usually something like `location ~ \.php$ { ... }`) to be interpreted via PHP-FPM.
MightyMouse avatar
us flag
Genius! Wow, that worked. The include against a PHP file inside a virtual directory does not work and pulls the content of the PHP file. I did this, which may be overkill: location /includes/ { alias "Z:/Virtual Folders/Includes/"; ssi on; types { text/html html htm inc php; } location ~ \.php$ { ssi on; types { text/html html htm inc php; } fastcgi_param SCRIPT_FILENAME $request_filename; } }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.