Lolak Lolak - 26 days ago 14
Linux Question

php forward visitor ip to nginx

i am trying to send real visitor ip to nginx from php

this is the situation

server A - exmaple.com/a.php

server B - example/file.txt

when access exmaple.com/a.php it download file.txt located on server b

but nginx logs show server A ip as requested download, i guess that correct cause the file.txt downloaded via a.php located on server A

so how can i send the ip of the visitor instead of the server to nginx

i already have this in my nginx config

proxy_set_header X-Real-IP $remote_addr;


thank you

Answer

You would need to add it to your request headers.

$opts['http']['header'] = 'X-Real-IP: ' . $_SERVER['REMOTE_ADDR'] . "\r\n";

You would also need to configure Nginx to accept this, with set_real_ip_from config directives.

A better option would be to use cURL (see @Terra's answer), which gives you a bit more flexibility than the fopen wrappers.

The best option however is just to let Nginx do this. It's far more efficient than piping all this data through PHP. Use proxy_pass.

Comments