$ http_referer的行为类似于$ document_uri

Ekk*_*tan 10 nginx

我需要caller在ssi include中设置param作为当前uri,但$ document_uri奇怪地工作

带有ssi-include的模板部分:

<div class="panel">
    <div class="ym-wrapper">
      <div class="ym-wbox">
        <!--# set var="panel"
          value="<!--# include virtual='/panel/?project=project_name&color=dark&caller=$http_referer' -->" -->
        <!--# echo var="panel" encoding="none" -->
      </div>
    </div>
  </div>
Run Code Online (Sandbox Code Playgroud)

确切地说$http_referer,不是$document_uri

Nginx日志格式:

log_format  subtimed  '$remote_addr - $remote_user [$time_local] '
                      '"INC $uri$is_args$args" $status $bytes_sent "$http_referer" '
                      '"$http_user_agent"
Run Code Online (Sandbox Code Playgroud)

/ panel /的Nginx位置:

location /panel/ {
            internal;
            ssi  on;
            proxy_set_header        Host panel.domain.zone;
            proxy_method            GET;
            proxy_pass              http://panel.domain.zone/;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_connect_timeout   1s;
            proxy_send_timeout      2s;
            proxy_read_timeout      2s;
            proxy_intercept_errors  on;
            log_subrequest on;
    }
Run Code Online (Sandbox Code Playgroud)

我打开页面http://devel.domain.zone,我在nginx日志中看到:

xxx.xxx.xxx.xxx - - [15/Oct/2015:18:29:31 +0300] "INC /panel/?project=project_name&color=dark&caller=http://devel.domain.zone" 200 0 "http://devel.domain.zone/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0"
Run Code Online (Sandbox Code Playgroud)

好的,请转到页面http://devel.domain.zone/sec/?theme=102 Nginx日志:

xxx.xxx.xxx.xxx - - [15/Oct/2015:18:30:29 +0300] "INC /panel/?project=project_name&color=dark&caller=http://devel.domain.zone/sec/?theme=102" 200 0 "http://devel.domain.zone/sec/?theme=102" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0"
Run Code Online (Sandbox Code Playgroud)

事实证明,在$http_referer类似的行为$document_uri.所以,如果我设置$document_uri,而不是$http_referer在SSI包括我nginx的日志是这样看:

xxx.xxx.xxx.xxx - - [15/Oct/2015:18:30:29 +0300] "INC /panel/?project=project_name&color=dark&caller=/ga/ga.js" 200 0 "http://devel.domain.zone/sec/?theme=102" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0"
Run Code Online (Sandbox Code Playgroud)

WTF?!! 在什么时候计算$document_uri变量?如何将变量传递给include:作为名称还是作为值?

cns*_*nst 0

目前尚不完全清楚你在问什么。你做了什么? 你期待什么? 你得到了什么?

根据http://nginx.org/r/%24document_uri$document_uri与 just 相同$uri(两者都不应该与 混淆$request_uri,这是一个完全不同的变量)。

$uri反过来,该变量也有非常清晰的记录(在http://nginx.org/r/%24urilocation )——用我自己的话来说,它本质上是 nginx /状态机的状态——你所知道的rewrite所有内部状态。rewrite执行改变这个变量,它基本上一直在被重写的过程中。另一个重要的一点是,除非您自己修改这个变量,否则它也会丢失$is_args$args其中的部分,并且它也是标准化的,就像location匹配一样,因为location匹配实际上完全针对 完成的$uri

顺便说一句,对于一些你可以用$uri一堆rewrite指令来做的有趣的事情,看看http://mdoc.su/(一定要看看它nginx.conf!)和一个更简单但仍然值得注意和有用的代码示例ServerFault 上也提供:“如何通过 nginx.conf 在 Google 网站管理员工具上验证站点所有权?”

如果您对如何在 C 中实现这些东西更好奇,您可以从以下位置开始您的旅程: