使用 nginx 修改响应头

D.B*_*ger 2 nginx reverse-proxy ibm-domino

我正在尝试将 nginx 配置为后端 Domino 服务器上的 Web 应用程序的反向代理服务器。我们有 99.9% 的工作,但最后 0.1% 真的让我烦恼。

我来解释一下。在某些情况下,应用程序返回部分刷新,并带有一个名为 的特殊响应标头X-XspLocation。如果存在,则它包含要由客户端重定向到的 url。它是 XPage 环境生成和使用的标头,我的代码本身不会设置或读取它。那么它的值是:

http://localhost:81/database.nsf/page.xsp/ThankYou
Run Code Online (Sandbox Code Playgroud)

我希望它就是这样:/ThankYou

我尝试了一百万种方法,但似乎不可能改变它的价值。只要我使用proxy_hide_header X-XspLocation;没有新的标题就可以使用add_header! 如果我忽略隐藏,我会在标题中得到双倍值,所以我知道我的替换值是正确的。这是我最近失败的尝试:

map $sent_http_x_xsplocation $xsplocation_new {
    "~http://localhost:81/database.nsf/page.xsp/(.*)" "/$1";
}
server {
    ...
    location / {
      proxy_pass   http://localhost:81/database.nsf/page.xsp/;
      # redirect X-XspLocation
      proxy_hide_header X-XspLocation;
      add_header X-XspLocation $xsplocation_new;
      #add_header X-XspLocation2 $xsplocation_new;
    }
}
Run Code Online (Sandbox Code Playgroud)

我什至尝试用 njs 来更改标题,它可能失败了,因为我不知道如何使用 js_set 或 js_content 来调用一个不返回任何内容的函数。

为什么修改响应头这么难?

真正的问题当然是:我怎样才能做到这一点?谢谢你的协助!!

更多信息

为了证明该地图有效,我进行了以下测试:

    location / {
      proxy_pass   http://localhost:81/database.nsf/page.xsp/;
      # redirect X-XspLocation
      # proxy_hide_header X-XspLocation;
      # add_header X-XspLocation $xsplocation_new;
      add_header X-XspLocation2 $xsplocation_new;
    }
Run Code Online (Sandbox Code Playgroud)

结果现在是原来的header加上新的headerX-XspLocation2,而第二个正是我在X-XspLocation.

顺便说一下,nginx 版本:Ubuntu 16.04.6 LTS 上的 nginx/1.18.0(我客户的供应商系统,不是我的......)

完整的审查配置文件

map $sent_http_x_xsplocation $xsplocation_new {
    "~http://localhost:81/database.nsf/page.xsp/(.*)" "/$1";
}

server {
    listen       4443 ssl;
    server_name  www.myclient.nl;

    ssl_certificate       /etc/nginx/ssl/www.myclient.nl.pem;
    ssl_certificate_key   /etc/nginx/ssl/www.myclient.nl.pem;

    # do not allow google to index this website
    # TODO: remove when going to production
    add_header  X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";

    # replace redirects in response header fields Location and Refresh
    proxy_redirect http://localhost:81/database.nsf/page.xsp/ https://www.myclient.nl:4443/;
    proxy_redirect http://localhost:81/ https://www.myclient.nl:4443/;

    # tell domino not to encode the response so we can use sub_filter
    proxy_set_header Accept-Encoding "";

    # substitute response content
    sub_filter 'localhost:81'       'www.myclient.nl:4443';
    sub_filter 'www.myclient.nl' 'www.myclient.nl:4443'; #TODO: remove when going production
    sub_filter '/database.nsf/page.xsp/'  '/';
    sub_filter '/database.nsf/'           '/other/';
    sub_filter_once off;

    # Domino
    location = /favicon.ico {
      access_log off; log_not_found off;
      proxy_pass http://localhost:81/database.nsf/Images/favicon.ico/%24file/favicon.ico;
    }

    # root / homepage
    location = / { proxy_pass   http://localhost:81/database.nsf/page.xsp/HomePage; }

    #login
    location /names.nsf { proxy_pass http://localhost:81/names.nsf; }

    # XPages
    location /xsp/ { proxy_pass  http://localhost:81/xsp/; }
    location /domjava/ { proxy_pass  http://localhost:81/domjava/; }

    # training
    location ~* ^/.*-training/(.*) {
      proxy_pass http://localhost:81/database.nsf/page.xsp/training/$1;
    }
    location ~* ^/(.*)-training$ {
      proxy_pass http://localhost:81/database.nsf/page.xsp/$1;
    }

    # IMAGES
    # image resources - any case insensitive match with 'images'
    location ~* '/images/(.*)$' {
      proxy_pass   'http://localhost:81/database.nsf/Images/$1';
    }
    # images referenced from css in file.xsp have this url, redirect to backend correctly
    location ~* '/file.xsp/images/(.*)$' {
      proxy_pass   'http://localhost:81/database.nsf/Images/$1';
    }

    # file resources
    location /file.xsp/ { proxy_pass  http://localhost:81/database.nsf/file.xsp/; }

    # other resources
    location /other/ { proxy_pass   http://localhost:81/database.nsf/; }

    # all other urls
    location / {
      proxy_pass   http://localhost:81/database.nsf/page.xsp/;
      # redirect X-XspLocation
      #add_header X-XspLocation $xsplocation_new always;
      proxy_hide_header X-XspLocation;
      add_header X-XspLocation $xsplocation_new;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

“为什么修改响应头这么难??”

有些人也需要谋生!

啊,那不是真正的问题。该死。尝试这个

map $upstream_http_x_xsplocation $m_replaceme {
    ""  "";
    "~^.*/page.xsp/(.*)$" "/$1";
    "~.*" "";
}

location / {
      proxy_pass   http://localhost:81/database.nsf/page.xsp/;
      proxy_hide_header X-XspLocation;
      add_header X-XspLocation  $m_replaceme;
}
Run Code Online (Sandbox Code Playgroud)

用 nginx/1.14.2 测试


小智 5

添加总是对我有用

proxy_hide_header X-XspLocation;
add_header X-XspLocation  $m_replaceme always;
Run Code Online (Sandbox Code Playgroud)