How to override the value of a variable $host - nginx

I tried to change the value of the $host variable to $http_x-real-host:
set $host $http_x-real-host;
In response, I receive a message like:
11:53:51 [emerg] 12781#12781: the duplicate "host" variable in /tmp/nginx/nginx-cfg350749497:710
I can’t find any restrictions are imposed on the redefinition of core variables in nginx...

Related

How to log "x-kong-proxy-latency" in custom log formatter in Kong

I would like to log values of "x-kong-proxy-latency" and "x-kong-upstream-latency" headers to Kong log. How can I get access to those value in log_format?
KONG_PROXY_ACCESS_LOG: /dev/stdout custom_formatter
KONG_NGINX_HTTP_LOG_FORMAT: custom_formatter 'xkpl $$x_kong_proxy_latency'
This gets me an error:
2022-11-20 00:13:18 Run with --v (verbose) or --vv (debug) for more details
2022-11-20 00:14:20 Error: could not prepare Kong prefix at /usr/local/kong: nginx configuration is invalid (exit code 1):
2022-11-20 00:14:20 nginx: [emerg] unknown "x_kong_proxy_latency" variable
2022-11-20 00:14:20 nginx: configuration file /usr/local/kong/nginx.conf test failed
Now, what is the correct way to get this data in a variable?
As the error implied, x_kong_proxy_latency is not a variable nginx knows by default.
x-kong-proxy-latency and x-kong-upstream-latency are HTTP headers that are sent to the client indicating kong latency and upstream latency respectively
Since these headers are created by Kong to send t clients, we can use $sent_http_ nginx's prefix to inject the header into access_log, for example:
KONG_PROXY_ACCESS_LOG:/dev/stdout latency
KONG_NGINX_HTTP_LOG_FORMAT:latency '$$sent_http_x_kong_proxy_latency $$sent_http_x_kong_upstream_latency'
will inject the value of both headers you're looking for.
In case you are looking for more configs, you can look at the nginx document here

nginx map of header from upstream goes to default

I want to have a conditional header based on a header I want to get from the upstream.
For some reason it always gets translated to default.
Configuration:
upstream service decides if a header called x-no-iframe-protection should exist.
main nginx:
map $http_x_no_iframe_protection $x_frame_options {
yes "";
default "SAMEORIGIN";
}
server {
...
add_header X-Frame-Options $x_frame_options;
...
}
No matter what I try - I get both headers:
$ curl -v myhost
...
< x-no-iframe-protection: yes
< x-frame-options: SAMEORIGIN
...
Just to clarify - I use the x-no-iframe-protection just as a trick to remove x-frame-options in specific cases. I'm OK with it staying (although it is not needed once parsed by nginx)
Anyways - how can I make it get caught in order to replace the header value?
An HTTP transaction contains request headers and response headers. From the context of your question you are setting the value of a response header based on the value of another response header (which was received from upstream).
Nginx stores request headers in variables with names beginning with $http_ and response headers in variables with names beginning with $sent_.
In addition, response headers received from upstream may also be stored in variables with names beginning with $upstream_http_.
In your configuration you use the variable $http_x_no_iframe_protection, whereas you should be using either $sent_x_no_iframe_protection or perhaps $upstream_http_x_no_iframe_protection.
All of the Nginx variables are documented here.
try using $upstream_x_no_iframe_protection to access upstream response header.

How do I escape $ in nginx variables

I'm trying to map the BPM specific $WSSR header to the Host header in a nginx configuration and I continue to get "nginx: [emerg] unknown "wssn" variable
" errors. How can I access this header value in a nginx configuration? Is there a way to escape the $ character?
Here is my current configuration to try to map the custom header and the host to a single value
map $http_\$wssn $x_host {
default $host;
"~." $http_\$wssn;
}
reloading my config with this map results in this error
# nginx -s reload
2019/08/12 18:37:42 [emerg] 25091#25091: unknown "wssn" variable
nginx: [emerg] unknown "wssn" variable
As per agentzh-nginx-tutorials, the solution is a bit tricky. Since there is no way to escape dollar sign in NGINX variables, you have to make it a variable.
Totally untested, but maybe:
geo $dollar {
default "$";
}
geo $foo {
default "http_${dollar}wssn";
}
map ${dollar}${foo} $x_host {
default $host;
"~." ${dollar}${foo};
}

nginx set invalid number of arguments in "set" directive

I'm using the set directive as described in the nginx doc but I keep getting this error:
nginx_1 | 2016/09/13 15:06:08 [emerg] 8#8: invalid number of arguments in "set" directive in /etc/nginx/conf.d/default.conf:9
nginx_1 | nginx: [emerg] invalid number of arguments in "set" directive in /etc/nginx/conf.d/default.conf:9
default.conf :
server {
set $dn "foo.dnsalias.net";
...
}
I've tried both with and without quotes, with no change.
I'm using nginx version 1.10.1
Does anyone know what the issue is?
In your snippet you have
server {
set $dn "foo.dnsalias.net";
...
}
However, if in your actual conf file you are missing a trailing semicolon ; after the set directive, the directive will not be terminated and text on the next line will be treated as additional arguments, which will trigger your error.

Nginx how to pass a parameter to proxy server?

I have a very simple problem but I am new to nginx so I may be missing some obvious solution.
So I have a location defined in nginx
/example/$id
I would like to pass $id to parametrized route on my proxy server like so
http://server.com/example/$id
This gives me unknown id variable on nginx reload. So my question is how can I pass a parameter from nginx to my proxy server.
Assuming all the rest are correctly set, you can just create a location with a regex and pass the captured variable to your proxy.
location ~ ^/example/(.+)$ {
proxy_pass http://server.com/example/$1;
}
If your $id is numeric only, the regex could be more restrictive
location ~ ^/example/(\d+)$ {
proxy_pass http://server.com/example/$1;
}
Note that you can't just use a variable without declaring it first. Declaring $id is not necessary, it is captured inside the parentheses of the regex and passed to $1

Resources