将PHP数据传递给Nginx

将PHP数据传递给Nginx

问题描述:

我试图通过Nginx访问phpMyAdmin时添加一些额外的日志信息,但我有一些问题试图找出将信息传递给Nginx而不会公开显示的问题。最主要的是我试图在PMA_logUser函数中包含在libraries/logging.lib.php文件中“记录”的用户名/状态。如果你不确定这个函数是什么,那么我将把它包含在这里。将PHP数据传递给Nginx

function PMA_logUser($user, $status = 'ok') 
{ 
    if (function_exists('apache_note')) { 
     apache_note('userID', $user); 
     apache_note('userStatus', $status); 
    } 
} 

这可以让你把用户ID和userStatus并通过模块之间的信息。大多数人使用它来添加日志记录的附加信息,以便检查是否有人正在强制密码或类似的东西。我试图做一些搜索,找不到Nginx的好选择。我做了一些更改,并执行以下操作将信息添加到标题中。

function PMA_logUser($user, $status = 'ok') 
{ 
    if (function_exists('apache_note')) { 
     apache_note('userID', $user); 
     apache_note('userStatus', $status); 
    } else { 
     header("userID: $user"); 
     header("userStatus: $status"); 
    } 
} 

这并不让我补充$sent_http_userID$sent_http_userStatus到自定义log_format把它列入我的日志。但这会让它公开可见。我试着编译Nginx的与头的更多模块并添加以下到我的服务器配置结束

more_clear_headers 'userID'; 
more_clear_headers 'userStatus'; 

这将防止它被送到试图登录的人,但这些变化会阻止我记录我正在寻找的信息。有什么建议吗?

+0

问题是,当你使用Apache的php_module它像本地,复合,但nginx的作品与PHP通过PHP-FPM所以没有从PHP到Nginx的方向,我不知道,但可能没有明确的方式。您可以使用外部存储,如本文中所述http://mauro-stettler.blogspot.com.tr/2010/02/variables-from-php-sessions-in-nginx.html – FZE

有没有设计的方式来实现这一点。但是,如果您只想记录类似当前用户的内容,那么一个选项是使用auth_request模块。

请务必理解它,因为它会将请求数加倍并可能导致问题。

auth_request模块设计用于在继续进行正常处理之前允许传入的请求通过远程系统进行身份验证。这个模块恰好允许认证请求中的值在nginx中使用,而客户端不可见。

假设您有一个快速的PHP脚本,它返回了您希望Nginx记录的值,例如当前登录的用户。该脚本可以位于/var/www/auth/state.php,并通过头部'CurrentUser'和一个http 200响应返回可登录的值。

现有应用程序的其余部分可能位于/ var/www/html /中。这个例子假设你使用php-fpm,但它可以在其他nginx代理模块上工作。

server { 
    .. server name, listen, etc 

    root /var/www/html; 

    # get state for any php requests 
    location ~ \.php$ { 
     # Send all php requests here first 
     # any response other than http 200 will block the request 
     auth_request /getstate; 
     auth_request_set $currentuser $upstream_http_currentuser; 

     # after the auth_request returns http 200, the request will then go here 
     # replace this with your own upstream configuration 
     fastcgi_pass localhost:9000; 
     fastcgi_index index.php; 

     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
    } 

    # auth_requests go this route 
    location /getstate { 
     internal; 
     fastcgi_pass localhost:9000; 

     # state.php must return http 200 response and ideally should be very fast 
     # state.php also needs to return values for logging as headers e.g. 'CurrentUser' 

     fastcgi_param SCRIPT_NAME  /state.php; 
     fastcgi_param DOCUMENT_ROOT /var/www/auth 
     fastcgi_param SCRIPT_FILENAME /var/www/auth/state.php; 

     # don't send the request body unless it's needed by state.php 
     fastcgi_pass_request_body off; 
    } 
} 

当所有的作品,将有可能通过转换响应报头使用auth_request_set变量记录从state.php数据。然后,这些变量可以附加到使用log_format指令访问日志:

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
        '$status $body_bytes_sent "$http_referer" ' 
        '"$http_user_agent" "$http_x_forwarded_for" $currentuser'; 

应用程序的其余部分将继续存在于它的作为不会受到额外的state.php查找当前的形式。

您将需要构建带有--with-http_auth_request_module选项的nginx以使用auth_request模块。

如果你需要一个更具体的例子,请包括你到目前为止的问题中的nginx配置。