![使用 nginx 反向代理上的 php 处理错误,并向管理员发送邮件回复](https://linux22.com/image/677493/%E4%BD%BF%E7%94%A8%20nginx%20%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E4%B8%8A%E7%9A%84%20php%20%E5%A4%84%E7%90%86%E9%94%99%E8%AF%AF%EF%BC%8C%E5%B9%B6%E5%90%91%E7%AE%A1%E7%90%86%E5%91%98%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E5%9B%9E%E5%A4%8D.png)
一旦 Nginx 检测到错误,是否有办法从 php 访问服务器响应,并将服务器响应邮寄给管理员,同时向访问者显示自定义错误页面?
目前,我能够将错误页面委托给 PHP,然后我可以从那里向管理员发送一封邮件,其中包含从 nginx 获取的有关请求的信息,但它将所有内容检测为 GET 请求,丢失所有 POST 信息,并且我发现无法访问实际响应,因此我可以将其邮寄给管理员,所以基本上我发送了一份关于错误的非常不完整的报告。
有没有办法不丢失 POST 信息,并在将请求委托给 PHP 后从服务器读取响应? 有没有其他方法可以邮寄错误页面,同时显示自定义错误页面?
这是我当前的 nginx 设置(附加到子句的 GET 参数error_page
是我发现的将有关生成错误的后端服务器的某些信息传递给 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] => __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
)