# Apache Reverse Proxy to Nginx

**URL:** <https://www.ruby-forum.com/t/apache-reverse-proxy-to-nginx/215275>\
**Category:** NGINX\
**Created:** [December 29, 2011, 3:17am UTC](https://www.ruby-forum.com/t/apache-reverse-proxy-to-nginx/215275 "2011-12-29T03:17:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [December 29, 2011, 3:17am UTC](https://www.ruby-forum.com/t/apache-reverse-proxy-to-nginx/215275/1 "2011-12-29T03:17:33Z")

</div>

Hello guys,

I’m having a bit of trouble with getting the proper REMOTE\_ADDR client  
address on my server.

The current configuration is Apache ReverseProxy (mod\_security) —\>  
Ngninx —\> PHP-FPM.

Apache is listening on a public IP, nginx listens on 127.0.0.1, so does  
PHP-FPM. My PHP app returns 127.0.0.1 for REMOTE\_ADDR where it should be  
returning the real client’s IP address.

Apache VirtualHost config:

#public IP  
ServerAdmin removed\_email\_address@domain.invalid  
DocumentRoot /home/domain/public\_html  
ServerName [www.domain.com](http://www.domain.com)

```
RewriteEngine on

ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://127.0.0.1:8080/ # nginx
ProxyPassReverse / http://127.0.0.1:8080/ # nginx

ErrorLog logs/error_log
CustomLog logs/access_log common

```

* * *

Nginx config:

http {  
include mime.types;  
default\_type application/octet-stream;  
client\_max\_body\_size 10M;  
log\_format main ‘$remote\_addr - [$time\_local] “$request”’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;

# large\_client\_header\_buffers 16k;

```
    sendfile on;
    keepalive_timeout 0;
    server_tokens off;
    tcp_nopush off;

    ssl_certificate cert.crt;
    ssl_certificate_key cert.key;
    ssl_ciphers

```

ECDHE-RSA-AES256-SHA:AES256-SHA:CAMELLIA256-SHA:DES-CBC3-SHA;  
# compression  
gzip off;

```
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;

    upstream php_backend {
            ip_hash;
           server 127.0.0.1:9000 max_fails=3 fail_timeout=40s;
           server XX.XX.XX.XX:9000 max_fails=3 fail_timeout=40s;
    }

    server {
            listen 127.0.0.1:8080;
            listen 127.0.0.1:8443 ssl;
            server_name www.domain.com domain.com;

            # Disable access log to save I/O
           access_log off;

            root /home/domain/public_html;
            error_page 403 /404.html;
            error_page 404 /404.html;

            fastcgi_param SERVER_PORT $server_port;

            if ($server_port = 443){
                    set $https on;
            }

            if ($server_port = 80){
                    set $https off;
            }

            location / {
                    index index.php;
            }

            location ~*

```

.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$  
{  
root /home/domain/public\_html;  
expires 30d;  
}

```
            location ~ \.php$ {
                    fastcgi_pass php_backend;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
fastcgi\_param SERVER\_PORT $server\_port;  
fastcgi\_param HTTPS $https;  
set\_real\_ip\_from 127.0.0.1;  
real\_ip\_header X-Forwarded-For;  
include fastcgi\_params;  
}  
}

My question is: How would I make NGINX set REMOTE\_ADDR to the IP in  
X-Forwarded-For so when the PHP script tries to read  
$\_SERVER[‘REMOTE\_ADDR’] it will return the IP address in  
X-Forwarded-For

Right now it returns 127.0.0.1. Am i missing something?

Thanks in advance for any help you can provide.

-J

Posted at Nginx Forum:

> **[Apache Reverse Proxy to Nginx](https://forum.nginx.org/read.php?2,220642,220642#msg-220642)**
>
> Hello guys, I'm having a bit of trouble with getting the proper REMOTE\_ADDR client address on my server. The current configuration is Apache ReverseProxy (mod\_security) ---\> Ngninx ---\> PHP-FPM. Apache is listening on a public IP, nginx listens...
