场景是有一台内网的 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;
}
![]() | 1 nodejx 2024-04-18 21:50:34 +08:00 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; } |
![]() | 2 AloneHero 2024-04-18 22:11:45 +08:00 via Android 重定向应该带不了 header |
![]() | 3 Puteulanus 2024-04-18 22:18:28 +08:00 307 和 308 ? |
![]() | 4 caola 2024-04-18 22:19:36 +08:00 307 转发也不行吗? |
![]() | 5 xiri 2024-04-18 23:28:56 +08:00 301 、302 重定向都是服务端告诉客户端重新对一个新地址发起请求,当前会话直接是结束了,没法影响客户端下一次重新发起请求中的内容的,感觉你需要的是反向代理而不是重定向? |
![]() | 6 icaolei 2024-04-19 02:06:12 +08:00 用 rewrite 试试呢? |
![]() | 7 ETiV 2024-04-19 06:00:36 +08:00 via iPhone 302 都能带 header ,网络安全就不存在了… 你需要做的是在这个不能访问外网的 nginx 前面再加一个能访问的,对于你提到 302 的这个请求 proxy pass 到外网去,其余的 pass 给正文提到的 nginx |
![]() | 8 neighbads 2024-04-19 06:52:44 +08:00 换到 get 参数上,放到 url 里。外网服务改一下 |
9 yinmin 2024-04-19 07:16:40 +08:00 via iPhone 这题有实战经验,安全的解法是:return 200 带一个 html 页面,页面里有一个 form ,post 参数到外网 url ,然后外网服务获取 post 参数。 |
10 leonshaw 2024-04-19 08:27:09 +08:00 via Android CORS |
![]() | 11 zliea 2024-04-19 08:51:54 +08:00 301 ,302 重发请求带参数不是客户端该干的事么? |
![]() | 12 sagaxu 2024-04-19 08:53:36 +08:00 标准答案 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. |
13 F7TsdQL45E0jmoiG 2024-04-19 09:05:56 +08:00 按照 http 协议规范,307 可以 |
![]() | 14 siweipancc 2024-04-19 09:28:21 +08:00 via iPhone location 由请求方处理,一般客户端都会处理这个 |
15 hello826 OP 试了 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 不能访问外网 |
16 jifengg 2024-04-19 10:05:55 +08:00 proxy_set_header 是你用 proxy_pass 时携带给代理地址的,不是返回给客户端的,所以你应该用 add_header 注:没实验过,请楼主自行实验 |
17 hello826 OP @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; |
![]() | 18 killva4624 2024-04-19 10:36:21 +08:00 add_header 只是在这个 302 返回上加 header ,和客户端重定向之后的新请求没有关系吧。 可以换个思路,需要访问外网的请求,代理转发到另外一个出口代理。 |
19 superrichman 2024-04-19 10:50:17 +08:00 @hello826 #15 客户端在处理 307 重定向,默认只会读取返回的 Location 头信息并更新,然后保持原有的 header 和 body 信息向新的位置发起请求。在 307 响应中服务端设置的其他头信息通常不会被客户端处理。 |
20 F7TsdQL45E0jmoiG 2024-04-19 10:55:57 +08:00 add_header 处理的是响应头,应该是发请求头,请求头在客户端发起时设置 |
21 jifengg 2024-04-19 10:56:26 +08:00 试了下,启动一个 http 服务( 8081 端口)打印原始 header 和 body ,并用 nginx ( 80 端口)配置 307 到这个服务 location =/test307 { return 307 http://127.0.0.1:8081/?redirtby=nginx; } 客户端发起请求时携带 header curl "127.1/test307" -iL --header "h1:v1" --header "X-Original-user-account:jifeng" 在 http 服务中是能收到这两个 header 的。 在 nginx 中 add_header 没用。必须是客户端携带的 header 。 另外,你要确保你取 header 的那个代码是正确的。 |
22 yinmin 2024-04-19 11:47:24 +08:00 nginx 加了是 response header ,307 是指 request header ,不是一个东西。 |
![]() | 23 sparkssssssss 2024-04-19 12:25:57 +08:00 cookie 试试? 设置 cookie 时指定了 Domain 属性为.test.com 比如你内网你解析一个 a.test.com,然后重定向到 b.test.com,这时候,cookie 可以 share 的吧? |
![]() | 24 proxytoworld 2024-04-19 13:00:29 +08:00 为什么不反代呢 |
25 feixiangcode 2024-04-19 13:45:39 +08:00 302 是服务器给终端发送一个 HTTP 的状态码,具体的跳转行为是终端拿到 Location 自己做的跳转。服务器只发状态码,跟数据无关。 |
26 dyllen 2024-04-19 14:04:49 +08:00 你这配置明显就是错的,用的 proxy_set_header 指令,又 return 302 ,302 又不是转发请求,ng 服务器访问不了外网,就不能用它来代理访问外网的请求。 |
![]() | 27 NeedI09in 2024-04-19 14:36:11 +08:00 1.通过返回的 Location 字段携带信息,例如 Location: /test?data=1 2.放在客户端 cookie 里 http 协议规范里好像没有与转发请求头相关的,所以用 cookie 比较适合你的方案。 |
28 jpyl0423 2024-04-19 16:51:24 +08:00 用反代能解决吧 |
29 deorth 2024-04-20 12:23:32 +08:00 via Android 要在脑子里打开 |