我正在尝试将 nginx 配置为后端 Domino 服务器上的 Web 应用程序的反向代理服务器。我们的工作量已达到 99.9%,但最后的 0.1% 确实让我很烦恼。
我来解释一下。在某些情况下,应用程序会返回部分刷新,其中包含一个名为 的特殊响应标头X-XspLocation
。如果存在,则它包含客户端要重定向到的 URL。它是 XPages 环境生成和使用的标头,我的代码本身不会设置或读取它。它的值为:
http://localhost:81/database.nsf/page.xsp/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;
}
}
我甚至尝试使用 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;
}
现在的结果是原始标题加新的标题X-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;
}
}
答案1
“修改响应头为什么这么困难?”
有些人也需要谋生!
啊,这不是真正的问题。该死。试试这个
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;
}
使用 nginx/1.14.2 测试
答案2
添加总是对我有用
proxy_hide_header X-XspLocation;
add_header X-XspLocation $m_replaceme always;
答案3
显然,如果不重新编译,在 nginx 中从后端向代理响应添加标头会有点问题。
add_header
将在代理向后端服务器的请求中添加标头。只有后端在自己的响应中返回相同的标头而不进行修改时,才会实现目标。这种情况可能会发生,但当然不能保证。因此,如果没有发生这种情况 - 您可以尝试配置后端以允许此类行为。