在 NGINX 中重定向时添加请求标头

时间:2021-07-11 15:07:31

标签: http nginx routes

我有一个简单的重定向规则,例如

location = /redirect-me {
    return 307 http://localhost:3001/redirect-me;
}

将对 redirect-me url 进行 POST 调用,我想要做的就是将 post 调用重定向到另一台服务器。

但是我怎样才能将 X-Header 添加到 request header 去 http://localhost:3001/。

我尝试了 add_headerproxy_set_headeradd_header 是将标头添加到响应标头而不是请求标头

location = /redirect-me {
    add_header 'X-Header-a' 'cust_info';
    proxy_set_header 'X-Header-p' 'cust_info';
    return 307 http://localhost:3001/redirect-me;
}

并且 proxy_set_header 没有添加任何东西。

那么如何在使用 307 重定向时向请求添加标头。?

1 个答案:

答案 0 :(得分:0)

使用 proxy_pass 指令代替重定向。我提供了以下文档的链接,其中包括设置标题的示例。

location /redirect-me {
    proxy_set_header 'X-Header-p' 'cust_info';
    proxy_pass http://localhost:3001/redirect-me;
}

https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/