How can I separate access logs from requests of two different domains of the same server?

Satoru.Logic asked:

My nginx configuration looks like this:

server {
   listen 80;
   server_name domain1.com domain2.com;

   access_log /var/log/myapp/nginx/access.log;

   ...
}

Currently requests from both domains are logged into the same file, I want to separate these so that requests from domain1.com continue to be saved in access.log, while requests from domain2.com be saved in domain2.access.log.

How can I do this?

My answer:


You can use a variable in your access_log entry. For instance:

access_log /var/log/nginx/$http_host-access.log

View the full question and answer on Server Fault.

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