Jun 19, 2018 - I use “docker-compose up” to run the php-nginx cannot obtain the client IP, the. Bridge services: nginx: image: nginx:1-alpine networks: lns-network: null. For docker client's sake proxysetheader X-Real-IP $remoteaddr;.
I have a website set up with nginx acting as a reverse proxy to apache 2.2, which is running php. From apache and php's perspective the IP address of all requests is the nginx server. I'd like php to see the same remote IP that nginx sees.
Nginx sets a header X-Real-IP which contains the remote IP that nginx sees. I tried doing something like this in the apache conf:
My hope was that I could set the REMOTE_ADDR environment variable and when php finally gets invoked, it would see the remote IP that nginx sees. I think the php code is doing this:
Anyway, this isn't working. Any ideas? Can you not set REMOTE_ADDR in the apache config file? Thanks.
Pascal MARTIN346k6161 gold badges597597 silver badges621621 bronze badges
Russell NeufeldRussell Neufeld
8 Answers
Not sure whether
REMOTE_ADDR
can be changed that way...Actually, you might need to install / enable another Apache module, like
mod_rpaf
(quoting) :It changes the remote address of the client visible to other Apache modules when two conditions are satisfied.
First condition is that the remote client is actually a proxy that is defined in httpd.conf.
Secondly if there is an incoming X-Forwarded-For header and the proxy is in it's list of known proxies it takes the last IP from the incoming X-Forwarded-For header and changes the remote address of the client in the request structure.
It also takes the incoming X-Host header and updates the virtualhost settings accordingly.
For Apache2 mod_proxy it takes the X-Forwared-Host header and updates the virtualhosts
First condition is that the remote client is actually a proxy that is defined in httpd.conf.
Secondly if there is an incoming X-Forwarded-For header and the proxy is in it's list of known proxies it takes the last IP from the incoming X-Forwarded-For header and changes the remote address of the client in the request structure.
It also takes the incoming X-Host header and updates the virtualhost settings accordingly.
For Apache2 mod_proxy it takes the X-Forwared-Host header and updates the virtualhosts
![Remote_addr Empty Nginx Php Remote_addr Empty Nginx Php](/uploads/1/2/4/3/124325273/947566061.png)
Here's a blog-post about that : Nginx proxy to Apache - access remote host IP address using mod_praf
Update: original link not working right now, but it is available also as a debian package:
Marki555apt-get install libapache2-mod-rpaf
4,19722 gold badges2525 silver badges4646 bronze badges
Pascal MARTINPascal MARTIN346k6161 gold badges597597 silver badges621621 bronze badges
I have solved this using mod_remoteip for Apache. mod_remoteip is for Apache 2.5 but thanks to this guy you can use it on Apache 2.2.x as well.
Download mod_remoteip.c from https://gist.github.com/1042237 and compile it with
![Remote_addr Remote_addr](/uploads/1/2/4/3/124325273/924306368.png)
This should create mod_remoteip.so copy in the modules directory. apxs will also add LoadModule directive in your httpd.conf for newly created module.
Open httpd.conf and check does LoadModule directive for mod_remoteip exists
Add RemoteIPHeader directive in httpd.conf
This directive will instruct mod_remoteip to use X-Forwarded-For value from nginx as remote_addr. You can also use X-Real-IP instead:
Restart the Apache. If you have set the proxy headers in nginx it will work like a charm and remote_addr in Apache will be correct
mileusnamileusna
You may try this simple PHP module
It simply rewrites $_SERVER['REMOTE_ADDR'] with X-Forwarded-For or X-Real-IP whatever header you like. You can accomplish this with many different method, but I just would like to do it as PHP module. So I wrote it.
Yasuo OhgakiYasuo Ohgaki
I don't know whether REMOTE_ADDR can be manipulated - it could be that it can't - but you should be able to get hold of the X-Real-IP header within PHP through something like
$_SERVER['HTTP_X_Real_IP']
or similar - check
phpinfo()
for the correct notation.Also, Apache environment variables set in
Pekka 웃Pekka 웃.htaccess
should be visible in PHP.365k120120 gold badges859859 silver badges10241024 bronze badges
Apache (requires mod_headers module):
REF for SetEnvIf RegEx: https://stackoverflow.com/a/10346093
Works also but doesn't verify input as IP:
In PHP:
-or-
No non-core Apache modules required
Community♦
TrevorwTrevorw
mod_rpaf(sudo apt-get install libapache2-mod-rpaf)
resolved all issues and REMOTE_ADDRR now looks Ok!..
wactowacto
Off the cuff.. but could you pass the header X-Real-IP as a variable to the php using some rewrite magic..? Can't htaccess do stuff with header information before it invokes PHP?
dmpdmp9,94466 gold badges3333 silver badges4646 bronze badges
You probably want to use this actually: http://httpd.apache.org/docs/2.3/mod/mod_remoteip.html. Has quite a bit more features and is maintained by apache themselves.
Edit: I was referring to the mod_rpaf suggestion above.
n_are_qn_are_q