0

I want make dynamic subdomains with logs in separate directories, but have a problem with it:

  1. If I provide a path to logs like /var/www/demo/subdomains/$sub/logs/access.log and same with error, NGINX writes /var/www/demo/subdomains/$sub/logs/error.log" failed (2: No such file or directory)
  2. If I provide a path like in config now /var/www/demo/subdomains/logs/$sub-error.log I have the problem that $sub-access.log is named correctly like demo1-access.log or demo2-acces.log, but $sub-error.log is named just as $sub-error.log. For 2 domains I have 3 files - demo1-access.log, demo2-access.log, and $sub-error.log.

What do I need to change to make the first method working? The second is fine too, but not so beautiful.

server {
    listen 80;
    listen [::]:80;

    server_name "~^(?<sub>.+)\.deb\.test$";
    root /var/www/demo/subdomains/$sub/www;
    index index.php index.html;

    access_log /var/www/demo/subdomains/logs/$sub-access.log combined;
    error_log /var/www/demo/subdomains/logs/$sub-error.log info;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FLIENAME $document_root@fastcgi_script_name;
        include /etc/nginx/fastcgi.conf;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
        return 403;
        error_page 403 /403_error.html;
    }

    location ~* \.(ico|pdf|flv)$ {
        expires 1y;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
        expires 14d;
    }

    location ~ /\.ht {
        deny all;
    }
}

Adding one more server block with error_log directive works the same:

server {
        listen 80;
        listen [::]:80;
        server_name "~^(?<sub>.+)\.deb\.test$";
        error_log /var/www/demo/subdomains/logs/$sub-error.log info;
}
0

It is possible to use variables (with some limitations) in the access_log directive, but it is not possible to use variables in the error_log directive. To log errors to a different file, you would need to use a different error_log directive, probably in a new server block.

  • I have moved error_log directive from first server block to second and it works same. – AccessDenied May 22 at 8:35

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.