实现反向代理客户端IP透传

实现反向代理客户端IP透传
默认情况下,使用反向代理时,后端服务器只能看到访问是从反向代理服务器的IP,无法真正识别到客户端IP。通过配置IP透传实现后端服务器识别到客户端真实IP。

一、Apache后端服务器部署

1.1 安装apaceh

[root@web ~]# yum -y install httpd

1.2 修改配置文件

[root@web ~]# vim /etc/httpd/conf/httpd.conf
...省略
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%  #在此项添加%{X-Forwarded-For}i

1.3 启动apache

[root@web ~]# systemctl start httpd

二、安装反向代理

2.1 安装nginx

[root@nginx ~]# yum -y install nginx

2.2 修改配置文件

[root@nginx ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        root         /usr/share/nginx/html;
        include     /etc/nginx/default.d/*.conf;
        location / {
            index index.html index.php;
            root /data/nginx/html/pc;
            proxy_pass http://10.0.0.18;  #添加此项
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  #添加此项
        }
    }
}

2.3 启动nginx

[root@nginx ~]# systemctl start nginx

三、测试IP透传

#打开客户端浏览器访问10.0.0.8,然后观察后端服务器日记
[root@web ~]#tail /var/log/httpd/access_log
...省略...
10.0.0.3 10.0.0.8 - - [16/Jan/2022:14:11:31 +0800] "GET /favicon.ico HTTP/1.0" 404 196 "http://10.0.0.8/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
10.0.0.3 10.0.0.8 - - [16/Jan/2022:14:11:33 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
上一篇:windows 下安装redis


下一篇:docker安装nginx实现反向代理