Lấy IP thật client khi sử dụng Google Cloud load balancer với NGINX

0
1650

Khi sử dụng Gcloud Load Balancer, Kết nối tới server backend là từ IP của LB. Vì vậy sẽ không thể hiển thị IP thật của Client trong log reuqest. Để lấy được IP thật của Client ta làm như sau: Tạo 1 file /etc/nginx/real_ip_glb

set_real_ip_from 130.211.0.0/16;
set_real_ip_from 130.211.0.0/22;
set_real_ip_from 35.191.0.0/16;

real_ip_header X-Forwarded-For;
real_ip_recursive on;

Thêm config sau vào vhost nginx:

        include /etc/nginx/real_ip_glb;

Ví dụ:

server {
        listen  80;
        server_name  hoctapit.com;
        access_log  /var/log/nginx/hoctapit.com.access.log custom;
        error_log   /var/log/nginx/hoctapit.com.error.log;
        include /etc/nginx/real_ip_glb;

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect   off;
                proxy_pass http://plf_new_backend;
        }

....

Như vậy đã có thể lấy IP thật của client trong log access như bình thường.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here