在nginx反向代理上使用php处理错误,并向管理员

时间:2016-02-23 17:56:44

标签: php email nginx error-handling reverse-proxy

问题是: 一旦Nginx检测到错误,是否有办法从php访问服务器响应,并将服务器响应邮寄给管理员,同时向访问者显示自定义错误页面?

目前最好的解决方法是直接从后端服务器邮寄相关信息,但我无法强迫他们始终向我发送电子邮件。我想检测反向代理中的HTTP错误响应代码,并相应地采取相应的行动,无论后端服务器在做什么。

目前我可以将错误页面委托给PHP,然后我可以向管理员发送邮件,其中包含我从nginx获取的有关请求的信息,但它会将所有内容检测为GET请求,丢失所有POST信息,以及我找不到访问实际响应的方法,所以我可以将其邮寄给管理员,所以基本上我发送的报告非常不完整。

有没有办法不丢失POST信息,并在请求委托给PHP后从服务器读取响应?是否有其他方式邮寄错误页面,同时显示自定义错误页面?

请注意,后端服务器是远程计算机,不一定在我的控制之下。那些服务器正在向Nginx发送它需要的所有信息来做我想要的,它似乎是nginx上缺乏的功能,但我希望有人能想到一个解决方法或其他东西......

这是我当前的nginx设置(附加到error_page子句的GET参数是我发现向PHP传递关于生成错误的后端服务器的一些信息的唯一方法):

location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_connect_timeout 4s;
        proxy_pass $scheme://hsbservers_$scheme;
        proxy_next_upstream error timeout invalid_header http_502 http_504;
        proxy_intercept_errors on;
        server_tokens off;
}

error_page 500 503 502 504 /error.php?err=$status&svr=$upstream_addr&uperr=$upstream_status&sT=$upstream_response_time&pT=$request_time;

location ~ error\.php$ {
        try_files $uri /index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}

这是我从PHP发送的生成错误的POST请求的电子邮件示例:

_SERVER
Array
(
    [USER] => www-data
    [HOME] => /var/www
    [FCGI_ROLE] => RESPONDER
    [QUERY_STRING] => err=500&svr=45.79.216.211:443&uperr=500&sT=1.476&pT=1.610
    [REQUEST_METHOD] => GET
    [CONTENT_TYPE] => multipart/form-data; boundary=----WebKitFormBoundarydq4hsdTNEzIU6Vve
    [CONTENT_LENGTH] => 9550
    [SCRIPT_FILENAME] => /usr/share/nginx/html/error.php
    [SCRIPT_NAME] => /error.php
    [REQUEST_URI] => /node/add/article
    [DOCUMENT_URI] => /error.php
    [DOCUMENT_ROOT] => /usr/share/nginx/html
    [SERVER_PROTOCOL] => HTTP/1.1
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_SOFTWARE] => nginx/1.4.6
    [REMOTE_ADDR] => 181.49.223.170
    [REMOTE_PORT] => 42033
    [SERVER_ADDR] => 172.31.55.210
    [SERVER_PORT] => 443
    [SERVER_NAME] => _
    [HTTPS] => on
    [REDIRECT_STATUS] => 200
    [HTTP_HOST] => hsbnoticias.com
    [HTTP_CONNECTION] => keep-alive
    [HTTP_CONTENT_LENGTH] => 9550
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*\/*;q=0.8
    [HTTP_ORIGIN] => https://hsbnoticias.com
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
    [HTTP_CONTENT_TYPE] => multipart/form-data; boundary=----WebKitFormBoundarydq4hsdTNEzIU6Vve
    [HTTP_REFERER] => https://hsbnoticias.com/node/add/article
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_ACCEPT_LANGUAGE] => es-ES,es;q=0.8,en;q=0.6
    [HTTP_COOKIE] => /* I removed this for security */
    [PHP_SELF] => /error.php
    [REQUEST_TIME_FLOAT] => 1456243321.1031
    [REQUEST_TIME] => 1456243321
)


_POST
Array
(
)


_GET
Array
(
    [err] => 500
    [svr] => 45.79.216.211:443
    [uperr] => 500
    [sT] => 1.476
    [pT] => 1.610
)

0 个答案:

没有答案