广告位联系
返回顶部
分享到

nginx配置x-forwarded-for头部的方法介绍

nginx 来源:互联网 作者:佚名 发布时间:2023-01-09 00:34:10 人浏览
摘要

nginx配置x-forwarded-for头部 本地用tomcat起了一个j2ee的应用,然后又起了一个nginx做反向代理。 nginx.conf: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

nginx配置x-forwarded-for头部

本地用tomcat起了一个j2ee的应用,然后又起了一个nginx做反向代理。

nginx.conf:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

#user  nobody;

worker_processes  1;

  

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

  

#pid        logs/nginx.pid;

  

  

events {

    worker_connections  1024;

}

  

  

http {

    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;

  

    sendfile        on;

    #tcp_nopush     on;

  

    #keepalive_timeout  0;

    keepalive_timeout  65;

  

    #gzip  on;

  

    server {

        listen       50001;

        server_name  localhost;

  

        #charset koi8-r;

  

        #access_log  logs/host.access.log  main;

  

        location / {

            root   html;

            index  index.html index.htm;

        }

  

        location /ly {

            proxy_pass   http://127.0.0.1:8080/hello.do;

            proxy_set_header            Host $host; 

            proxy_set_header            X-real-ip $remote_addr; 

            proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for; 

        }   

  

        #error_page  404              /404.html;

  

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

    include servers/*;

}

这里配置了nginx的监听端口为50001

使用了proxy_set_header来配置nginx转发的头部操作。

其中如下配置就是针对xff的:

其中$proxy_add_x_forwarded_for变量的值是当前包的x-forwarded-for变量和remote-addr变量,使用逗号隔开。

所以上面的命令就是把当前的包的x-forwarded-for的值设置为x-forwarded-for和remote-addr的连接。

这样这个包转发给下游时,下游就有了这台nginx服务器的ip地址。

当client第一次请求nginx服务器时,nginx拿到的x-forwarded-for为null,remote-addr就是client的实际地址,所以第一次的转发的xff值就只有client的ip地址,转发的nginx的地址是在remote-addr里。

下一台nginx服务器会把第一台nginx服务器的地址填入xff。

所以当一台服务器收到一个包时,上一台服务器的地址并不在xff里面,必须通过remote-addr拿到。

Controller:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public class MainController extends HttpServlet {

  

    public void doGet(HttpServletRequest request,

                      HttpServletResponse response)

            throws ServletException, IOException

    {

        PrintWriter out = response.getWriter();

        out.println("NGINX FORWARD");

        String ssfAddr = request.getHeader("X-Forwarded-For");

  

        String realIp = request.getHeader("X-Real-IP");

  

        String remoteAddr = request.getRemoteAddr();

  

        System.out.println("X-Forwarded-For: " + ssfAddr);

        System.out.println("X-Real-IP: " + realIp);

        System.out.println("remoteAddr: " + remoteAddr);

  

    }

  

}

本地ip为192.168.43.33。

然后我先使用了手机访问了nginx域名:192.168.43.33:50001/ly

显示:

1

2

3

X-Forwarded-For: 192.168.43.1

X-Real-IP: 192.168.43.1

remoteAddr: 127.0.0.1

这里192.168.43.1是手机的ip,127.0.0.1是nginx的ip。且通过x-real-ip可以获取到真实ip。

在使用一个crul命令:

1

curl http://localhost:50001/ly -H 'X-Forwarded-For: unkonw, <8.8.8.8> 1.1.1.1' -H 'X-Real-IP: 2.2.2.2'

显示:

1

2

3

X-Forwarded-For: unkonw, <8.8.8.8> 1.1.1.1, 127.0.0.1

X-Real-IP: 127.0.0.1

remoteAddr: 127.0.0.1

这里客户端就是本机,所以会在xff后面添加一个127.0.0.1。也是符合预期的。


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/u010900754/article/details/81160268
相关文章
  • Nginx隐藏server头信息的实现介绍
    分析 上一篇文章我们搭建了Nginx,请求响应头如下 1 2 3 4 5 6 7 8 9 10 [nginx@node01 sbin]$ curl -I 127.0.0.1:8090 HTTP/1.1 200 OK Server: nginx/1.9.9 Date: Fri, 11
  • nginx配置x-forwarded-for头部的方法介绍

    nginx配置x-forwarded-for头部的方法介绍
    nginx配置x-forwarded-for头部 本地用tomcat起了一个j2ee的应用,然后又起了一个nginx做反向代理。 nginx.conf: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1
  • shell脚本实战之部署nginx脚本实例介绍
    我们写脚本一定要从最简单的脚本开始,循序渐进,不要一上来就想着用for循环,用函数,而应该在写的过程中,突然发现这里可以改为函
  • nginx rewrite参数介绍
    在nginx的配置中,是否对rewrite的配置模糊不清,还有令人迷惑的$1、$2...参数,(其实$1、$2参数在shell脚本中经常用到,用来承接传递的参数
  • Nginx配置之main events块使用介绍
    作用 反向代理 负载均衡 web缓存 配置 nginx的配置主要可以划分为main、events、http、server、location块。 main:置影响nginx全局的指令。一般有运
  • Windows设置nginx开机自启动的方法

    Windows设置nginx开机自启动的方法
    使用环境:Windows 10 专业版,nginx 1.20.1 通过两种方式实现nginx的开机自启动:winws和window计划程序。 一、winws实现nginx开机自启动 1、首先下载
  • Clash Linux服务器安装教程

    Clash Linux服务器安装教程
    Clash软件安装 软件下载地址 github公开地址:https://github.com/Dreamacro/clash 1.使用wget下载linux安装包,保存并解压至/opt/clash文件夹中。 1 2 3 4 m
  • Nginx配置ssl证书(https)的全过程
    如果有防火墙的话,记得开通443端口 准备材料: 1.申请ssl证书,这个如何申请可以到百度搜一下,因为域名服务商不同,这里不做过多赘述; 2.服
  • 在Debian11上安装Openresty服务(Nginx+Lua)的教程
    OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处
  • Nginx的优化、安全与防盗链实例介绍

    Nginx的优化、安全与防盗链实例介绍
    1.Nginx的页面优化 1.1 Nginx的网页压缩 在Nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能。进行相关的配置修改,就能实现Nginx页面
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计