Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable logging images in nginx but still allow the get request?

I'm trying to log only java-script files request in the nginx access_log. I tried using the following code i found on this site:

location ~* ^.+.(jpg|jpeg|gif|css|png|html|htm|ico|xml|svg)$ {
   access_log        off;
}

the problem is it doesn't allow the get request at all and i get a 404 error when trying to run the html file that executes the js file in the browse.

I want everything to work just the same but for the access log to log only request for js files. How do i do that?

like image 578
michael Avatar asked Dec 04 '14 08:12

michael


People also ask

How do I disable Nginx logging?

If you wish to turn off the Nginx error logs completely, you need to change the line to : error_log /dev/null crit; This will completely turn off the Nginx error logs on your server.

How do I enable access logs in Nginx?

Enabling the access log The NGINX access Log should be enabled by default. However, if this is not the case, you can enable it manually in the Nginx configuration file ( /etc/nginx/nginx. conf ) using the access_log directive within the http block.

How do I change the log format in Nginx?

The syntax for configuring a log format is: log_format format_name 'set_of_variables_to_define_format'; and the syntax for configuring access log is: access_log /path/to/log_file format_name; #simplest form OR access_log /path/to/log_file [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

Where are Nginx access logs stored?

By default, the access log is located at /var/log/nginx/access. log , and the information is written to the log in the predefined combined format. You can override the default settings and change the format of logged messages by editing the NGINX configuration file ( /etc/nginx/nginx. conf by default).


2 Answers

Put it in the server block and make sure that the "root" is correctly set up. It does work

Working example:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires    +60d;
    access_log off;
}

I have this in the server block and not a location block.

like image 184
Olafur Tryggvason Avatar answered Oct 12 '22 18:10

Olafur Tryggvason


Alternatively you can keep all requests within single location but use access_log with condidional if operator to disable images logging:

map $request_uri $is_loggable {
    ~* ^.+\.(jpg|jpeg|gif|css|png|html|htm|ico|xml|svg)$ 0;
    default                                             1;
}
server {
    location / {
        access_log /path/to/log/file combined if=$is_loggable;
        ...
    }
}

Here combined is a name of default log format.

You say that you want to log only java-script files, so actually you can use even simplier solution:

map $request_uri $is_loggable {
    ~* ^.+\.js$  1;
    default      0;
}
like image 38
Marat Safin Avatar answered Oct 12 '22 20:10

Marat Safin