大网数据广告活动
服务器运维问答

passing up headers from auth_request

· 52CE 我爱测

I have an authorization module which is called whenever a request is made to a private endpoint. The module parses the token from the Authorization header, and: if it's invalid, it returns 401 if it's valid, it returns 200 if it'…

问题原文

原帖链接 · BackSlash · CC BY-SA 4.0

I have an authorization module which is called whenever a request is made to a private endpoint.

The module parses the token from the Authorization header, and:

  1. if it's invalid, it returns 401
  2. if it's valid, it returns 200
  3. if it's valid but is about to expire in X minutes, it generates a new token and returns that one in the Authorization header.

"profile" is one of the private endpoints, and it's configured this way:

location /profile {
  auth_request /jwtverify;

  auth_request_set  $authorization $upstream_http_authorization;
  proxy_set_header  authorization $authorization;
  proxy_pass        http://private-profile:80;
}

jwtverify is configured this way:

location = /jwtverify {
  internal;
  proxy_pass              http://auth-module:8080/auth/verify;
  proxy_pass_request_body off;
  proxy_set_header        Content-Length "";
  proxy_set_header        X-Original-URI $request_uri;
  auth_request_set        $http_authorization $upstream_http_authorization;
}

Now, everything works except for requirement no. 3: if the auth module sets the Authorization header, the client never receives it.

The flow should be like this:

  • Client makes the request
  • auth-module intercepts the request and, if valid, the proxy passes it to the private service
  • When the response is sent, headers set by auth-module should be kept and sent to the client

I think I didn't understand properly how to combine auth_request_set, proxy_set_header, auth_request_set, it might also be that they aren't correct for this scenario.

Is there a way to accomplish this in NGINX?

已采纳解答

原帖链接 · BackSlash · CC BY-SA 4.0

Ok, I was able to do that with the help of the headers_more module.

The full configuration is:

location = /jwtverify {
  internal;
  proxy_pass              http://auth-module:8080/auth/verify;
  proxy_pass_request_body off;
  proxy_set_header        Content-Length "";
  proxy_set_header        X-Original-URI $request_uri;
}

location /profile {
  auth_request /jwtverify;

  # this gets called right after auth_request returns.
  # it reads http "authorization" header from upstream (= auth_request)
  # and sets it to the variable $auth_header
  # https://stackoverflow.com/a/31485557/1759845
  auth_request_set $auth_header $upstream_http_authorization;

  # this gets called right before sending response to client.
  # it adds the previously set variable (= "authorization" 
  # header from auth_request) to the response
  more_set_headers "Authorization: $auth_header";

  proxy_pass        http://private-profile:80;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-01-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。