본문 바로가기
Linux

nginx redirect 시에 socket-io 에서 client ip 얻어오기

by GoodDev 2018. 1. 23.

nginx 에서 환경 설정


가. 아래 두개의 옵션을 필수 로 컴파일 한다.

--with-http_ssl_module --with-http_realip_module


나. conf 파일

    server {

        listen 8080;


        real_ip_header    X-Forwarded-For;

        real_ip_recursive on;


        location / {

            proxy_pass http://mysite;

            proxy_set_header Upgrade $http_upgrade;

            proxy_set_header Connection "Upgrade";

            proxy_http_version 1.1;

            proxy_set_header  Host $host;

            proxy_set_header  X-Real-IP $remote_addr;

            proxy_set_header  X-Forwarded-For $remote_addr;

            proxy_set_header  X-Forwarded-Host $remote_addr;

         }


        location /socket.io/ {

            proxy_pass http://mysite;

            proxy_http_version 1.1;

            proxy_set_header Upgrade $http_upgrade;

            proxy_set_header Connection "Upgrade";

            proxy_set_header  Host $host;

            proxy_set_header  X-Real-IP $remote_addr;

            proxy_set_header  X-Forwarded-For $remote_addr;

            proxy_set_header  X-Forwarded-Host $remote_addr;

        }

    }


해당 부분을 추가 한다.


다. node js 에서 ( client = socket )

data.clientip = client.request.headers['x-real-ip'] || client.request.connection.remoteAddress.replace(/^.*:/, '');

으로 해당 ip을 얻어오면 된다.


댓글