【问题标题】:nginx - add_header gets overwrittennginx - add_header 被覆盖
【发布时间】:2017-09-22 02:44:13
【问题描述】:

我很难理解 nginx 何时会覆盖我的 add_header 指令。

我有以下几点:

include /etc/nginx/vhost.d/ihc.example.com;
location = /auth {
    proxy_pass http://sso.example.com/auth/login;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
        auth_request /auth;
        auth_request_set $saved_set_cookie $upstream_http_set_cookie;
        add_header Set-Cookie $saved_set_cookie; #I don't see this header in the response
        proxy_pass http://ihc.example.com;
        include /etc/nginx/vhost.d/ihc.example.com_location;
}

我遇到的问题是auth_request_set $saved_set_cookie $upstream_http_set_cookie; cookie 没有出现在响应中。

现在,如果我删除 include /etc/nginx/vhost.d/ihc.example.com_location; 行,它会出现,但此文件包含一个共享的 CORS 修复程序,我非常非常想保留它:

if ($cors = 'trueGET') {
    add_header 'Access-Control-Allow-Origin' $http_origin; #I see this header in the response
[...]

如何在不必在每个位置重复 CORS 标头的情况下实现两者?是什么导致它被覆盖?当我阅读文档时,location 应该会覆盖 server,但在这种情况下,是什么导致了这个新的“范围”?

【问题讨论】:

  • 好的,我正在删除我的答案,因为我认为我误解了这个问题。如果/当我有时间回顾它并且可以弄清楚发生了什么时,将发布一个新的。希望同时这里的其他人能打败我..
  • @sideshowbarker:可能是我不清楚。我不确定我是否完全理解这里发生了什么。我会再调查一下。
  • 仍然不确定发生了什么。我将 CORS 标头直接复制到 location / 块中,现在一切正常。在我看来,ifinclude 都算作一个单独的范围。我希望有人能详细说明。
  • stackoverflow.com/questions/29467671/…的答案有一个可能的解决方案,相当于使用Lua nginx模块github.com/openresty/lua-nginx-module。因此,如果这在您的情况下是可行的,您可能想探索一下。同时,通过进一步阅读,我很清楚,鉴于if 条件在 nginx 中的工作方式的特性,此处问题中描述的行为是 nginx 中的预期行为。阅读更多内容后,我将在此处发布摘要
  • stackoverflow.com/questions/31017524/… 的答案有另一种可能适用于您的情况且不需要安装 Lua nginx 模块的解决方案

标签: nginx cookies cors


【解决方案1】:

我终于找到了问题的根本原因:

Ifs are evil

显然,在位置块中使用if 语句会导致未指定的行为。对我来说不幸的是,这意味着重新考虑我的整个设置,并可能改用 OpenResty。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-30
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多