Nginx獲取真實IP

Nginx獲取客戶端真實IP

一: 只有1層轉發的情況
客戶端:192.168.10.61
ng1:192.168.50.221
web:192.168.50.222

ng1:配置文件

# /etc/nginx/nginx.conf

    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        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;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
    }
    
# /etc/nginx/conf.d/proxy.conf

    server {
        listen 80;
        server_name www.cc.com;

      location / {
        proxy_pass http://192.168.50.222; ###反向代理核心
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header REMOTE-HOST  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

web:配置文件

# /etc/nginx/nginx.conf

    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        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;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
    }

# /etc/nginx/conf.d/www.cc.com.conf

    server {
        listen       80;
        server_name  www.cc.com;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

本地訪問,查看訪問日誌

# tailf /var/log/nginx/access.log

    192.168.10.61 - [02/Jul/2019:21:22:40 -0400] "GET / HTTP/1.0" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 
    192.168.10.61 - [02/Jul/2019:21:22:40 -0400] "GET /favicon.ico HTTP/1.0" 404 555 "http://www.cc.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"

二: 多層轉發的情況
客戶端:192.168.10.61
ng1:192.168.50.221
ng2:192.168.50.222
web:192.168.50.223

ng1:配置文件

# /etc/nginx/nginx.conf

    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        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;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
    }
    
# /etc/nginx/conf.d/proxy.conf

    server {
        listen 80;
        server_name www.cc.com;

      location / {
        proxy_pass http://192.168.50.222; ###反向代理核心
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header REMOTE-HOST  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

ng2:配置文件

# /etc/nginx/proxy.conf

    server {
        listen 80;
        server_name www.cc.com;

      location / {
        proxy_pass http://192.168.50.223; ###反向代理核心指令

        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header X-Forwarded-For $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }
      
# /etc/nginx.nginx.conf

    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        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;
        sendfile        on;
        #tcp_nopush     on;
        keepalive_timeout  65;
        #gzip  on;
        include /etc/nginx/conf.d/*.conf;
    }

web:配置文件

# /etc/nginx/conf.d/www.cc.cm.conf

    server {
        listen       80;
        server_name  www.cc.com;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
}

# /etc/nginx/nginx.conf

    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        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;
        sendfile        on;
        #tcp_nopush     on;
        keepalive_timeout  65;
        #gzip  on;
        include /etc/nginx/conf.d/*.conf;
    }

本地訪問,查看訪問日誌

# tailf /var/log/nginx/access.log

    192.168.10.61 - [02/Jul/2019:21:30:34 -0400] "GET / HTTP/1.0" 200 46 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 
    192.168.10.61 - [02/Jul/2019:21:30:34 -0400] "GET /favicon.ico HTTP/1.0" 404 555 "http://www.cc.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章