Nginx – Get the correct SERVER_ADDR on Apache when using nginx as reverse proxy

apache-2.4nginxreverse-proxy

(see edit below)

I have nginx forwarding requests to Apache 2.4 and I'm getting 127.0.0.1 as the value of SERVER_ADDR. The correct IP is available only on HTTP_X_SERVER_ADDRESS.

I have installed the rpaf module, but it only fixed the REMOTE_ADDR value (which was previously also 127.0.0.1 and now is the correct remote IP).

Is there any way to get the correct server IP on SERVER_ADDR?

I have the following config on nginx.conf:

proxy_set_header X-Real-IP  $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Server-Address $server_addr;
proxy_set_header Host $host;

EDIT: I just realized that I can use proxy_pass [my-server-ip] instead of proxy_pass 127.0.0.1, are there any drawbacks to that?

Best Answer

For nginx-to-apache in same host and single public IP address (for example 192.168.99.1), there are several available options:

  1. Nginx listen on 192.168.99.1 port 80 and and Apache listen on 192.168.99.1 on other ports except 80 (for example port 8080)

  2. Nginx listen on 192.168.99.1 port 80 and and Apache listen on 127.0.0.1 on port 80 (the same port)

The drawback of option 2 was your earlier problem. And for option 1, you shouldn't have any major drawbacks except some firewall rule to ensure that client not direct connecting to Apache port.