AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 404626
Accepted
lulalala
lulalala
Asked: 2012-07-04 21:29:37 +0800 CST2012-07-04 21:29:37 +0800 CST 2012-07-04 21:29:37 +0800 CST

如何在nginx日志中输出变量进行调试

  • 772

我正在测试 nginx 并想将变量输出到日志文件。我该怎么做以及它将转到哪个日志文件(访问或错误)。

nginx
  • 5 5 个回答
  • 220756 Views

5 个回答

  • Voted
  1. Best Answer
    yuvilio
    2014-03-09T11:58:26+08:002014-03-09T11:58:26+08:00

    您可以通过标头发送 nginx 变量值。便于开发。

    add_header X-uri "$uri";
    

    您会在浏览器的响应标头中看到:

    X-uri:/index.php
    

    我有时会在本地开发期间这样做。

    它也可以方便地告诉您某个小节是否正在执行。只需将它洒在您的子句中,看看它们是否被使用。

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
        add_header X-debug-message "A static file was served" always;
        ...
    }
    
    location ~ \.php$ {
        add_header X-debug-message "A php file was used" always;
        ...
    }
    

    因此,访问像http://www.example.com/index.php这样的 url将触发后一个标头,而访问http://www.example.com/img/my-ducky.png将触发前一个标头。

    • 218
  2. Thomas Decaux
    2015-02-22T09:39:00+08:002015-02-22T09:39:00+08:00

    您可以返回一个简单的字符串作为 HTTP 响应:

    location / {
        add_header Content-Type text/plain;
        return 200 $document_root;
    }
    

    或者使用插值同时观察多个变量:

    location / {
        add_header Content-Type text/plain;
        return 200 "document_root: $document_root, request_uri: $request_uri";
    }
    
    • 64
  3. mgorven
    2012-07-04T21:35:09+08:002012-07-04T21:35:09+08:00

    log_format您可以使用记录您感兴趣的变量的指令来设置自定义访问日志格式。

    • 23
  4. Mark Evans
    2013-11-22T14:30:40+08:002013-11-22T14:30:40+08:00

    另一种选择是在构建 nginx 时包含echo 模块,或者安装OpenResty,它是 nginx 捆绑了一堆扩展(比如 echo)。

    然后你可以简单地在你的配置中添加如下语句:

    echo "args: $args"
    
    • 8
  5. Christopher Horler
    2021-03-07T07:32:37+08:002021-03-07T07:32:37+08:00

    这些都没有回答所问的问题(日志)

    但是,@Victor Aguilar - 对此答案有评论,应该是一个答案!它说明了如何记录变量,并且有效。谢谢!

    https://serverfault.com/a/404628/400075

    即在/etc/nginx/nginx.conf

    error_log /var/log/nginx/error.log debug;

    导致以下类型的日志记录:

    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "SCRIPT_FILENAME: /usr/lib/cgit/cgit.cgi/something.git/cgit.cgi"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "QUERY_STRING"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "QUERY_STRING: "
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "REQUEST_METHOD"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "GET"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "REQUEST_METHOD: GET"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "CONTENT_TYPE"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "CONTENT_TYPE: "
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "CONTENT_LENGTH"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "CONTENT_LENGTH: "
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "SCRIPT_NAME"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "/cgit/cgit.cgi/something.git/cgit.cgi"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "SCRIPT_NAME: /cgit/cgit.cgi/something.git/cgit.
    cgi"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "REQUEST_URI"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "/cgit/cgit.cgi/something.git/"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "REQUEST_URI: /cgit/cgit.cgi/something.git/"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "DOCUMENT_URI"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "/cgit/cgit.cgi/something.git/"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "DOCUMENT_URI: /cgit/cgit.cgi/something.git/"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "DOCUMENT_ROOT"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "/usr/lib"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "DOCUMENT_ROOT: /usr/lib"
    2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "SERVER_PROTOCOL"
    2021/03/06 15:14:48 [debug] 2550#2550: *....
    
    
    • 4

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve