Custom error pages and access_log inheritance

bjoe2k4 nginx-forum at nginx.us
Fri Oct 2 11:30:28 UTC 2015


Hi,

i was wondering if it is possible to have custom error pages *without* the
error page locations inheriting the http or server level access_log
directives. Please consider the following config:

------------------
server {
    listen *:80;
    server_name test.domain;
    root /app/www/;
    
    location / {}
    
    location ~ \.html$ {
        access_log /app/log/html.log custom;
    }
    
    access_log /app/log/test.log custom;
}
------------------

All access logs for requests "/exists.html" with status code 200 and
"/nonexists.html" with status code 404 are in the html.log file.

However if i add a custom error page for the 404 error on the server level:
------------------
server {
    /* above config */
    
    error_page 404  /x;
    
    location = /x {
        internal;
        echo "error";
    }
}
------------------
The access logs for "/nonexists.html" are now located in the file
"test.log", which makes perfect sense, since the server level access_log
directive is inherited by location /x. The only way to fix this is a
somewhat lengthy config like this:

------------------
server {
    listen *:80;
    server_name test.domain;
    root /app/www/;
    
    location / {}
    
    location ~ \.html$ {
        access_log /app/log/html.log custom;
        error_page 404  /xhtml;
    }
    
    access_log /app/log/test.log custom;
    
    error_page 404  /x;
    
    location = /x {
        internal;
        echo "error";
    }
    
    location = /xhtml {
        internal;
        echo "error";
        access_log /app/log/html.log custom;
    }
}
------------------

Is there any way to have both 1) a custom error page and 2) all the access
logs stay in the inital location's access_log, *without* having to define
both error_page and the corresponding location with seperate log file?

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,261979,261979#msg-261979



More information about the nginx mailing list