Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx: can I use $server_name when specifying access log location?

I want to write a config file for an nginx virtual host that looks like this:

server {
    listen 80;
    server_name www.my-domain-name.com;

    access_log /home/me/sites/$server_name/logs/access.log;
    error_log  /home/me/sites/$server_name/logs/error.log;

    location /static {
        alias /home/me/sites/$server_name/static;
    }

    location / {
        proxy_pass http://localhost:8000;
    }
}

Using $server_name seems to work find for the location /static, but it doesn't seem to work for the access_log and error_log -- am I doing something wrong? Or is this just not possible? Can I do it some other way?

[update] - this is the error message when trying to reload nginx:

nginx: [emerg] open() "/home/me/sites/$server_name/logs/error.log" failed (2: No such file or directory)

like image 405
hwjp Avatar asked May 27 '13 12:05

hwjp


People also ask

Where are Nginx access logs stored?

By default, the access log is located at /var/log/nginx/access. log , and the information is written to the log in the predefined combined format. You can override the default settings and change the format of logged messages by editing the NGINX configuration file ( /etc/nginx/nginx. conf by default).

How do I enable access logs in nginx?

The NGINX access Log should be enabled by default. However, if this is not the case, you can enable it manually in the Nginx configuration file ( /etc/nginx/nginx. conf ) using the access_log directive within the http block.

What does server_name _ mean in nginx?

As a convention, the underscore is used as a server name for default servers. From http://nginx.org/en/docs/http/server_names.html. In catch-all server examples the strange name “_” can be seen: server { listen 80 default_server; server_name _; return 444; }

What is nginx access log?

The access log is enabled by default in the http context of core NGINX configuration file. That means access log of all the virtual host will be recorded in the same file. It is always better to segregate the access logs of all the virtual hosts by recording them in a separate file.


1 Answers

I wanted to do this too, but apparently by design nginx cannot expand variables in the error_log command, in case there are errors doing so and it cannot get a log filename to write them to.

Their suggestion is to use some program to generate your configuration files instead. You could use sed for this, to automatically search-and-replace your own variables and placing the output in the nginx configuration directory.

like image 52
Malvineous Avatar answered Oct 07 '22 00:10

Malvineous