Nginx

NGINX 無法從響應中讀取自定義標頭

  • September 30, 2019

從我的 nginx 伺服器,我想從外部 Apache 伺服器獲取帶有自定義標頭的身份驗證響應。問題是,我無法獲得自定義標頭的值。

location /app  {
               auth_request /auth;
               add_header custom $http_x-customheader; 
      }

location = /auth {
                   proxy_pass http://ip.externalserver/auth.php;
                   proxy_pass_request_body off;
                   proxy_set_header Content-Length "";
                   proxy_set_header X-Original-URI $request_uri;
                   proxy_set_header X-Original-METHO $request_method;
                   proxy_pass_request_headers on;

}

直接從 Apache 伺服器請求時:

$ curl -i "http://xxxx.xxx.xxxx.xxxx/auth.php"
HTTP/1.1 200 OK
Date: Mon, 30 Sep 2019 17:08:05 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.29
x_customheader: 2
x-headername: headervalue
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Content-Length: 193
Content-Type: text/html

但是通過nginx:

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 30 Sep 2019 16:47:28 GMT
Content-Type: application/vnd.apple.mpegurl
Content-Length: 559
Last-Modified: Mon, 30 Sep 2019 16:47:26 GMT
Connection: keep-alive
ETag: "5d92319e-22f"
Expires: Wed, 08 Jan 2020 16:47:28 GMT
Cache-Control: max-age=8640000
custom: -customheader
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: GET, HEAD, OPTIONS
Access-Control-Allow-Origin: *
Accept-Ranges: bytes

我試過這個沒有成功:

add_header custom $http_x-customheader; 
add_header custom $http_x_customheader; 
add_header custom $upstream_http_x_customheader; 
add_header custom $upstream_http_x-customheader; 

知道了:

auth_request_set $myheader $upstream_http_x_costumheader;
add_header costum $myheader; 

引用自:https://unix.stackexchange.com/questions/544496