3

I have a nginx to host a django application. I need to log 400 Bad Request errors from my web page to a separate log file in nginx configuration.

The docs I found was to give separate response in case of errors. I tried the following code with nginx configuration:

error_page 400 /400.html;

location /400.html {
    internal;
}

But the above code does not log the 400 errors.

2

2 Answers 2

1

In your snipet missing definiton of error log.

error_page 400 /400.html;

location /400.html {
    internal;
    error_log logs/400.log error;
}

In case you need documentation http://nginx.org/en/docs/ngx_core_module.html#error_log

1
  • but its not logging the 400 errors.. 
    – Nijo
    May 24, 2016 at 10:27
0

What is needed is to setup a separate access log for the errors. Terminology can be confusing, to quote the documentation:

NGINX writes information about encountered issues of different severity levels to the error log.

NGINX writes information about client requests in the access log right after the request is processed.

So even though 400 is an HTTP error code, it is typically found in the access log, as per the configuration.

To use the example in the question:

error_page 400 /400.html;

location /400.html {
    access_log  /var/log/nginx/400.log  main;
    error_log   /var/log/nginx/400_error.log info;
}

The same approach can be used in a server block, as appropriate. The example assumes that there is a log_format main defined elsewhere, and that you're also interested in actual errors.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .