1

I’m using NGINX as a gateway to forward requests from some client to a backend service, with an authentication service in between. The authentication service do nothing for now just response 200 and sets a custom header (db_read_time) in the response. However, when I try to access my backend service, the "db_read_time" header is not being forwarded by NGINX.

I tried to log the header but it seems that nginx cant find it, in the nginx docs i see it says the upstream_http_db_read_time refer only to the last server response so i guess it only see the header from the backend and not the authentication for some reason.

I need a way to get an header from the authentication service and forward it to the backend service.

authentication service: `

@RequiredArgsConstructor
@RestController
public class AuthenticationController {

    private final JdbcTemplate jdbcTemplate;

    @GetMapping("/authenticate")
    public ResponseEntity<String> test(@RequestHeader("Authorization") String authorizationHeader) {
        return ResponseEntity.ok()
                .header("db_read_time", "123").body("placeholder");
    }
}

`

nginx conf: `

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" '
                    '"$upstream_http_db_read_time"';

    access_log logs/access.log custom;

    server {
        listen       80;
        server_name  localhost;

        location /api/ {
            auth_request /auth;

            proxy_pass http://localhost:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

            proxy_set_header DB-Read-Time $upstream_http_db_read_time;
        }

        location = /auth {
            internal;
            proxy_pass http://localhost:5001/authenticate;
            proxy_set_header Content-Type application/x-www-form-urlencoded;
            proxy_set_header X-Original-URI $request_uri;
            proxy_set_header Authorization $http_authorization;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

`

nginx log: `

127.0.0.1 - student [04/Oct/2024:17:15:52 +0300] "GET /api/hello HTTP/1.1" 200 27 "-" "curl/8.1.2" "-" "-"

`

1
  • have you tried underscores_in_headers on;? Commented Nov 6, 2024 at 8:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.