Nginx set proxy_set_header if header is present

if statements aren't a good way of setting custom headers because they may cause statements outside the if block to be ignored.

You can use a map directive instead which is not prone to the same problems.

# outside server blocks
map $http_X_Amz_Cf_Id $is_cloudfront {
    default "No";
    ~. "Yes";  # Regular expression to match any value


# inside location block
proxy_set_header X_Custom_Header $is_cloudfront;

A general answer is that you can set variables in if and then use the variable. Like this:

set $variable "";
if ($http_X_Amz_Cf_Id) {
  set $variable "somevalue";
proxy_set_header someheader $variable;