As far as I know variables are still not allowed in
error_log even though you can use them in
access_log (as your screenshot reveals properly working in your scenario).
Besides that, you should almost always use
$host as it is the only guaranteed variable to say something sensible regardless of the user-agent's behavior.
You can check the difference between $host and $server_name on nginx documentation:
$host: in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name matching a request.
$server_name: name of the server which accepted a request. It contains the
server_name of the virtual host which processed the request, as it was defined in the nginx configuration. If a server contains multiple server_names, only the first one will be present in this variable.
As you have to account for all the possible things that a user-agent may do then forming an HTTP request (ancient HTTP/1.0 requests and modern badly-written software can happen all the time), you should consider using only the
$host variable for your intended purpose.
Worth mentioning that
access_log is handled by
ngx_http_log_module, while error_log is handled a little bit differently as a