2

I have nginx server that sends 404 errors to apache httpd at 1.1.1.1:8081. How can I pass all original headers to the error server?

I have the following config:

server {
   error_page 404 = http://1.1.1.1:8081/404.php?remote=1;
}

When examining headers in 404.php I get client original ip but no cookies from the client; also, I get request uri set to 404.php?remote=1 and not url that caused the 404 error:

<?php
  // 404.php file
  file_put_content("log.txt", $_SERVER['REQUEST_URI'], FILE_APPEND); // :(
  file_put_content("log.txt", $_SERVER['REMOTE_ADDR'], FILE_APPEND); // Ok
  file_put_content("log.txt", $_COOKIE['MyCookie'], FILE_APPEND); // :( empty
?>
rlib
  • 195
  • 1
  • 1
  • 7

1 Answers1

1

It isn't pretty, but one possible solution is to use a named location with a proxy pass to your error handler and then add as many custom headers as you need with the desired information. It means customising the 404.php file also.

For example:

error_page 404 = @handler;

location @handler {
    rewrite ^ /404.php?remote=1? break;
    proxy_set_header X-Request $request_uri;
    proxy_set_header X-Remote $remote_addr;
    proxy_set_header X-Cookie $cookie_some_cookie_name;
    proxy_pass http://1.1.1.1:8081;
}
Richard Smith
  • 11,859
  • 2
  • 18
  • 26
  • I did this myself 30 mins before your answer! :) Thanks! – rlib Dec 27 '15 at 10:07
  • This did not work for me: rewrite ^ /404.php?remote=1 break; I corrected this to rewrite ^(.*)$ /404.php?remote=1 break; and this is ok. – rlib Dec 27 '15 at 14:32
  • I don't like to use `^(.*)$` unless I need the capture. I don't know why `^` caused you a problem, but it *is* a common technique when using `rewrite` to unconditionally replace a URI. – Richard Smith Dec 27 '15 at 14:44