之前介绍过如何让运行在apache服务器下的html文件支持ssi,方法一 方法二,这里介绍一下用nginx服务器,如何支持ssi包含文件。

主要是三个参数,ssi,ssi_silent_errors和ssi_types,均可以放在http,server和location的作用域下。

ssi on
开启ssi支持,默认是off

ssi_silent_errors on
默认值是off,开启后在处理SSI文件出错时不输出错误提示:"[an error occurred while processing the directive] "

ssi_types
默认是ssi_types text/html,所以如果需要htm和html支持,则不需要设置这句,如果需要shtml支持,则需要设置:ssi_types text/shtml

SSI的格式:
<!--#include file="bottom.htm"-->

<!--#include virtual="/hx/bottom.htm"-->
路径是相对server中root根目录。

更多请参见官方文档:http://wiki.nginx.org/NginxChsHttpSsiModule

示例:

1.开启shtml后缀的文件名支持ssi
server{
......
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
}

2.开启html后缀的文件名支持ssi
server{
......
ssi on;
ssi_silent_errors on;
}

3.在zt目录下开启html后缀的文件名支持ssi
server{
......
location /hx/{
ssi on;
ssi_silent_errors on;
}
}

 

举个例子:

在nginx.conf 的http里面加入如下三行

server
        {
                listen       80;
                server_name www.benet.com;
                index index.html index.shtml index.php;
                root  /home/www;
                ssi on;
                ssi_silent_errors on;
                ssi_types text/shtml;
                location ~ .*\.(php|php5)?$
                        {
                                fastcgi_pass  unix:/tmp/php-cgi.sock;
                                fastcgi_index index.php;
                                include fcgi.conf;
                        }

                location /status {
                        stub_status on;
                        access_log   off;
                }
}

在index.shtml 页面里加入如下代码, 注意一定要用.shtml

<!--# include virtual="phpinfo.php" -->

然后访问www.benet.com/index.shtml 时它就会显示www.benet.com/phpinfo.php 的内容。