Disable logging for specific useragent in nginx conf











up vote
1
down vote

favorite












I want to disable logging for a specific useragent. This is a part of my current conf-file.



if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}



I've tried adding access_log off; but get the following error:




nginx: [emerg] "access_log" directive is not allowed here




I'm assuming this is because I only have a server block. I need a location block also. I've tried the following code:



location / {
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
}



But I get the following error:




duplicate location "/"




In my conf-file I already have this code:



location / {
try_files $uri $uri/ =404;
}



Can I combine the two location snippets into one? Or how do I proceed?










share|improve this question






















  • The access_log directive includes an if=condition which can be controlled by a map. There's an example here
    – Richard Smith
    Nov 8 at 19:25










  • Thanks! Is there a way to also return 403 if condition=true?
    – Niska
    Nov 8 at 20:10










  • Yes. Use: if ($condition) { return 403; }
    – Richard Smith
    Nov 8 at 20:36










  • Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
    – Niska
    Nov 8 at 20:44












  • No. Negate the map and use if ($condition = 0) { return 403; }. (assuming you're using 1 and 0 in your map)
    – Richard Smith
    Nov 8 at 20:48

















up vote
1
down vote

favorite












I want to disable logging for a specific useragent. This is a part of my current conf-file.



if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}



I've tried adding access_log off; but get the following error:




nginx: [emerg] "access_log" directive is not allowed here




I'm assuming this is because I only have a server block. I need a location block also. I've tried the following code:



location / {
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
}



But I get the following error:




duplicate location "/"




In my conf-file I already have this code:



location / {
try_files $uri $uri/ =404;
}



Can I combine the two location snippets into one? Or how do I proceed?










share|improve this question






















  • The access_log directive includes an if=condition which can be controlled by a map. There's an example here
    – Richard Smith
    Nov 8 at 19:25










  • Thanks! Is there a way to also return 403 if condition=true?
    – Niska
    Nov 8 at 20:10










  • Yes. Use: if ($condition) { return 403; }
    – Richard Smith
    Nov 8 at 20:36










  • Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
    – Niska
    Nov 8 at 20:44












  • No. Negate the map and use if ($condition = 0) { return 403; }. (assuming you're using 1 and 0 in your map)
    – Richard Smith
    Nov 8 at 20:48















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to disable logging for a specific useragent. This is a part of my current conf-file.



if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}



I've tried adding access_log off; but get the following error:




nginx: [emerg] "access_log" directive is not allowed here




I'm assuming this is because I only have a server block. I need a location block also. I've tried the following code:



location / {
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
}



But I get the following error:




duplicate location "/"




In my conf-file I already have this code:



location / {
try_files $uri $uri/ =404;
}



Can I combine the two location snippets into one? Or how do I proceed?










share|improve this question













I want to disable logging for a specific useragent. This is a part of my current conf-file.



if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}



I've tried adding access_log off; but get the following error:




nginx: [emerg] "access_log" directive is not allowed here




I'm assuming this is because I only have a server block. I need a location block also. I've tried the following code:



location / {
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
}



But I get the following error:




duplicate location "/"




In my conf-file I already have this code:



location / {
try_files $uri $uri/ =404;
}



Can I combine the two location snippets into one? Or how do I proceed?







nginx






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 19:11









Niska

73




