配置nginx和apache的ssi模块,使其支持shtml格式的页面和include使用 -云主机博士

配置nginx和apache的ssi模块,使其支持shtml格式的页面和include使用 -云主机博士 第1张

什么是SSI?
SSI是英文Server Side Includes的缩写,翻译成中文就是服务器端包含的意思。从技术角度上说,SSI就是在HTML文件中,可以通过注释行调用的命令或指针。SSI具有强大的功能,只要使用一条简单的SSI命令就可以实现整个网站的内容更新,时间和日期的动态显示,以及执行shell和CGI脚本程序等复杂的功能。

include的是使用方法如下:红色部分是引用的文件路径!
<!--#include file="1.html" -->
<!--#include virtual="/yunzhujiboshi.com/html/1.html" -->

示例
<!--被包含文件与父文件存在于相同目录中。-->
<!--#include file="1.html" -->

<!--被包含文件位于脚本虚拟目录中。 -->
<!--#include virtual="/yunzhujiboshi/html/1.html" -->

include file 与include virtual的区别:
1.#include file 包含文件的相对路径,#include virtual包含文件的虚拟路径。

2. 在同一个虚拟目录内,<!--#include file="1.html"-->和<!--#include virtual="1.html"-->效果是相同的,但假设虚拟目录名为piaoyun,则<!--#include virtual="piaoyun/file.html"-->也可以通过调试,但我们知道<!--#include file="piaoyun/file.html"-->是绝对要报错的。

3.如果一个站点下有2个虚拟目录piaoyun1和 yunzhujiboshi2,yunzhujiboshi1下有文件file1.html,yunzhujiboshi2下有文件file2.html,如果file1.html要调用 file2.html,那么在file1.html中要这样写:<!--#include virtual="yunzhujiboshi2/file2.html"-->,在这种情况下用#include file是无法实现的,用<!--#include file="yunzhujiboshi2/file2.html"-->必然报错。相反,在yunzhujiboshi2的文件中包含yunzhujiboshi1中的文件也是一样。 如果该被包含文件在某个文件夹下面,只要在虚拟路径中加上该文件夹即可。

4.不论用#include file 还是 #include virtual,在路径中用“/”还是“/”或者二者交叉使用都不会影响编译效果,程序会顺利执行。

5. 以上情况不适用于2个站点文件的相互调用,而且在同一个站点内,<!--#include file="file.html"-->和<!--#include virtual="file.html"-->等效,但假设站点名为website,使用<!--#include virtual="website/file.html"-->是错误的。

nginx 内置了ssi模块,开启方法
修改 nginx 的配置文件

# vi /usr/local/nginx/conf/nginx.conf

在http{}中添加如下代码即可:
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
ssi_types text/htm;

ps.这里文件类型可以改成其他的,比如让htm也支持ssi,那么把text/shtml改成text/htm就行了,但是这样的话,由于服务器里htm的文件较多,也不一定全都用ssi引入其他页面,会给nginx造成一些不必要的消耗,所以不建议这么做。

在nginx.conf 的http里面加入如下四行(绿色部分的代码)
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
ssi_types text/htm;
server
{
listen 80;
server_name www.yunzhujiboshi.org;
index index.shtml;

apache下的配置方法:

如何使你的Apache服务器支持SSI?
1. 确认加载include.so模块,将注释去掉:
LoadModule include_module modules/mod_include.so

2. AddType部分去掉这两段注释:
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml

3. Directory目录权限里面找到
Options Indexes FollowSymLinks
增加Includes修改为:
Options Indexes FollowSymLinks Includes ExecCGI MultiViews
PS:这里的ExecCGI是因为我本机的apache是以FastCGI方式加载的,所以要添加这个!

具体的安装配置方法如下:
Windows7 64位系统下面配置Apache2.2+PHP5.3+mod_fcgid运行高效的FastCGI模式安装方法

4. 重新启动Apache,测试:
<!--#include file=head.html”-->
Holle Word!,这是中间的内容
<!--#include file=foot.html”-->
方法一:
添加 SSI 页面的新句柄,缺省情况下,Apache 即使在需要时也不对 HTML 文件进行解析。假如将 .shtml 作为所有包含一个或多个 SSI 指令的 HTML 页面的 SSI 文件扩展名,需要告诉 Apache 将 .shtml 文件扩展名视为需要进行 SSI 解析的页面。如果因为某些原因,将 .html 和 .htm 用做 SSI 扩展名,该这样使用:
AddHandler server-parsed .html
AddType text/html .html
AddHandler server-parsed .htm
AddType text/html .htm

方法二:
添加如下信息:
AddType text/html .ssi
AddOutputFilterByType INCLUDES;DEFLATE text/html
然后保存httpd.conf,重启apache。
如果开启了虚拟主机配置文件的话,也要在相对应的虚拟主机配置文件添加如下内容:

虚拟主机配置方式:(红色部分的内容)

<VirtualHost *:60>
ServerAdmin webmaster@phpcms
DocumentRoot "D:/wwwroot/yunzhujiboshi.com"
ServerName yunzhujiboshi.org
ErrorLog "logs/dummy-yunzhujiboshi.com-error.log"
CustomLog "logs/dummy-yunzhujiboshi.com-access.log" common
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI MultiViews
AllowOverride All
Order deny,allow
allow from all
</Directory>
<IfModule mime_module>
AddHandler server-parsed .html
AddType text/html .html
AddHandler server-parsed .htm
AddType text/html .htm
AddHandler server-parsed .php
AddType text/html .php
</IfModule>
</VirtualHost>

如果让php也支持SSI语句,可以在虚拟机配置和httpd.conf里面都加上一行,AddOutputFilter Includes .php,如下:

<Directory />
Options Indexes FollowSymLinks Includes
AllowOverride None
AddOutputFilter Includes .php
</Directory>

保存后,重起apache即可。
PS:我在本地按照这个方法设置,PHP始终是不支持includes呢,求各位大神的解决办法!如果有好的解决办法,请在博文后给我留言!
写在最后的话:
如果你在windows下安装有mod_gzip.so模块的话,这个会和ssi与include起冲突,取消就可以了!

宝塔服务器面板,一键全能部署及管理,送你3188元礼包,点我领取


iproyal.png
原文链接:,转发请注明来源!
「配置nginx和apache的ssi模块,使其支持shtml格式的页面和include使用 -云主机博士」评论列表

发表评论