bitbucket
bitbucket

Reputation: 1

add_header directive not honored by nginx

I'm overlooking something very simple here. Using the following configuration, the header set with add_header never materializes. I tried adding it to both server clauses as well with the same effect. version is 1.19.2

server {
    listen              443 default_server ssl;
    listen              [::]:443 default_server ssl;
    server_name         _;
    ssl_certificate     /etc/ssl/some.crt;
    ssl_certificate_key /etc/ssl/some.key;
}

server {
    listen      443;
    server_name wiki.some.com;
    location / {
        add_header  X-Upstream  "test123";
        proxy_pass http://172.17.0.4:80;
    }
}

For what it's worth, I'm using the following bit of php to display headers received at the server:

<?php
$headers =  getallheaders();
foreach($headers as $key=>$val){
  echo $key . ': ' . $val . '<br>';
}
?>

Upvotes: 0

Views: 914

Answers (1)

Ivan Shatsky
Ivan Shatsky

Reputation: 15697

A very common beginners mistake. add_header directive is used for adding additional headers to the response nginx send as an answer to user's browser request. If you want to add aditional headers to the proxied request going to some backend use proxy_set_header directive.

Upvotes: 1

Related Questions