9

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>[^.]+\.[^.]+)$;

Aurielle
  • 91
  • 1
  • 1
  • 2

1 Answers1

12

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.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Why not `$host`? – Alexey Ten Jul 04 '14 at 06:07
  • @AlexeyTen Because its semantics don't match what this person wants. [See the docs.](http://nginx.org/en/docs/http/ngx_http_core_module.html#var_host) – Michael Hampton Jul 04 '14 at 13:37
  • I understand this can be done for access logs. How should I modify the error logs? From what I've learned, it's not possible set custom format for them. (I'm trying to get split done by hostname, however it's giving me weird results right now – modified it and waiting for new entries.) – Aurielle Jul 04 '14 at 18:05
  • You can't modify the format of error logs, but you can certainly split them by host. – Michael Hampton Jul 04 '14 at 18:07
  • Okay, I have modified the directives like this: `access_log /var/log/nginx/access.$http_host.log; error_log /var/log/nginx/error.$http_host.log;` but nginx creates file with name `error.$http_host.log` (without replacing for the acutal one) on restart. Is this intended behaviour? – Aurielle Jul 04 '14 at 18:10
  • Also pardon wrong formatting and editing, I'm yet not used to this comment system. – Aurielle Jul 04 '14 at 18:12
  • Good question. I'm quite sure I have this working somewhere, but I can't find the configuration right now. – Michael Hampton Jul 04 '14 at 18:31
  • 3
    The Nginx `error_log` directive does not allow you to use variables in the file name param like `acesss_log` does. This seems to be because `access_log` is handled by the log module, but `error_log` is in core and has a different implementation. – Synchro Dec 10 '14 at 13:43