1

Is there a way to access the server response from php once Nginx detects an error, and mail the server response to the admins while at the same time show a custom error page to the visitor?

Currently I am able to delegate error pages to PHP and from there can I send a mail to the admin with the information I get from nginx about the request, but it detects everything as a GET request, looses all POST information, and I have found no way to access the actual response so I can mail that to the admin, so basically I'm sending a very incomplete report about the error.

Is there a way to not loose the POST information, and read the response from the server once the request is delegated to PHP? Is there any other way to mail the error page while at the same time show customized error page?

This is my current nginx setup (The GET params attached to the error_page clause are the only way I found to pass some information to PHP about the backend server that generated the error):

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;
}

This is an example of the email I'm sending from PHP for a POST request that generated an error:

_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] => __utma=25821092.617945397.1386681757.1424269387.1424279038.1469; Akamai_AnalyticsMetrics_clientId=20F2C6C63C0941486F0568FD14A252190904562C; trc_cookie_storage=usatodaydemo%253Asession-data%3Dv2_177fa1cd37c772080393b0e4007ac4e6_582a3853-af49-45d6-9600-d7481e581c4a_1429209499_1429209499_CKLTqAQQkAk%7Ctaboola%2520global%253Auser-id%3D582a3853-af49-45d6-9600-d7481e581c4a; __auc=0f2b68a314c8a0dc7ab8c361ffd; SESSee59450aceeb2ad9ba034cb2ae7af965=sDwitzc8TjB-UrjReaVWF0h5X-MjHHa8xJEBwPyd5fI; SSESSee59450aceeb2ad9ba034cb2ae7af965=ztTP58Pqe1GnuolI2nlm56pWuafDDHlTZHGnVIX-XCg; IBCTY=Bogotá; IBCTR=CO; pa-submit=%20(109-1456229200023; SESSe4f5b902a099a887d938a023f3196d9f=jvAgtxfWQTEdn7brQWvDzLAX0GLKI5rq4AMESZjBpPc; SSESSe4f5b902a099a887d938a023f3196d9f=WDVksh3fUArALczBiCq-BiZYANsiDgn_wUVM2RZ1sr8; _gat=1; _ga=GA1.2.617945397.1386681757; IBPOP=0$|0; __unam=e216ae8-142dcc6664e-75f824f6-43252; has_js=1; Drupal.tableDrag.showWeight=0
    [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
)
jacmkno
  • 115
  • 7

0 Answers0