73












  • The access_log directive includes an if=condition which can be controlled by a map. There's an example here
    – Richard Smith
    Nov 8 at 19:25










  • Thanks! Is there a way to also return 403 if condition=true?
    – Niska
    Nov 8 at 20:10










  • Yes. Use: if ($condition) { return 403; }
    – Richard Smith
    Nov 8 at 20:36










  • Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
    – Niska
    Nov 8 at 20:44












  • No. Negate the map and use if ($condition = 0) { return 403; }. (assuming you're using 1 and 0 in your map)
    – Richard Smith
    Nov 8 at 20:48




















  • The access_log directive includes an if=condition which can be controlled by a map. There's an example here
    – Richard Smith
    Nov 8 at 19:25










  • Thanks! Is there a way to also return 403 if condition=true?
    – Niska
    Nov 8 at 20:10










  • Yes. Use: if ($condition) { return 403; }
    – Richard Smith
    Nov 8 at 20:36










  • Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
    – Niska
    Nov 8 at 20:44












  • No. Negate the map and use if ($condition = 0) { return 403; }. (assuming you're using 1 and 0 in your map)
    – Richard Smith
    Nov 8 at 20:48


















The access_log directive includes an if=condition which can be controlled by a map. There's an example here
– Richard Smith
Nov 8 at 19:25




The access_log directive includes an if=condition which can be controlled by a map. There's an example here
– Richard Smith
Nov 8 at 19:25












Thanks! Is there a way to also return 403 if condition=true?
– Niska
Nov 8 at 20:10




Thanks! Is there a way to also return 403 if condition=true?
– Niska
Nov 8 at 20:10












Yes. Use: if ($condition) { return 403; }
– Richard Smith
Nov 8 at 20:36




Yes. Use: if ($condition) { return 403; }
– Richard Smith
Nov 8 at 20:36












Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
– Niska
Nov 8 at 20:44






Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
– Niska
Nov 8 at 20:44














No. Negate the map and use if ($condition = 0) { return 403; }. (assuming you're using 1 and 0 in your map)
– Richard Smith
Nov 8 at 20:48






No. Negate the map and use if ($condition = 0) { return 403; }. (assuming you're using 1 and 0 in your map)
– Richard Smith
Nov 8 at 20:48














2 Answers
2






active

oldest

votes

















up vote
1
down vote













As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.



For example:



map $http_user_agent $goodagent {
default 1;
~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
}

server {
access_log ... if=$goodagent;

if ($goodagent = 0) { return 403; }

...
}


The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.






share|improve this answer




























    up vote
    0
    down vote













    At the http level declare a map like so.



    map $http_user_agent $ignore_status_checks {
    default 0;
    "~Pingdom.*" 1;
    "~*(StatusCake)" 1;
    "~*mod_pagespeed*" 1;
    "~*NodePing*" 1;
    }


    Then in your server's location block add:



    if ($ignore_status_checks) {
    access_log off;
    }


    This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.






    share|improve this answer























    • @RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
      – Shawn C.
      Nov 9 at 13:54











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214613%2fdisable-logging-for-specific-useragent-in-nginx-conf%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.



    For example:



    map $http_user_agent $goodagent {
    default 1;
    ~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
    }

    server {
    access_log ... if=$goodagent;

    if ($goodagent = 0) { return 403; }

    ...
    }


    The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.






    share|improve this answer

























      up vote
      1
      down vote













      As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.



      For example:



      map $http_user_agent $goodagent {
      default 1;
      ~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
      }

      server {
      access_log ... if=$goodagent;

      if ($goodagent = 0) { return 403; }

      ...
      }


      The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.



        For example:



        map $http_user_agent $goodagent {
        default 1;
        ~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
        }

        server {
        access_log ... if=$goodagent;

        if ($goodagent = 0) { return 403; }

        ...
        }


        The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.






        share|improve this answer












        As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.



        For example:



        map $http_user_agent $goodagent {
        default 1;
        ~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
        }

        server {
        access_log ... if=$goodagent;

        if ($goodagent = 0) { return 403; }

        ...
        }


        The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 9:00









        Richard Smith

        18.9k32137




        18.9k32137
























            up vote
            0
            down vote













            At the http level declare a map like so.



            map $http_user_agent $ignore_status_checks {
            default 0;
            "~Pingdom.*" 1;
            "~*(StatusCake)" 1;
            "~*mod_pagespeed*" 1;
            "~*NodePing*" 1;
            }


            Then in your server's location block add:



            if ($ignore_status_checks) {
            access_log off;
            }


            This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.






            share|improve this answer























            • @RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
              – Shawn C.
              Nov 9 at 13:54















            up vote
            0
            down vote













            At the http level declare a map like so.



            map $http_user_agent $ignore_status_checks {
            default 0;
            "~Pingdom.*" 1;
            "~*(StatusCake)" 1;
            "~*mod_pagespeed*" 1;
            "~*NodePing*" 1;
            }


            Then in your server's location block add:



            if ($ignore_status_checks) {
            access_log off;
            }


            This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.






            share|improve this answer























            • @RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
              – Shawn C.
              Nov 9 at 13:54













            up vote
            0
            down vote










            up vote
            0
            down vote









            At the http level declare a map like so.



            map $http_user_agent $ignore_status_checks {
            default 0;
            "~Pingdom.*" 1;
            "~*(StatusCake)" 1;
            "~*mod_pagespeed*" 1;
            "~*NodePing*" 1;
            }


            Then in your server's location block add:



            if ($ignore_status_checks) {
            access_log off;
            }


            This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.






            share|improve this answer














            At the http level declare a map like so.



            map $http_user_agent $ignore_status_checks {
            default 0;
            "~Pingdom.*" 1;
            "~*(StatusCake)" 1;
            "~*mod_pagespeed*" 1;
            "~*NodePing*" 1;
            }


            Then in your server's location block add:



            if ($ignore_status_checks) {
            access_log off;
            }


            This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 14:01

























            answered Nov 8 at 21:29









            Shawn C.

            3,04111931




            3,04111931












            • @RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
              – Shawn C.
              Nov 9 at 13:54


















            • @RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
              – Shawn C.
              Nov 9 at 13:54
















            @RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
            – Shawn C.
            Nov 9 at 13:54




            @RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
            – Shawn C.
            Nov 9 at 13:54


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214613%2fdisable-logging-for-specific-useragent-in-nginx-conf%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Landwehr

            Reims

            Javascript gets undefined on array