使用 proxy_pass 时,NGINX 不转发标头值

dal*_*ark 13 nginx proxy http-headers

我有以下设置并配置为将所有 /api 请求发送到不同的服务器:

location /api {
    proxy_pass              https://myapp.herokuapp.com;
    rewrite                 ^/api/(.*)              /$1     break;
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序发送一个标头 (USER_CUSTOMER),当从它工作的应用程序直接与 myapp.herokuapp.com 通信时,但是当通过代理服务器请求时,该值在 API 服务器上显示为 NULL。

以下在 NGINX 中有效,但我需要该应用程序能够设置 USER_CUSTOMER 的值。

location /api {
    proxy_pass              https://app.herokuapp.com;
    proxy_set_header        USER_CUSTOMER           ABC;
    rewrite                 ^/api/(.*)              /$1     break;
}
Run Code Online (Sandbox Code Playgroud)

将来我可能有额外的标头要发送,所以我希望有一个标志可以将所有标头从代理传递到 API 服务器。

Ubuntu
nginx/1.1.19
Run Code Online (Sandbox Code Playgroud)

dal*_*ark 31

标题属性 USER_CUSTOMER 是无效的语法。下划线在标题属性中无效。

有一个解决方法,但最好的解决方案是将属性重写为有效的语法。

解决方法是在配置中指定服务器名称的位置设置以下内容:

underscores_in_headers on;
Run Code Online (Sandbox Code Playgroud)

  • 在我的情况下不起作用,错误日志显示: nginx: [emerg] "underscores_in_headers"directive is not allowed here in nginx.conf:55 (2认同)

pho*_*ops 9

我认为你正在寻找proxy_pass_request_headers选择。将其设置为开启:

location /api {
    proxy_pass_request_headers      on;
    proxy_pass                      https://app.herokuapp.com;
    proxy_set_header                USER_CUSTOMER              ABC;
    rewrite                         ^/api/(.*)                 /$1     break;
}
Run Code Online (Sandbox Code Playgroud)

  • @dallasclark:有道理,因为默认是它在... http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass_request_headers (2认同)