How can I set an User-Agent header when empty? (Eg, HTTP/1.0)
This is what I tried so far:
set $ua $http_user_agent;
if ($http_user_agent = "") //also tried = false
{
set $ua "Fixing-Empty-User-Agent";
proxy_pass http://$host$request_uri;
}
proxy_connect_timeout 2;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_set_header User-Agent: $ua;
Your configuration is correct and works like a charm here (CentOS 7, nginx 1.6.1).
If you check your error_log file, you'll probably see an error like this:
In that case, add a resolver to nginx's location clause. For instace:
And this is the result in access_log:
It is better to avoid the if directive. You can instead use a map to populate
$ua
based on whether the receivedUser-Agent
header is missing or empty: