nginx 302 转发如何携带原请求的 head

27 天前
 hello826

场景是有一台内网的 nginx 不能反问外网,但是有个请求需要转发到外网的一个地址,目前通过 return 302 实现了 但是 302 的时候无法携带 head ,试了

proxy_set_header X-Original-userAccount $http_x_original_userAccount

proxy_set_header X-Original-user-account $http_x_original_user_account

也还是取不到头,服务的是通过 request.getHeader("userAccount")取的 具体 nginx 配置如下,有无运维大佬能指点一下

localtion /test{

proxy_set_header X-Original-userAccount $http_x_original_userAccount;

proxy_set_header X-Original-user-account $http_x_original_user_account;

return 302 http://10.14.13.12/test?$args;

}

2322 次点击
所在节点    程序员
29 条回复
nodejx
27 天前
location /test {
proxy_set_header X-Original-userAccount $http_x_original_userAccount;
proxy_set_header X-Original-user-account $http_x_original_user_account;
proxy_pass http://10.14.13.12/test?$args;
}
AloneHero
27 天前
重定向应该带不了 header
Puteulanus
27 天前
307 和 308 ?
caola
27 天前
307 转发也不行吗?
xiri
27 天前
301 、302 重定向都是服务端告诉客户端重新对一个新地址发起请求,当前会话直接是结束了,没法影响客户端下一次重新发起请求中的内容的,感觉你需要的是反向代理而不是重定向?
icaolei
27 天前
用 rewrite 试试呢?
ETiV
26 天前
302 都能带 header ,网络安全就不存在了…

你需要做的是在这个不能访问外网的 nginx 前面再加一个能访问的,对于你提到 302 的这个请求 proxy pass 到外网去,其余的 pass 给正文提到的 nginx
neighbads
26 天前
换到 get 参数上,放到 url 里。外网服务改一下
yinmin
26 天前
这题有实战经验,安全的解法是:return 200 带一个 html 页面,页面里有一个 form ,post 参数到外网 url ,然后外网服务获取 post 参数。
leonshaw
26 天前
CORS
zliea
26 天前
301 ,302 重发请求带参数不是客户端该干的事么?
sagaxu
26 天前
标准答案 307

The only difference between 307 and 302 is that 307 guarantees that the method and the body will not be changed when the redirected request is made.
morenacl
26 天前
按照 http 协议规范,307 可以
siweipancc
26 天前
location 由请求方处理,一般客户端都会处理这个
hello826
26 天前
试了 307 也不行,还是取不到头,这两种都试了

location /test {

proxy_set_header X-Original-userAccount $http_x_original_userAccount;

proxy_set_header X-Original-user-account $http_x_original_user_account;

return 307 http://10.14.13.12/test?$args;
}

location /test {

return 307 http://10.14.13.12/test?$args;

}

目前想到的方法是走服务端转发,服务端所在的机器可以访问外网,nginx 不能访问外网
jifengg
26 天前
proxy_set_header 是你用 proxy_pass 时携带给代理地址的,不是返回给客户端的,所以你应该用 add_header
注:没实验过,请楼主自行实验
hello826
26 天前
@jifengg add_header 也试了,不行,看来这条路是走不通
add_header X-Original-userAccount $http_x_original_userAccount always;
add_header X-Original-user-account $http_x_original_user_account always;
add_header X-Original-UserAccount $http_useraccount always;
killva4624
26 天前
add_header 只是在这个 302 返回上加 header ,和客户端重定向之后的新请求没有关系吧。
可以换个思路,需要访问外网的请求,代理转发到另外一个出口代理。
superrichman
26 天前
@hello826 #15 客户端在处理 307 重定向,默认只会读取返回的 Location 头信息并更新,然后保持原有的 header 和 body 信息向新的位置发起请求。在 307 响应中服务端设置的其他头信息通常不会被客户端处理。
morenacl
26 天前
add_header 处理的是响应头,应该是发请求头,请求头在客户端发起时设置

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1033742

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX