Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP status code in access_log #175

Closed
mrgharabaghi opened this issue Sep 13, 2024 · 3 comments
Closed

Support HTTP status code in access_log #175

mrgharabaghi opened this issue Sep 13, 2024 · 3 comments
Labels

Comments

@mrgharabaghi
Copy link

mrgharabaghi commented Sep 13, 2024

Describe the feature you'd like to add to nginx

In order to separate the logs to different endpoints, it's a good idea to support HTTP status code like error_page directive.

# nginx.conf

user nginx;
error_log /var/www/log/error.log warn;

http {
    log_format main '$status -> "$request"';

    # Keep the access_log's current functionality.
    access_log /var/www/log/access.log main;

    include /etc/nginx/http.d/example.com.conf;
}
# example.com.conf

server {
    listen 443 ssl;

    access_log 503 off;
    access_log 500 /var/log/nginx/5xx.log main;
    access_log 401 403 404 syslog:server=unix:/var/log/nginx/4xx.sock,nohostname;
}

Describe the problem this feature solves

Separation of logs.

Additional context

Keep the access_log's current functionality.
I except these at the end:

access_log /var/log/nginx/error.log main;
access_log 403 off;
access_log 404 /var/log/nginx/404.log main;
access_log 500 503 /var/log/nginx/5xx.log main;
@mrgharabaghi
Copy link
Author

In addition to the original post, another good option could be like this:

# Turn off logs for 401 and 403 HTTP status code.
access_log 401 403 off;

@lcrilly
Copy link

lcrilly commented Sep 13, 2024

Please see the if parameter to the access_log directive
https://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

This provides a conditional logging facility that meets all the above requirements.

@mrgharabaghi
Copy link
Author

Thank you for the fast response.

# nginx.conf

user nginx;
error_log /var/log/nginx/error.log warn;

http {
    log_format main '$status -> "$request"';

    map $status $loggable {
        403 1;
        default 0;
    }

    include /etc/nginx/http.d/example.com.conf;
}
# example.com.conf

server {
    listen 443 ssl;

    access_log logs/403.log main if=$loggable;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants