让nginx 的ssi支持include相对路径

好久没接触nginx了,今天帮同事解决一个客户的问题,顺便记录下;

version : nginx-1.6.2

问题描述:

客户的files.shtml里面include一个网站的头部文件( <!–# include file=”../../woqu.htm”–>),采用的是相对路径,这样的写法在apache里面可以正常访问,但是在nginx里面不能正常加载

日志上提示:

unsafe URI "../../woqu.html" was detected while sending response to client

解决方法:

修改源码重新编译nginx

vi src/http/modules/ngx_http_ssi_filter_module.c

注视掉下面这个判断:

/*

if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {

return NGX_HTTP_SSI_ERROR;

}

*/

然后安装三部曲,reload一下就搞定了。

顺带说下,部分人在nginx里面为了安全会添加规则禁止访问隐藏文件(点 .开头),如:

location ~ (.*\.sh?$|/\.|.*\.log?$)

{

return 403;

}

然后就会跟我们上面改的起冲突,因为include ../../test.html 这样的时候,其实也是正常请求这样的一个URL,“..”也是点开头,然后你就会看到一个403的页面,好恶心。