我正在尝试将nginx配置为后端Domino服务器上web应用程序的反向代理服务器。我们有99.9%的人在工作,但最后的0.1%确实困扰着我。
我来解释。在某些情况下,应用程序使用名为X-XspLocation
的特殊响应头返回部分刷新。如果它存在,它包含一个由客户端重定向到的url。它是由XPages环境生成和使用的标题,我的代码本身并不设置或读取它。因此,其价值是:
http://localhost:81/database.nsf/page.xsp/ThankYou
我希望它是这样的: /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来调用一个不返回任何内容的函数。
为什么修改响应头如此困难??
真正的问题当然是:我怎样才能做到这一点?谢谢你的帮助!!
More信息
为了证明地图有效,我用以下方法进行了测试:
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版本: nginx/1.18.0在Ubuntu 16.04.6 LTS (我客户的供应商系统,而不是我的.)上。
The全删失配置文件
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;
}
}
发布于 2020-05-21 11:52:02
“为什么修改响应头如此困难?”
有些人也需要谋生!
这不是真正的问题。见鬼。尝尝这个
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测试
发布于 2021-12-07 12:53:44
添加的总是对我有用
proxy_hide_header X-XspLocation;
add_header X-XspLocation $m_replaceme always;
发布于 2022-06-30 07:27:33
显然,在nginx中,在没有重新编译的情况下,从后端向代理响应添加头有点问题。
add_header
将向从代理到后端服务器的请求中添加报头。只有当后端返回相同的标题而不修改自己的响应时,它才能实现目标。这是可能发生的,但当然不能保证。因此,如果没有发生这种情况-您可以尝试配置您的后端以允许这样的行为。
https://serverfault.com/questions/1018050
复制相似问题