我们都知道, 当我们的页面请求一个 js 文件 一个 cs 文件或者点击到其他页面, 浏览器一般都会给这些请求头加上表示来源的 Referrer 字段 Referrer 在分析用户的来源时非常有用, 比如大家熟悉的百度统计里面就利用到 Referrer 信息了 但是遗憾的是, 目前百度统计仅仅支持来源于 http 页面的 referrer 头信息 ; 也就是说, 如果你网站是 http 的, 那么百度统计只能统计到从其他 http 页面进入到你网站的 referrer, 而如果是从 https 页面进入到你的网站, 百度统计目前是无法统计到 在介绍解决办法之前, 我们先来了解 Referrer Policies Referrer Policies 如果想及时了解 Spark Hadoop 或者 Hbase 相关的文章, 欢迎关注微信公共帐号 :iteblog_hadoop 目前存在九种 Referrer Policies, 包括 : 空字符串 no-referrer, no-referrer-whendowngrade, same-origin, origin, strict-origin, origin-when-cross-origin, strict-origin-whencross-origin 以及 unsafe-url: enum ReferrerPolicy { "", "no-referrer", "no-referrer-when-downgrade", "same-origin", "origin", "strict-origin", "origin-when-cross-origin", "strict-origin-when-cross-origin", 1 / 5
"unsafe-url" }; 我们这对每种 Referrer Policies 进行介绍 空字符串 空字符串表示没有 referrer 策略 比如我们有一个 HTML 标签元素, 而且并没有声明任何的 Referrer Policies, 这时候的 Referrer Policies 就是空字符串 默认的空字符串 Referrer Policies 就等同于 no-referrer-whendowngrade no-referrer 最简单的 Referrer Policies 就是 no-referrer ; 这种 Referrer Policies 会导致所有的请求都不带 referrer 信息 比如如果 https://www.iteblog.com/archives/1929 页面将 Referrer Policies 设置成 no-referrer, 那么从这个页面转到 https://www.iteblog.com/( 或者其他任何页面 ) 都不会发送 Referer 头信息 no-referrer-when-downgrade 这种策略在从受 TLS 保护的 URL 跳转到任何潜在可信的 URL 以及从不受 TLS 保护的 URL( 比如 H TTP) 跳转到任何 URL 都会发送完整的 URL Referrer 信息 但是如果从受 TLS 保护的 URL( 比如 HTTPS) 跳转不受信任的 URL( 比如 HTTP), 那么 Referr er 头将不会带任何的信息 这是大多数浏览器默认的 Referrer Policies 比如 : 假设 https://www.iteblog.com/archives/1929 页面的 Referrer Policies 设置成 noreferrer-when-downgrade, 那么从这个页面跳转到任何的 https 页面将会带上值为 https://www.iteblog.com/archives/1929 的 Referer 头信息 ; 但是如果从那个页面跳转到任何 HTTP 页面, 将不会带上任何的 Referer 头信息 这就是为什么百度统计无法获取到从 HTTPS 跳转到 HTTP 的信息 same-origin 对于同源的链接, 会发送 referrer, 其他的不会 同源意味着域名需要相同,iteblog.com 和 idea.iteblog.com 是非同源的 origin 2 / 5
这种 Referrer Policies 对于任何资源 ( 包括可信和不可信 ) 来说只发送网站的根域名, 不发送完整的 url 比如 :https://www.iteblog.com/archives/1929 页面的 Referrer Policies 设置成 origin, 那么从这个页面跳转到任何网站将会带上值为 https://www.iteblog.com/ 的 Referer 头信息 strict-origin 我们可以把他看做是 origin 和 no-referrer-when-downgrade 的结合体 也就是说, 如果从受 TLS 保护的 URL 跳转到任何潜在可信的 URL 以及从不受 TLS 保护的 URL( 比如 HTTP) 跳转到任何 U RL 都会发送根域名的 Referrer 头信息 但是如果从受 TLS 保护的 URL( 比如 HTTPS) 跳转不受信任的 URL( 比如 HTTP), 那么 Referr er 头将不会带任何的信息 比如 https://www.iteblog.com/archives/1929 页面的 Referrer Policies 设置成 strict-origin, 那么从它跳转到 https://xxx.iteblog.com 将会带上值为 https://www.iteblog.com/ 的 Referer 头信息 ; 如果从那个页面跳转到 http://idea.iteblog.com 将不会带上任何的 Referer 头信息 再比如 :http://idea.iteblog.com 页面的 Referrer Policies 设置成 strictorigin, 那么从这个页面跳转到 http://books.iteblog.com 或者 https://www.iteblog.com/archives/1929 将会带上值为 http://idea.iteblog.com/ 的 Referer 头信息 不过目前这种 Referrer Policies 浏览器可能不支持 origin-when-cross-origin 该策略在同源的链接中发送完整的 URL, 其他情况仅发送根域名 需要注意的是 https://www.iteblog.com 和 https://www.iteblog.com 被认为是非同源的 比如 https://www.iteblog.com/archives/1929 页面的 Referrer Policies 设置成 origin-whencross-origin, 那么从它跳转到 https://www.iteblog.com/archives/1464 将会带上值为 https://www.iteblog.com/archives/1929 的 Referer 头信息 ; 如果从那个页面跳转到 http://idea.iteblog.com/ 页面 ( 注意这里 HTTP 和 HTTPS 页面都可以 ), 将会带上值为 https://ww w.iteblog.com/ 的 Referer 头信息 不过这种 Referrer Policies 在 Chrome 实现的时候被误写成了 origin-when-crossorigin strict-origin-when-cross-origin 这种策略可以看做是 origin-when-cross-origin 和 no-referrer-when-downgrade 的结合体 对于同源请求, 发送完整的 URL; 对于同为 https 的页面, 或者是从 HTTP 页面跳转到任何的页面 ( 包括 HTTP 和 HTTPS) 只发送根域名 ; 如果是从 https 页面跳转到 http 将不发送 referrer If a document at https://example.com/page.html sets a policy of "strict-origin-whencross-origin", then navigations to https://example.com/not-page.html would send a 3 / 5
Referer header with a value of https://example.com/page.html. Navigations from that same page to https://not.example.com/ would send a Referer header with a value of https://example.com/. Navigations from that same page to http://not.example.com/ would send no Referer header. unsafe-url 无论是否发生协议降级 (HTTPS 到 HTTP), 无论是本站链接还是站外链接, 统统都发送 Referrer 信息 这是最宽松而最不安全的策略 ; 比如 :https://www.iteblog.com/archives/1929 页面的 Referrer Policies 设置成 unsafe-url, 那么从它跳转到 http://idea.iteblog.com/ 页面 ( 注意这里 HTTP 和 HTTPS 页面都可以 ), 将会带上值为 https://www.iteblog.com/archives/1929 的 Referer 头信息 如何设置 页面是设置 Referrer Policies 主要有以下几种 CSP 响应头 CSP(Content Security Policy), 是一个跟页面内容安全有关的规范 在 HTTP 中通过响应头中的 Content-Security-Policy 字段来告诉浏览器当前页面要使用何种 CSP 策略 : Content-Security-Policy: referrer 我们还可以在里面设置 : <meta http-equiv="content-security-policy" content="referrer"> <meta> 标签 通过标签也可以指定 Referrer 策略, 同样很简单 : <meta name="referrer" content="no-referrer"> 4 / 5
Powered by TCPDF (www.tcpdf.org) 我们把 <meta> 放在 <head>...</head> 之间 注意, 如果出现的位置不对会被忽略 同样, 如果没有给它定义 content 属性, 或者 content 属性为空, 也会被忽略 如果 content 属性不是合法的取值, 浏览器会自动选择 noreferrer 这种最严格的策略 <a> 标签的 referrer 属性 通过给 <a> 标签增加 referrer 属性也可以指定 Referrer 策略, 格式如下 : <a href="https://www.iteblog.com" referrer="no-referrer"> 过往记忆 </a> 这种方式作用的只是这一个链接 并且,<a> 标签可用的 Referrer 策略只有三种 :noreferrer origin 以及 unsafe-url 另外, 这样针对单个链接设置的策略优先级比 CSP 和要高 解决百度统计无法获取 https 页的 referrer 说到这里, 如果我们想让百度统计能够统计到来源 HTTPS 的页面, 我们有两种方式 :(1) 将自己的网站也升级到 HTTPS( 参考 在 Nginx 中使用 Let's Encrypt 免费证书配置 HTTPS );(2) 让对方网站设置相关的 Referrer Policies 本博客文章除特别声明, 全部都是原创! 转载本文请加上 : 转载自过往记忆 (https://www.iteblog.com/) 本文链接 : () 5 / 5