Lolak Lolak - 1 year ago 126
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 -

server B - example/file.txt

when access 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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download