nginx - 将请求标头变量作为查询参数传递给上游URL

问题描述 投票:2回答:2

我有一个运行在localhost上的应用程序侦听端口8080

nginx作为反向代理运行,侦听端口80

因此,在端口80上发送到nginx的请求将发送到此应用程序,侦听localhost:8080并将此应用程序的响应发送回用户

现在,此应用程序无法从请求标头中读取标头变量,并且只能读取查询参数

所以我希望nginx将头值作为查询参数传递给这个应用程序,监听localhost:8080

例如。让我们在请求标题中说有一个名为'userid'的自定义变量。

我们如何将此userid传递为&userid = value附加到应用程序侦听localhost 8080的url

我当前的站点可用和启用站点的测试文件是

server {

    location /test {

        proxy_pass http://localhost:8080;
    }

}
nginx nginx-reverse-proxy nginx-config
2个回答
3
投票

如果你有一个名为userid的请求头,它将在名为$http_userid的Nginx变量中可用。

您可以使用rewrite...break语句更改原始请求的查询参数。

例如:

location /test {
    rewrite ^(.*)$ $1?userid=$http_userid break;
    proxy_pass http://localhost:8080;
}

有关详细信息,请参阅this document


1
投票

所以不需要重写或其他任何东西。只需将您要作为查询参数传递的头参数传递给localhost应用程序,如下所示,方法是附加参数。

如果你有自定义头参数,如userid,那么它将是$ http_userid

server {

    location /test {

          set $args $args&host=$http_host;

          proxy_pass http://localhost:8080;
    }
 }
© www.soinside.com 2019 - 2024. All rights reserved.