【问题标题】:serve different content according to header value in Nginx根据 Nginx 中的标头值提供不同的内容
【发布时间】:2017-04-15 03:22:37
【问题描述】:

我在以下拱门后面有一些网络内容,为此我需要为桌面和移动客户端提供不同的内容。

客户端 > AWS Cloudfront > Nginx > ...

cloudfront 具有识别用户代理的内置功能,他们将其统一为 4 个特殊标头(例如 CloudFront-Is-Desktop-Viewer),它们是真或假。

在 Nginx 上,我正在尝试根据这些标头来决定要提供哪些内容。 例如:

 location / {
   if ($http_CloudFront-Is-Desktop-Viewer = true) {
        proxy_pass http://upstream;
        break;
   }
   root /var/www/static/en-US;
   try_files $uri /index.html;
 }

所以在上述情况下,如果 CloudFront-Is-Desktop-Viewer 值为 true,它应该转到上游,如果它为 false 或不存在,则从 nginx 获取静态文件。

但由于某种原因,我总是从 nginx 获取静态文件。 我确定这个标头正在被转发,我什至尝试使用标头修饰符直接从 chrome 发送它。

我错过了什么?

非常感谢

【问题讨论】:

    标签: web-services nginx web http-headers amazon-cloudfront


    【解决方案1】:

    试试这个变量:

    $http_cloudfront_is_desktop_viewer

    From manual about embedded variables:

    $http_name nginx

    任意请求头域;变量名的最后一部分是 字段名称转换为小写,用破折号替换 下划线

    【讨论】:

      猜你喜欢
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 2018-07-19
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多