【问题标题】:Is it possible to get Information from the Nginx Server using php?是否可以使用 php 从 Nginx 服务器获取信息?
【发布时间】:2022-08-17 20:25:58
【问题描述】:

我有一个运行 Nginx 的 docker 容器,我需要在我的一个 php 文件中获取 Nginx 的版本。我尝试使用 docker exec nginx_container nginx -v 它仅在终端中有效,它不返回 $output。

有人知道解决这个问题的正确方法吗?

    标签: php docker nginx


    【解决方案1】:

    如果您的 nginx 提供静态文件,您可以请求其中一个文件,然后查看响应中的 Server 标头。

    为了说明,运行 nginx 并发出请求

    docker run --rm -d -p 8080:80 nginx
    curl -v http://localhost:8080/
    

    响应将在 Server 标头中包含 Nginx 版本。在我的机器上,它是Server: nginx/1.23.0

    【讨论】:

      【解决方案2】:

      这是一个小例子(自从我使用 PHP 以来已经有一段时间了,所以可能有更好的方法来做到这一点):

      $request = curl_init();
      curl_setopt( $request , CURLOPT_URL , "http://localhost/" );
      curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt( $request , CURLOPT_HEADER , true );
      curl_setopt( $request , CURLOPT_NOBODY , true );
      $response = curl_exec( $request );
      $headers = explode("\r\n", $response);
      $server = implode("", preg_grep ('/Server/', $headers));
      echo $server;
      

      结果:

      Server: nginx
      

      就我而言,没有版本信息,因为我们使用以下方法关闭了版本信息:

      bash-5.0# cat/etc/nginx/nginx.conf | grep server_tokens
          server_tokens off;
      

      如果您在同一个 docker 容器中运行 PHP,您可以忽略 curl 并使用 shell_exec 通过 php 运行命令:

      php > echo shell_exec('nginx -v');
      nginx version: nginx/1.18.0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        • 2011-06-10
        • 2011-02-26
        • 2012-05-06
        • 2012-09-24
        相关资源
        最近更新 更多