Logging Multiple Domains in Nginx

2024-04-18

I bought another domain that I promise I’m totally going to use for a real project. I’m not expecting too much traffic, so I went ahead and set it up on the same server as this blog. And this is why running your own server is pretty cool: you can’t take two steps without learning something new. I was looking at the access logs and I noticed that they do not include the domain being accessed. If I load the front page of this blog, or the front page of my other site, Nginx logs the same thing for the request: /.

After many minutes of searching through documentation, I found the solution. In the server block of your configuration you can specify the path to log to. So it is simply a matter of having each domain log to its own file. Like so:

server{
    server_name foo.com;
    access_log /var/log/nginx/foo.access.log;
}
server{
    server_name bar.com;
    access_log /var/log/nginx/bar.access.log;
}

And now I know, and so do you.