# Can I log HTTP 400s to a different log file instead of my default access logs

**URL:** <https://www.ruby-forum.com/t/can-i-log-http-400s-to-a-different-log-file-instead-of-my-default-access-logs/213617>\
**Category:** NGINX\
**Created:** [November 7, 2011, 12:21pm UTC](https://www.ruby-forum.com/t/can-i-log-http-400s-to-a-different-log-file-instead-of-my-default-access-logs/213617 "2011-11-07T12:21:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [November 7, 2011, 12:21pm UTC](https://www.ruby-forum.com/t/can-i-log-http-400s-to-a-different-log-file-instead-of-my-default-access-logs/213617/1 "2011-11-07T12:21:57Z")

</div>

Hello

I’d like to know if its possible for me to log HTTP 400 errors to a  
different log file. Right now they go in the access log.

To give some background, we’ve got this problem where if a single  
resource is requested from chrome, after the resource request is served,  
a http 400 error is logged in the access logs. This I’ve understood to  
be is due to the nature of connections opened by Chrome (2 for every  
request) and one of the connections not been used before its closed. And  
nginx reports a 400 in this instance. I’ve gathered this based on a  
different forum entry. And when this 400 is logged, the  
$request\_filename is /etc/nginx//html

So I’m looking at any possibility where the 400s can be logged else  
where. I tried the following with no luck as well;

server {  
location / {  
if ($request\_filename ~\* /etc/nginx) {  
access\_log /someotherlog.log myformat  
}  
}  
}

Any help/advice is much appreciated. Thanks!!

Cheers  
– Imran

Posted at Nginx Forum:

> **[Can I log HTTP 400s to a different log file instead of my default access logs](https://forum.nginx.org/read.php?2,217913,217913#msg-217913)**
>
> Hello I'd like to know if its possible for me to log HTTP 400 errors to a different log file. Right now they go in the access log. To give some background, we've got this problem where if a single resource is requested from chrome, after the resource...

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2011, 12:38pm UTC](https://www.ruby-forum.com/t/can-i-log-http-400s-to-a-different-log-file-instead-of-my-default-access-logs/213617/2 "2011-11-07T12:38:40Z")

</div>

Also to add on a bit more about the chrome connection issue which is  
causing the 400, I turned on info logging for error logs and when I get  
the 400, it logs a ‘client closed prematurely connection while reading  
client request…’ in the error logs.

Cheers  
– Imran

Posted at Nginx Forum:

> **[Re: Can I log HTTP 400s to a different log file instead of my default access...](https://forum.nginx.org/read.php?2,217913,217916#msg-217916)**
>
> Hello I'd like to know if its possible for me to log HTTP 400 errors to a different log file. Right now they go in the access log. To give some background, we've got this problem where if a single resource is requested from chrome, after the resource...

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2011, 12:39pm UTC](https://www.ruby-forum.com/t/can-i-log-http-400s-to-a-different-log-file-instead-of-my-default-access-logs/213617/3 "2011-11-07T12:39:17Z")

</div>

On 7 Nov 2011 11h21 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> used before its closed. And nginx reports a 400 in this  
> instance. I’ve gathered this based on a different forum entry. And  
> when this 400 is logged, the $request\_filename is /etc/nginx//html

Try (untested):

at the http level:

map $request\_filename $is\_400 {  
default 0;  
/etc/nginx/html 1;  
}

on the vhost config:

error\_page 400 @log-400;

location @log-400 {

access\_log /path/to/400.log;

}

Caveat emptor: I’ve never played with the access\_log directive that  
much.

— appa

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2011, 1:05pm UTC](https://www.ruby-forum.com/t/can-i-log-http-400s-to-a-different-log-file-instead-of-my-default-access-logs/213617/4 "2011-11-07T13:05:15Z")

</div>

On 7 Nov 2011 11h38 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > understood to be is due to the nature of connections opened by  
> > default 0;  
> > /etc/nginx/html 1;  
> > }

Oops. Solly I haven’t my coffe yet 🙂

Try (untested):

at the http level:

map $request\_filename $is\_400 {  
default 0;  
/etc/nginx/html 1;  
}

On the vhost:

if ($is\_400) {  
return 302 @log-400;  
}

location @log-400 {  
access\_log /path/to/400.log;  
}

— appa

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2011, 2:03pm UTC](https://www.ruby-forum.com/t/can-i-log-http-400s-to-a-different-log-file-instead-of-my-default-access-logs/213617/5 "2011-11-07T14:03:25Z")

</div>

Hi

Thanks for your reply. I tried this but didn’t have any luck with it.  
Based on the error reported on the error log (client closed permaturely  
connection while reading client request line), I’m wondering if i can  
instead ignore logging those errors in the access logs. Would you if  
that’s possible? Thanks!!

Cheers  
– Imran

Posted at Nginx Forum:

> **[Re: Can I log HTTP 400s to a different log file instead of my default access...](https://forum.nginx.org/read.php?2,217913,217921#msg-217921)**
>
> Hello I'd like to know if its possible for me to log HTTP 400 errors to a different log file. Right now they go in the access log. To give some background, we've got this problem where if a single resource is requested from chrome, after the resource...

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 7, 2011, 3:20pm UTC](https://www.ruby-forum.com/t/can-i-log-http-400s-to-a-different-log-file-instead-of-my-default-access-logs/213617/6 "2011-11-07T15:20:15Z")

</div>

On 7 Nov 2011 13h03 WET, removed\_email\_address@domain.invalid wrote:

Well you can try this:

location @is-400 {  
access\_log off;  
}

If you want to re-purpose the 400 code you have to use the error\_page  
directive.

— appa
