michael
michael

Reputation: 91

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?

Upvotes: 9

Views: 9594

Answers (2)

Marat Safin
Marat Safin

Reputation: 1909

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

Upvotes: 1

Olafur Tryggvason
Olafur Tryggvason

Reputation: 4874

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.

Upvotes: 16

Related Questions