Swoole-src: Incorrect remote_addr from swoole

Created on 13 Jul 2020  ·  14Comments  ·  Source: swoole/swoole-src

Please answer these questions before submitting your issue. Thanks!

  1. What did you do? If possible, provide a simple script for reproducing the error.

    $req->server['remote_addr']

  2. What did you expect to see?

    192.24.12.56

  3. What did you see instead?

    127.0.0.1

  4. What version of Swoole are you using (show your php --ri swoole)?

swoole

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 4.5.2
Built => Jul 12 2020 18:49:23
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
http2 => enabled
pcre => enabled
zlib => 1.2.8
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?
2.6.32-042stab141.3 #1 SMP Fri Nov 15 22:45:34 MSK 2019 x86_64 GNU/Linux

PHP 7.2.32-1+0~20200710.46+debian9~1.gbp625eb5 (cli) (built: Jul 10 2020 07:22:38) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.32-1+0~20200710.46+debian9~1.gbp625eb5, Copyright (c) 1999-2018, by Zend Technologies

gcc 6.3.0
question

Most helpful comment

Al right, it works! I looked into var_dump($request->header) as you suggested. Yhea, its case sensitive, Now it works as expected. Tnx!

All 14 comments

@jobs-git

What did you do? If possible, provide a simple script for reproducing the error. Include client and server code.

Just the basic setup

    $server = new swoole_http_server("127.0.0.1", 9505, SWOOLE_PROCESS);
    $server->set([
        'worker_num' => 1
    ]);
    $server->on('request', function (swoole_http_request $req, swoole_http_response $res) {
        $res-end($req->server['remote_addr']);
    });
    $server->start();


Here's my program, no problem:

server.php:

<?php

use Swoole\Coroutine\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

use function Swoole\Coroutine\run;

run(function () {
    $server = new Server('127.0.0.1', 9501);
    $server->handle('/', function (Request $request, Response $response) {
        var_dump($request->server['remote_addr']);
        var_dump($request->server['remote_port']);
    });
    $server->start();
});

client.php:

<?php

$context = stream_context_create([
    'socket' => [
        'bindto' => '192.168.8.237:9503',
    ],
]);

echo file_get_contents('http://127.0.0.1:9501', false, $context);
    $server = new swoole_http_server("127.0.0.1", 9505, SWOOLE_PROCESS);
    $server->set([
        'worker_num' => 1
    ]);
    $server->on('request', function (swoole_http_request $req, swoole_http_response $res) {
        $res-end($req->server['remote_addr']);
    });
    $server->start();

You did not give me your client program, which is more important than you give me the server program.

Client is c/c++, but what I am wondering is why does php-fpm from the server get it correctly. While swoole does not.

@jobs-git If the request forwarded by nginx, You need to set the real IP of the client to the http header.

location / {
            root   html;
            proxy_set_header X-Real-IP $remote_addr;
        }

does it go like this?

location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass 127.0.0.10:9000;
}

where proxy_pass is the swoole server.

@matyhtf it does not work. To add more bits of information, swoole server is behind nginx. Through this config

location / {
            proxy_pass 127.0.0.10:9000;
}

I tried chrome as client but I get the same thing. PHP-fpm got it right though.

Please reopen, or should I submit a new issue? Tnx!

@jobs-git In swoole environment you should use $request->header['X-Real-IP']to get client ip

Hmm, I tried also

proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

but got an undefined index from php running swoole server.

@jobs-git

Try to dump all headers, var_dump($request->header), The keys of the array are case sensitive

Al right, it works! I looked into var_dump($request->header) as you suggested. Yhea, its case sensitive, Now it works as expected. Tnx!

If somebody encounters it, it should be: x-real-ip.

Always check the $request->header cases.

Also, more digging reveals that this ain't swoole issue, very application in proxy_pass from nginx have the same behavior.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndyChanCode picture AndyChanCode  ·  3Comments

pthreat picture pthreat  ·  3Comments

godtail picture godtail  ·  4Comments

andreybolonin picture andreybolonin  ·  4Comments

jerryli1 picture jerryli1  ·  4Comments