带有错误状态代码的nginx单独访问日志文件

带有错误状态代码的nginx单独访问日志文件,nginx,Nginx,我得到了域domainName.com的nginx配置文件,它将访问日志写入文件/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+xm

我得到了域
domainName.com
nginx
配置文件,它将访问日志写入文件
/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;
        }
}
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"
是否可以将此配置文件更改为允许nginx使用http状态代码
504
502
写入错误请求,以单独命名文件,例如
/var/log/nginx/bad.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;
        }
}
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"

如果您不介意为此错误创建自定义错误页,那么这是可能的

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;
    }
}

您需要让文件
/root/Dropbox/nodeApps/nodeJsProject/port/public/50x.html

此方法仅当
node.js
站点在端口9000上停止时有效。如果它正在运行,它会用自己的
node.js
50x
页面做出响应,因此
错误。access.log
保持干净。