Home  >  Q&A  >  body text

nginx - How to configure access_log file in if condition

I hope to locate the access logs of different UAs into different access_logs, but it seems that the access_log parameter is not allowed to be placed in the if.
I don't want to locate different requests to different locations through methods such as rewrite. How can I specify accesss_log based on UA ​​in the same location?

曾经蜡笔没有小新曾经蜡笔没有小新2734 days ago738

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 17:31:38

    It can be configured like this, there should be a better way to write it.

            location /log {
                if ($http_user_agent ~ MSIE ) {
                      access_log /var/log/msie.log;
                }
                if ($http_user_agent ~ Opera ) {
                      access_log /var/log/opera.log;
                }
                if ($http_user_agent ~ Webkit ) {
                      access_log /var/log/webkit.log;
                }
                if ($http_user_agent ~ Mozilla ) {
                      access_log /var/log/mozilla.log;
                }
                add_header "Content-Type" "text/plain;charset=UTF-8";
                return 200  "$http_user_agent";
            }

    reply
    0
  • Cancelreply