nginx proxy forward and stream_socket_get_name

Viewed 112

How to make stream_socket_get_name return the real IP address from the remote client?

$ip = stream_socket_get_name($socket, true);

The above returns something like 127.0.0.1:39872

nginx

server {
    listen 8443 ssl;
    server_name  websocket.example.com;
    
    ssl_certificate  /var/ini/ssl/public.crt;
    ssl_certificate_key  /var/ini/ssl/private.key;
    
    location / {
        proxy_redirect  off;
        
        proxy_pass  http://127.0.0.1:9000/;
        proxy_http_version  1.1;
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection "upgrade";
        proxy_read_timeout  300;
        
        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;
    }
}
1 Answers

The problem is stream_socket_get_name() operates on a file handle. In this case the file handle always connects to your proxy, so you can only get the proxy information. But you see those proxy_set_header directives? Those are how the remote IP is and remote port could be passed in. In your PHP, you'd have to check the values for the headers.

Related