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

反向代理配置参数

proxy_pass;
#用来设置将客户端请求转发给的后端服务器的主机,可以是主机名(将转发至后端服务做为主机头首部)、IP地址:端口的方式
#也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持

proxy_hide_header field;
#用于nginx作为反向代理的时候,在返回给客户端http响应时,隐藏后端服务器相应头部的信息,可以设置在http,server或location块
proxy_hide_header ETag;
#隐藏后端服务器ETag首部字段

proxy_set_header;
#可更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的时候,就要更改每一个报文的头部

#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#添加客户端IP和反向代理服务器IP到请求报文头部

“X-Forwarded-For”客户端请求标头字段,$remote_addr附加变量,用逗号分隔。
如果客户端请求标头中不存在“X-Forwarded-For”字段,
则该$proxy_add_x_forwarded_for变量等于该$remote_addr变量。

proxy_set_header X-Real-IP $remote_addr;
#添加HOST到报文头部,如果客户端为NAT上网那么其值为客户端的共用的公网IP地址,常用于在日之中记录客户端的真实IP地址。

#在后端httpd服务器修改配置,添加日志记录X-Forwarded-For字段
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\"" combined

反向代理缓存配置参数

proxy_cache zone_name | off; 默认off
#指明调用的缓存,或关闭缓存机制;Context:http, server, location
#zone_name 表示缓存的名称.需要由proxy_cache_path事先定义

proxy_cache_key string;
#缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;

proxy_cache_valid [code ...] time;
#定义对特定响应码的响应内容的缓存时长,定义在http{...}中

proxy_cache_path;
#设置缓存的路径和其他参数。缓存数据存储在文件中。

#在http配置定义缓存信息
#定义缓存保存路径,proxycache会自动创建
proxy_cache_path /data/nginx/proxycache

levels=1:2:2 
#定义缓存目录结构层次,1:2:2可以生成2^4x2^8x2^8=2^20=1048576个目录

keys_zone=proxycache:20m 
#指内存中缓存的大小,主要用于存放key和metadata(如:使用次数),一般1M可存放8000个左右的key

inactive=120s 
#缓存有效时间

max_size=1g; 
#最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值

proxy_cache_key $request_uri; 
#对指定的数据进行MD5的运算做为缓存的key

proxy_cache_key $scheme$proxy_host$uri$is_args$args;
#默认情况下,指令的值接近字符串

proxy_cache proxycache;
#调用缓存功能,需要定义在相应的配置段,如server{...};或者location等

proxy_cache_valid 200 302 301 10m; 
#指定的状态码返回的数据缓存多长时间

proxy_cache_valid any 1m; 
#除指定的状态码返回的数据以外的缓存多长时间,必须设置,否则不会缓存

响应报文头部配置参数

add_header X-Via $server_addr; 
#当前nginx主机的IP

add_header X-Cache $upstream_cache_status; 
#是否缓存命中

add_header X-Accel $server_name; 
#客户访问的FQDN

环境准备

四台机器:

client :10.0.0.150 测试
nginx1 :10.0.0.7 X-Forwarded-For:client ip
nginx2 :10.0.0.17 X-Forwarded-For:client ip
httpd :10.0.0.27 X-Forwarded-For:client ip,nginx1

1.第一个代理服务器

#定义反向代理
[root@nginx1 conf.d]#pwd
/apps/nginx/conf/conf.d
[root@nginx1 conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server{
    listen 80;
    proxy_cache off;
    proxy_cache_key $host$uri$is_args$args;
    proxy_cache_valid 200 302 301 10m;
    proxy_cache_valid any 5m;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_hide_header ETag;
    add_header X-Via $server_addr;
    add_header X-Cache $upstream_cache_status;
    add_header X-Accel $server_name;

    server_name www.linux2022.com;
    location / {
        root /data/nginx/html/pc;
        proxy_pass http://10.0.0.17;
    }
}
[root@nginx1 conf.d]#nginx -t
[root@nginx1 conf.d]#nginx -s reload

#开启日志格式,记录x_forwarded_for
[root@nginx1 conf.d]#vim /apps/nginx/conf/nginx.conf
http {
    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;
    proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
    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  logs/access.log  main;
   
[root@nginx1 conf.d]#nginx -s reload

2.第二个代理服务器

#定义反向代理
[root@nginx2 conf.d]#pwd
/apps/nginx/conf/conf.d
[root@nginx2 conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server{
    listen 80;
    server_name www.linux2022.com;
    root /data/nginx;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location / {
        proxy_pass http://10.0.0.27;
    }
}

[root@nginx2 conf.d]#nginx -t
[root@nginx2 conf.d]#nginx -s reload

#修改默认页面
[root@nginx2 conf.d]#vim /apps/nginx/html/index.html
10.0.0.17

[root@nginx2 conf.d]#vim /apps/nginx/conf/nginx.conf
    include    /apps/nginx/conf/conf.d/*.conf; #设置Nginx子配置文件优先级,放在server语句块的前面
    server {
        listen       80;
        server_name  localhost;

[root@nginx2 conf.d]#nginx -t
[root@nginx2 conf.d]#nginx -s reload

#开启日志格式,记录x_forwarded_for
[root@nginx2 conf.d]#vim /apps/nginx/conf/nginx.conf
http {
    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;
    
    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  logs/access.log  main;

[root@nginx2 conf.d]#nginx -s reload

3.在第一个proxy上面查看日志

[root@nginx1 ~]#tail -f /apps/nginx/logs/access.log
10.0.0.150 - - [06/Apr/2022:10:10:57 +0800] "GET /index.html HTTP/1.1" 200 10 "-" "curl/7.58.0" "-"

4.在第二个proxy上面查看日志

[root@nginx2 conf.d]#tail -f /apps/nginx/logs/access.log
10.0.0.7 - - [06/Apr/2022:10:10:57 +0800] "GET /index.html HTTP/1.0" 200 10 "-" "curl/7.58.0" "10.0.0.150"

5.后端Apache服务器配置

#安装Apache
[root@httpd ~]#yum -y install httpd 

#后端Apache服务器配置日志格式
[root@httpd ~]#vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\"" combined

[root@httpd ~]#httpd -t
[root@httpd ~]#systemctl start httpd

#设置访问页面
[root@httpd ~]#vim /var/www/html/index.html
10.0.0.27

6.客户端测试访问

[root@client ~]#curl http://www.linux2022.com/index.html
10.0.0.27

7.后端Apache服务器查看日志

[root@httpd ~]#tail -f /var/log/httpd/access_log
10.0.0.17 - - [06/Apr/2022:10:10:57 +0800] "GET /index.html HTTP/1.0" 200 10 "-" "curl/7.58.0" "10.0.0.150, 10.0.0.7"

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