南粤雨林 發表於 2026-1-13 00:00:00

linux中的nginx启用ssi实现include包含html文件

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>Nginx配置</li><li>Nginx实例</li><li>动态内容包含</li><li>virtual和file的区别<ul class="second_class_ul"><li>1. &zwnj;指令类型&zwnj;</li><li>2. &zwnj;路径解析对比&zwnj;</li><li>3. &zwnj;使用建议&zwnj;</li></ul></li></ul></div><p>SSI Server Side Include,是一种基于服务端的网页制作技术,大多数(尤其是基于Unix平台)的web服务器如Netscape Enterprise Server等均支持SSI命令。 它的工作原因是:在页面内容发送到客户端之前,使用SSI指令将文本、图片或代码信息包含到网页中。对于在多个文件中重复出现内容,使用SSI是一种简便的方法,将内容存入一个包含文件中即可,不必将其输入所有文件。通过一个非常简单的语句即可调用包含文件,此语句指示Web服务器将内容插入适当网页。而且,使用包含文件时,对内容的所有更改只需在一个地方就能完成。</p>
<p class="maodian"></p><h2>Nginx配置</h2>
<p>主要是三个参数,ssi,ssi_silent_errors和ssi_types,均可以放在http,server和location的作用域下。</p>
<blockquote><p>ssi on<br />开启ssi支持,默认是off</p>
<p>ssi_silent_errors on<br />默认值是off,开启后在处理SSI文件出错时不输出错误提示:&rdquo; &rdquo;</p>
<p>ssi_types<br />默认是ssi_types text/html,所以如果需要htm和html支持,则不需要设置这句,如果需要shtml支持,则需要设置:ssi_types text/shtml</p></blockquote>
<p class="maodian"></p><h2>Nginx实例</h2>
<p>开启shtml后缀的文件名支持ssi</p>
<div class="dxycode"><pre class="brush:bash;">server{
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
}</pre></div>
<p>开启html后缀的文件名支持ssi</p>
<p><img alt="" src="https://zhuji.jb51.net/uploads/allimg/20260113/1-260113102234228.png" /></p>
<div class="dxycode"><pre class="brush:bash;">server{
ssi on;
ssi_silent_errors on;
}</pre></div>
<p>只在fcbu_com目录下开启shtml后缀的文件名支持ssi</p>
<div class="dxycode"><pre class="brush:bash;">server{
location /fcbu_com/{
ssi on;
ssi_types text/shtml;
ssi_silent_errors on;
}
}</pre></div>
<p class="maodian"></p><h2>动态内容包含</h2>
<p>在HTML页面中可以通过以下命令包含另一个包含动态内容的页面:</p>
<pre>&lt;!--#include&nbsp;virtual=&quot;/bottom.shtml&quot;&nbsp;--&gt;或&lt;!--#include&nbsp;file=&quot;/bottom.shtml&quot;&nbsp;--&gt;</pre>
<p class="maodian"></p><h2>virtual和file的区别</h2>
<p>关于 Nginx SSI 中&nbsp;&lt;!--#include virtual=&quot;/bottom.shtml&quot; --&gt;&nbsp;和&nbsp;&lt;!--#include file=&quot;/bottom.shtml&quot; --&gt;&nbsp;指令的详细说明:</p>
<p class="maodian"></p><h3>1. &zwnj;指令类型&zwnj;</h3>
<ul><li><p>&zwnj;<strong>file&nbsp;指令</strong>&zwnj;</p>
<p>&lt;!--#include file=&quot;/bottom.shtml&quot; --&gt;</p>
<ul><li>&zwnj;<strong>作用</strong>&zwnj;:包含服务器文件系统中的文件(绝对路径)。</li><li>&zwnj;<strong>路径</strong>&zwnj;:从 Nginx 配置的&nbsp;root&nbsp;目录开始计算绝对路径(如&nbsp;/var/www/html/bottom.shtml)。</li><li>&zwnj;<strong>适用场景</strong>&zwnj;:适用于静态文件或需通过 Nginx 直接访问的文件。</li></ul></li><li><p>&zwnj;<strong>virtual&nbsp;指令</strong>&zwnj;</p>
<p>&lt;!--#include virtual=&quot;/bottom.shtml&quot; --&gt;</p>
<ul><li>&zwnj;<strong>作用</strong>&zwnj;:包含通过 Nginx 处理的虚拟路径(相对路径)。</li><li>&zwnj;<strong>路径</strong>&zwnj;:基于请求的上下文解析(如&nbsp;/bottom.shtml&nbsp;会被解析为&nbsp;http://example.com/bottom.shtml)。</li><li>&zwnj;<strong>适用场景</strong>&zwnj;:适用于需要通过 Nginx 重写规则或代理处理的资源。</li></ul></li></ul>
<p class="maodian"></p><h3>2. &zwnj;路径解析对比&zwnj;</h3>
<table><tbody><tr><th>指令类型</th><th>路径解析方式</th><th>示例</th></tr><tr><td>file</td><td>绝对路径(从&nbsp;root&nbsp;目录)</td><td>/var/www/html/bottom.shtml</td></tr><tr><td>virtual</td><td>相对路径(基于请求上下文)</td><td>http://example.com/bottom.shtml</td></tr></tbody></table>
<p class="maodian"></p><h3>3. &zwnj;使用建议&zwnj;</h3>
<ul><li>&zwnj;<strong>静态资源</strong>&zwnj;:优先使用&nbsp;file&nbsp;指令(性能更优)。</li><li>&zwnj;<strong>动态资源</strong>&zwnj;:使用&nbsp;virtual&nbsp;指令(支持 Nginx 重写规则)。</li><li>&zwnj;<strong>注意事项</strong>&zwnj;:<ul><li>file&nbsp;指令路径需确保 Nginx 有读取权限。</li><li>virtual&nbsp;指令路径需避免循环包含。</li></ul></li></ul>
頁: [1]
查看完整版本: linux中的nginx启用ssi实现include包含html文件