Maxim Yefremov
Maxim Yefremov

Reputation: 14165

nginx separate access_log file with bad status codes

I got nginx config file for domain domainName.com which write access log to file /var/log/nginx/logName.access.log

server {
    listen 80;
    server_name domainName.com;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype;
    access_log /var/log/nginx/logName.access.log;
    location / {
        proxy_pass    http://127.0.0.1:9000/;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location ~ ^/(min/|images/|ckeditor/|img/|javascripts/|apple-touch-icon-ipad.png|apple-touch-icon-ipad3.png|apple-touch-icon-iphone.png|apple-touch-icon-iphone4.png|generated/|js/|css/|stylesheets/|robots.txt|humans.txt|favicon.ico) {
          root /root/Dropbox/nodeApps/nodeJsProject/port/public; 
          access_log off;
          expires max;
        }
}

Is it possible to change this config file to let nginx write bad requests with http status codes 504 or 502 to separate file named for example /var/log/nginx/bad.access.log

ip - - [07/May/2014:19:28:35 +0400] "GET /url HTTP/1.1" 502 575 "http://domain.com/url" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.1003 Safari/537.36 MRCHROME SOC CHANNEL_inet2amigo"

Upvotes: 0

Views: 1192

Answers (1)

Alexey Ten
Alexey Ten

Reputation: 14354

It's possible if you don't mind to create custom error page for this errors.

server {
    ...
    access_log /var/log/nginx/logName.access.log;
    location / {
        proxy_pass    http://127.0.0.1:9000/;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        error_page 502 504 /50x.html;
    }

    ...

    location = /50x.html {
        internal;
        root /root/Dropbox/nodeApps/nodeJsProject/port/public;
        access_log /var/log/nginx/bad.access.log;
    }
}

And you will need to have file /root/Dropbox/nodeApps/nodeJsProject/port/public/50x.html.

Upvotes: 2

Related Questions