關於Nginx的SSI(包含路徑)
若是shtml裏面的網頁代碼包含語句寫成以下:
<!--#include virtual="/test.html"-->
這樣是沒有問題,能夠包含的,可是若是寫成這樣:html <!--#include virtual="../test.html"-->因爲須要包含當前代碼文件所在目錄路徑的上級目錄文件,nginx會爲此請求產生的子請求uri爲/../test.html,默認nginx會認爲這個uri並非安全的,日誌(error_log)會輸入以下錯誤:nginx unsafe URI "/../test.html" was detected while sending response to client, client: 192.168.10.204, server: test.aijuzhe.cn, 不能正確包含文件,頁面會輸出[an error occurred while processing the directive],解決方法是找到nginx源代碼目錄的unsafe uri檢查函數並強制使其返回一個NGX_OK,即以下文件:ide vi nginx-VERSION/src/http/ngx_http_parse.c找到ngx_http_parse_unsafe_uri函數,並在前面加入一句return NGX_OK;函數 ngx_int_tngx_http_parse_unsafe_uri(ngx_http_request_t *r, ngx_str_t *uri,ngx_str_t *args, ngx_uint_t *flags){return NGX_OK; /*這一句是後面加的*/u_char ch, *p;size_t len;len = uri->len;p = uri->data;if (len == 0 || p[0] == '?') {goto unsafe;}if (p[0] == '.' && len == 3 && p[1] == '.' && (p[2] == '/'#if (NGX_WIN32)|| p[2] == '\\'#endif)){goto unsafe;}for ( /* void */ ; len; len--) {ch = *p++;if (usual[ch >> 5] & (1 << (ch & 0x1f))) {continue;}if (ch == '?') {args->len = len - 1;args->data = p;uri->len -= len;return NGX_OK;}if (ch == '\0') {*flags |= NGX_HTTP_ZERO_IN_URI;continue;}if ((ch == '/'#if (NGX_WIN32)|| ch == '\\'#endif) && len > 2){/* detect "/../" */if (p[0] == '.' && p[1] == '.' && p[2] == '/') {goto unsafe;}#if (NGX_WIN32)if (p[2] == '\\') {goto unsafe;}if (len > 3) {/* detect "/.../" */if (p[0] == '.' && p[1] == '.' && p[2] == '.'&& (p[3] == '/' || p[3] == '\\')){goto unsafe;}}#endif}}return NGX_OK;unsafe:ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,"unsafe URI \"%V\" was detected", uri);return NGX_ERROR;}從新編譯之後nginx能夠包含上級目錄的文件,固然,帶來的後果是安全性的降低。ui |