0

I have an API powered by a mean stack and served using PM2. It exposes a HTTP endpoint at http://89.89.89.89:8080 (example IP) - I can access this directly using my browser.

I have installed Nginx and I'm using the following configuration to redirect requests to the API on the same server.

server {
        listen 8081;
        server_name example.com;

        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header  X-Forwarded-Host $remote_addr;
        proxy_pass http://127.0.0.1:8080;
        }
}

The above works and I can access the API at http://89.89.89.89:8081 (Nginx Port), however requests to the target are from 127.0.0.1. I would like to forward the real user's IP address to PM2. I've searched and tried several solutions but can't get this to work.

Any assistance or pointers in the right directions are appreciated.

Ralph
  • 862
  • 11
  • 26

1 Answers1

0

you need to have nginx with module --with-http_realip_module or you should be able to do it without it.

The check if the module is present execute nginx -v.

Without the module you should specify the following on your configuration log format. EXAMPLE:

log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent"';

access_log  /var/log/nginx/access.log  main;

real_ip_header X-Forwarded-For;

After this restart you nginx and check the logs.

If the module is installed you need to modify you config accordingly

# Directives for setting real_ip/XFF IP address in log files
set_real_ip_from    192.168.101.10; #IP address of master LB
real_ip_header      X-Forwarded-For;

The real ip module is used to change client source IP address to the value in the header. we wanted to set the real IP address for traffic coming from a server with the IP address 192.168.101.10.

Again, after the changes the nginx service must be restarted.

BANJOSA
  • 370
  • 1
  • 3
  • 15