We have nginx set up with about 5 domains. Each domain has a specific IP associated with it. When a request comes in on an IP, nginx directs that request to the relevant site, regardless of what domain the request came in on.

For example, our site is parent.com and is on The A record for child.com points to Therefore, nginx will send all requests for child.com to parent.com (Parent.com is running a SaaS setup and detects the requested domain automatically). This all works perfectly fine.

What we want to do now is define an independent log file for each domain. Right now our configuration file defines a static log location. We want to modify this so that the log location is set based on the domain that is accessed.

Our current log directives:

access_log /home/parent/_logs/access.log;
error_log /home/parent/_logs/error.log;

I just discovered that I can use $host in my access_log directive, which is enough for our needs.

Our new access_log directive is:

access_log /home/parent/_logs/$host.access.log

And it appears to be working perfectly.

  • I'm getting $host.error.log for some reason – aexl Sep 24 '18 at 13:34

Your "solution" with $host variable is ugly. Just try ...

curl -H "Host: abc" http://parent.com
curl -H "Host: def" http://parent.com
curl -H "Host: 123" http://parent.com
curl -H "Host: 321" http://parent.com
curl -H "Host: xyz" http://child.com
curl -H "Host: zyx" http://child.com
curl -H "Host: 456" http://child.com
curl -H "Host: 654" http://child.com

... and look to /home/parent/_logs/ directory.

Define two server {} sections with server_name parent.com; and server_name child.com and use variable $server_name in access_log directive.

  • 1
    Why is it ugly? We are running THOUSANDS of client.com type of sites through one parent domain. I'm not going to make a server block for each of those domains. I don't understand what is so ugly about putting $host in the access_log area. It creates one log per domain, which is what we wanted to do in the first place. – Kevin Jun 5 '13 at 18:44
  • $host variable taken from Host: blablabla field of HTTP header. The bad guys can set here anything random on every request. You will get HUNDREDS OF THOUSANDS "garbage" files in your logs directory. – cadmi Jun 6 '13 at 15:13
  • 1
    I understand that, but your 'solution' won't work for us either because we aren't going to define a server block for every single domain hosted. logrotate will take care of the garbage files so it's not that big of a deal. If you have a better solution than what I did, then please enlighten me. – Kevin Jun 6 '13 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.