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 / 问题 / 1115559
Accepted
peter
peter
Asked: 2022-11-14 06:01:53 +0800 CST2022-11-14 06:01:53 +0800 CST 2022-11-14 06:01:53 +0800 CST

如何查看出站连接的 httpd 日志?

  • 772

在 httpd.conf 中为网站指定的访问日志仅显示传入连接的信息。对于出站连接,比如php file_get_contents函数发出的连接,如何获取log?

apache-2.2
  • 4 4 个回答
  • 942 Views

4 个回答

  • Voted
  1. Best Answer
    madman_xxx
    2022-11-14T07:24:09+08:002022-11-14T07:24:09+08:00

    修改第三方 PHP 应用程序可能不是一个可行的解决方案。应该使用 HTTP 代理,至少有两个原因:

    1. 每次任何 PHP 脚本尝试访问外部资源时,它都会使用代理,该代理有自己的访问日志
    2. 代理应该有访问控制规则,只允许某些地址并阻止所有其他地址。
    • 10
  2. Tero Kilkanen
    2022-11-14T06:13:59+08:002022-11-14T06:13:59+08:00

    没有任何解决方案可以直接打开,但必须实现此功能。

    一种可能的方法是:

    对发出传出请求的函数进行包装:

    function log_file_get_contents( $url ) {
        log_request( $url ); // A separate logging function that you create
        file_get_contents( $url );
    }
    

    然后,log_file_get_contents()用于您想要记录的所有请求。

    需要为用于传出请求的其他函数编写类似的包装函数。

    • 5
  3. Greenonline
    2022-11-15T12:28:46+08:002022-11-15T12:28:46+08:00

    将我的回答调整为如何轻松获取应用程序从外部连接到的所有 HTTPS 地址?:

    从使用 lsof 连续监视文件,您可以lsof结合使用 repeat ( -r) 选项。以下每两秒重复一次

    $ lsof -i TCP:80,443 -r 2
    

    这将每 2 秒为您提供一个渐进的历史日志:

    =======
    COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
    firefox 9542 user   27u  IPv4 1068219      0t0  TCP user-300V3Z-300V4Z-300V5Z:37360->192.0.78.23:https (ESTABLISHED)
    firefox 9542 user   48u  IPv4 1053405      0t0  TCP user-300V3Z-300V4Z-300V5Z:45948->ec2-54-213-37-69.us-west-2.compute.amazonaws.com:https (ESTABLISHED)
    =======
    COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
    firefox 9542 user   27u  IPv4 1068219      0t0  TCP user-300V3Z-300V4Z-300V5Z:37360->192.0.78.23:https (ESTABLISHED)
    firefox 9542 user   48u  IPv4 1053405      0t0  TCP user-300V3Z-300V4Z-300V5Z:45948->ec2-54-213-37-69.us-west-2.compute.amazonaws.com:https (ESTABLISHED)
    firefox 9542 user   52u  IPv4 1138942      0t0  TCP user-300V3Z-300V4Z-300V5Z:57602->kul08s01-in-f10.1e100.net:https (SYN_SENT)
    firefox 9542 user  102u  IPv4 1139934      0t0  TCP user-300V3Z-300V4Z-300V5Z:49102->kul09s13-in-f14.1e100.net:https (ESTABLISHED)
    firefox 9542 user  110u  IPv4 1138950      0t0  TCP user-300V3Z-300V4Z-300V5Z:49104->kul09s13-in-f14.1e100.net:https (SYN_SENT)
    =======
    ...
    =======
    COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
    firefox 9542 user   27u  IPv4 1068219      0t0  TCP user-300V3Z-300V4Z-300V5Z:37360->192.0.78.23:https (ESTABLISHED)
    firefox 9542 user   48u  IPv4 1053405      0t0  TCP user-300V3Z-300V4Z-300V5Z:45948->ec2-54-213-37-69.us-west-2.compute.amazonaws.com:https (ESTABLISHED)
    firefox 9542 user   51u  IPv4 1140129      0t0  TCP user-300V3Z-300V4Z-300V5Z:52284->kul09s13-in-f10.1e100.net:https (ESTABLISHED)
    firefox 9542 user  108u  IPv4 1137384      0t0  TCP user-300V3Z-300V4Z-300V5Z:55886->103.229.10.236:https (ESTABLISHED)
    firefox 9542 user  122u  IPv4 1137399      0t0  TCP user-300V3Z-300V4Z-300V5Z:55870->kul08s12-in-f1.1e100.net:https (ESTABLISHED)
    firefox 9542 user  126u  IPv4 1137402      0t0  TCP user-300V3Z-300V4Z-300V5Z:47370->stackoverflow.com:https (SYN_SENT)
    

    注意:每两秒间隔由 分隔=======。

    然后你可以将输出通过管道传输到一个文件,就像这样

    $ lsof -i TCP:80,443 -r 2 > /tmp/http_out.log
    

    如果您不想记录所有传出的 HTTP(S) 请求,您可以输入grep脚本/进程的名称:

    $ lsof -i TCP:80,443 -r 2 | grep <name of your process>
    

    我认为grep应该可以工作,但我无法对其进行测试。


    不可否认,输出不如使用

    watch -n1 lsof -i TCP:80,443 
    

    但这只会为您提供当前传出请求的即时快照:

    dropbox    3280 saml   23u  IPv4 56015285      0t0  TCP greeneggs.qmetricstech.local:56003->snt-re3-6c.sjc.dropbox.com:http (ESTABLISHED) 
    thunderbi  3306 saml   60u  IPv4 56093767      0t0  TCP greeneggs.qmetricstech.local:34788->ord08s09-in-f20.1e100.net:https (ESTABLISHED) 
    mono       3322 saml   15u  IPv4 56012349      0t0  TCP greeneggs.qmetricstech.local:54018->204-62-14-135.static.6sync.net:https (ESTABLISHED) 
    chrome    11068 saml  175u  IPv4 56021419      0t0  TCP greeneggs.qmetricstech.local:42182->stackoverflow.com:http (ESTABLISHED) 
    

    同样,要将输出限制为仅 PHP 进程,您可以使用 grep

    watch -n1 lsof -i TCP:80,443 | grep <name of your process>
    
    • 1
  4. MonkeyZeus
    2022-11-16T09:53:06+08:002022-11-16T09:53:06+08:00

    没有现成的日志。您需要专门针对您的操作系统的解决方案。


    如果您是程序员,那么您可以通过使用命名空间和auto_prepend_filephp.ini 指令来研究重新声明本机 PHP 函数。

    <?php
    namespace override;
    function file_get_contents( string $filename, $use_include_path = false, $context = null, $offset = 0, $length = null )
    {
        // If $filename seems like a URL then do log stuff
        if( preg_match( '/^https?:\\/\\//i', $filename ))
        {
            // Do log stuff
            echo 'Doing log stuff for '.$filename;
        }
        
        return \file_get_contents( $filename, $use_include_path, $context, $offset, $length );
    }
    
    \override\file_get_contents( 'https://onlinephp.io/' );
    

    输出:

    Doing log stuff for https://serverfault.com/
    

    此外,您必须确保对发出 Web 请求的任何其他函数或类执行此操作,例如:

    curl_init("http://www.example.com/");
    
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    
    curl_setopt_array($ch, array(CURLOPT_URL => 'http://www.example.com/'));
    
    fopen('http://www.example.com/', 'r');
    

    可能还有更多我不知道的。

    • 1

相关问题

  • Apache Django Mod_Wsgi - 自动重新加载应用程序

  • Apache:对多个虚拟主机使用相同的目录指令

  • Apache 上的子域不工作 - 找不到服务器

  • PHP 作为 CGI 还是 Apache 模块?

  • 避免将某些丢失的文件记录到 Apache2 错误日志中

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