【问题标题】:The $http_referer behaves like the $document_uri$http_referer 的行为类似于 $document_uri
【发布时间】:2016-01-27 23:41:33
【问题描述】:

我需要将 ssi 中的 caller 参数设置为当前 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>

没错,$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"

/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;
    }

我打开页面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"

好的,转到页面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"

原来$http_referer 的行为类似于$document_uri... 所以,如果我在 ssi include 中设置 $document_uri 而不是 $http_referer,我会在 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"

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

【问题讨论】:

  • 仅供参考:不知道你在问什么!你做了什么,你期望什么,你得到了什么?
  • ping。你能澄清我的答案是否遗漏了什么吗?如果没有,如果它被接受/承诺赏金,我将不胜感激。 :-)

标签: nginx


【解决方案1】:

您的要求并不完全清楚。你做了什么? 你期待什么?你得到了什么?

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

反过来,$uri 变量也很清楚地记录在案(在http://nginx.org/r/%24uri)——用我自己的话来说,它本质上是 nginx location/rewrite 状态机的状态——所有内部你执行的rewrites 更改了这个变量,它基本上总是在被重写的过程中。另一个重要的一点是,除非您自己修改此变量,否则它也缺少其中的 $is_args$args 部分,并且它也是 规范化,就像 location 匹配一样,因为 location 匹配实际上完全针对$uri

顺便说一句,对于一些你可以用$uri 和一堆rewrite 指令做的有趣的事情,看看http://mdoc.su/(一定要看看它的nginx.conf!)和一个更简单的但是,on ServerFault: "How do I verify site ownership on Google Webmaster Tools through nginx.conf?" 也提供了值得注意且有用的代码示例。

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

【讨论】:

  • @TobySpeight,是您根据答案的实际 content 投下反对票,或者更多的是开头段落,“您要问什么并不完全清楚。 “?如果我的回答不够充分,但问题本身与投票支持的一样好,那么为什么没有其他人对可接受的答案有任何线索,甚至是一个遥远的估计?尤其是在赏金之后?这个答案的 -2 票是完全荒谬和不必要的!
  • 我没有投反对票;我被要求审查这个,第一段看起来像是在要求澄清。仔细阅读后,这次似乎是我错了,我撤回评论并给你一个upvote。请查看您的网址 - 我认为您需要在几个地方将 $ 编码为 %24
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多