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. ‌指令类型‌</li><li>2. ‌路径解析对比‌</li><li>3. ‌使用建议‌</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文件出错时不输出错误提示:” ”</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><!--#include virtual="/bottom.shtml" -->或<!--#include file="/bottom.shtml" --></pre>
<p class="maodian"></p><h2>virtual和file的区别</h2>
<p>关于 Nginx SSI 中 <!--#include virtual="/bottom.shtml" --> 和 <!--#include file="/bottom.shtml" --> 指令的详细说明:</p>
<p class="maodian"></p><h3>1. ‌指令类型‌</h3>
<ul><li><p>‌<strong>file 指令</strong>‌</p>
<p><!--#include file="/bottom.shtml" --></p>
<ul><li>‌<strong>作用</strong>‌:包含服务器文件系统中的文件(绝对路径)。</li><li>‌<strong>路径</strong>‌:从 Nginx 配置的 root 目录开始计算绝对路径(如 /var/www/html/bottom.shtml)。</li><li>‌<strong>适用场景</strong>‌:适用于静态文件或需通过 Nginx 直接访问的文件。</li></ul></li><li><p>‌<strong>virtual 指令</strong>‌</p>
<p><!--#include virtual="/bottom.shtml" --></p>
<ul><li>‌<strong>作用</strong>‌:包含通过 Nginx 处理的虚拟路径(相对路径)。</li><li>‌<strong>路径</strong>‌:基于请求的上下文解析(如 /bottom.shtml 会被解析为 http://example.com/bottom.shtml)。</li><li>‌<strong>适用场景</strong>‌:适用于需要通过 Nginx 重写规则或代理处理的资源。</li></ul></li></ul>
<p class="maodian"></p><h3>2. ‌路径解析对比‌</h3>
<table><tbody><tr><th>指令类型</th><th>路径解析方式</th><th>示例</th></tr><tr><td>file</td><td>绝对路径(从 root 目录)</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. ‌使用建议‌</h3>
<ul><li>‌<strong>静态资源</strong>‌:优先使用 file 指令(性能更优)。</li><li>‌<strong>动态资源</strong>‌:使用 virtual 指令(支持 Nginx 重写规则)。</li><li>‌<strong>注意事项</strong>‌:<ul><li>file 指令路径需确保 Nginx 有读取权限。</li><li>virtual 指令路径需避免循环包含。</li></ul></li></ul>
頁:
[1]