Show real server name in nginx logs and params passed to fastcgi?

Aurielle asked:

I have configured my nginx vhosts with a regex in server_name. nginx shows the raw regex in logs and in params passed to php-fpm (I know there’s $_SERVER['HTTP_HOST'] with a real value, that’s not an issue). My question is how can I get nginx to put the real hostname to logs, instead of the regex.

The server_name directive looks like this:

server_name ~^(:?(?<second>.+)\.)?(?<domain>[^.]+\.[^.]+)$;

My answer:

Use $http_host in your log_format to get the actual host that the client sent.

You can also use that variable in access_log directives to split your logs by hostname.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.