一、使用X-Forwarded-For

一级代理 192.168.200.120
二级代理 192.168.200.121
web 192.168.200.122

 一级代理配置

[root@nginx ~]# vim /etc/nginx/conf.d/test-01.conf 
server {
  listen 80;
  server_name www.test-01.org;
  location / {
    proxy_pass http://192.168.200.121:80;
    proxy_http_version 1.1;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}
~

二级代理配置

[root@nginx ~]# vim /etc/nginx/conf.d/test-01.conf 
server {
  listen 80;
  server_name www.test-01.org;
  location / {
    proxy_pass http://192.168.200.122:80;
    proxy_http_version 1.1;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

web配置

[root@nginx conf.d]# vim test.conf 
server {
  listen 80;
  server_name www.test-01.org;
    root /code/test;
    location / {
      index index.html;
  }
}

访问查看日志记录的IP地址

[root@nginx ~]# tailf /var/log/nginx/access.log 
192.168.200.121 - - [19/Jun/2023:19:51:33 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0" "192.168.200.10, 192.168.200.120"

二、使用realip的方式记录真实IP

[root@nginx conf.d]# vim test.conf 

server {
  listen 80;
  server_name www.test-01.org;
    root /code/test;
    set_real_ip_from 192.168.200.120;    # 一级代理的IP地址
    set_real_ip_from 192.168.200.121;    # 二级代理的IP地址
    real_ip_header X-Forwarded-For;      # 从哪个header检索需要的IP地址
    real_ip_recursive on;                # 排除set_real_ip_from出现的IP,剩余的就是真实IP
    location / {
      index index.html;
  }
}

[root@nginx ~]# systemctl reload nginx.service

[root@nginx ~]# tailf /var/log/nginx/access.log 
192.168.200.10 - - [19/Jun/2023:20:02:42 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0" "192.168.200.10, 192.168.200.120"

原文地址:https://blog.csdn.net/L596462013/article/details/131294045

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_8415.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注