Welcome! Log In Create A New Profile

Advanced

$_SERVER['REMOTE_ADDR'] @ PHP

Posted by steves 
$_SERVER['REMOTE_ADDR'] @ PHP
October 12, 2012 06:18AM
I not sure whether this is possible or not, or even if it goes against the whole concept behind nginx.
I have my nginx configure to work with two nodes (or upstreams, if you will).


                     NGINX
                          / \
                         /   \
              Node 1    Node 2


I have built a piece of code that will register on a specific DB all of the calls being made of some specific functions along with its parameters and IP Address.
The problem is that, the php variable $_SERVER['REMOTE_ADDR'] returns the NGINX IP Address instead of the remote client's one.


Is there a way to retrieve the IP Address that I want?



Thanks for the help,
steves.
Re: $_SERVER['REMOTE_ADDR'] @ PHP
October 12, 2012 07:38AM
Can you show us the configuration for the upstreams?
Re: $_SERVER['REMOTE_ADDR'] @ PHP
October 12, 2012 08:30AM
here's the config:

upstream integration {
server 172.20.0.12:80;
server 172.20.0.13:80;
}

server {
listen 172.20.0.11:80;
server_name my.server.com;
rewrite ^(.*) https://my.server.com$1 permanent;
# Logs
error_log /var/log/nginx/int-error.log;
access_log /var/log/nginx/int-access.log;
}


# HTTPS
server {
listen 172.20.0.11:443 ssl;
# SSL
ssl_certificate "/etc/nginx/ssl/certifacate.pem";
ssl_certificate_key "/etc/nginx/ssl/certificate.key";
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;

# Server Name (SNI)
server_name my.server.com;
# Locations
location / { proxy_pass http://172.20.0.11/index.html; }
location /area1/ {
proxy_pass http://my/area1/;
}
location /area2/ {
proxy_pass http://my/area2/;
}
# Logs
error_log /var/log/nginx/int-ssl-error.log;
access_log /var/log/nginx/int-ssl-access.log;
}



Edited 1 time(s). Last edit at 10/12/2012 08:30AM by steves.
Re: $_SERVER['REMOTE_ADDR'] @ PHP
October 12, 2012 08:44AM
You'll have to insert 'ip_hash;' to your upstream configuration:

upstream integration {
ip_hash;
server 172.20.0.12:80;
server 172.20.0.13:80;
}
Re: $_SERVER['REMOTE_ADDR'] @ PHP
October 12, 2012 12:03PM
The issue is that when I run a piece of code like this on the nodes:

<?php
echo "Your IP: ".$_SERVER['REMOTE_ADDR'];
?>

It returns 172.20.0.11 which is the NGINX machine IP address instead of 10.104.3.61 which own IP.
Re: $_SERVER['REMOTE_ADDR'] @ PHP
October 15, 2012 02:42AM
Even with the 'ip_has;' inserted in you upstream configuration? (+ nginx restart ;))
Re: $_SERVER['REMOTE_ADDR'] @ PHP
October 15, 2012 04:43AM
Yes, I have restarted nginx and it does not work. :\
Re: $_SERVER['REMOTE_ADDR'] @ PHP
October 15, 2012 06:14AM
I found a way to do it.
Basically, I've added the following line to my nginx.conf:

proxy_set_header X-Real-IP $remote_addr;


then, on my PHP script I only had to use $_SERVER['HTTP_X_REAL_IP'] to retrieve the client real IP address.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 120
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready