nginx(四):如何在 Nginx 中配置以保留真实 IP 地址

大家好,我是欧阳方超,可以我的公众号“欧阳方超”,后续内容将在公众号首发。在这里插入图片描述

1、概述

当使用nginx作为反向代理服务器时,客户端的请求会经过nginx转发到后端服务器。在这过程中,客户端的IP可能会被覆盖,导致后端在获取请求的IP时只能获取到nginx所在机器的IP。为了保留客户端的真实IP,nginx提供了X-Real-IP 和 X-Forwarded-For 两个 HTTP 头信息。

2、nginx配置示例

在nginx的配置文件中需要添加以下内容:

server {
    listen 80;

    location / {
        proxy_pass http://backend_server;  # 替换为具体的后端服务器地址
        proxy_set_header Host $host;  # 设置原始请求的 Host 头
        proxy_set_header X-Real-IP $remote_addr;  # 设置真实客户端 IP
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  # 添加 X-Forwarded-For 头
    }
}

2.1、配置说明

proxy_pass: 指定请求转发到的后端服务器地址。
proxy_set_header Host $host: 将请求的 Host 头设置为原始请求的 Host。
proxy_set_header X-Real-IP $remote_addr: 将真实的客户端 IP 地址添加到 X-Real-IP 头中。这里的 $remote_addr 是 Nginx 的内置变量,代表直接连接的客户端 IP。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for: 将客户端 IP 地址附加到 X-Forwarded-For 头中。如果该头已经存在,则会在其后追加新的 IP 地址。

2.2、客户端获取真实IP

在后端应用中,需要从请求中读取这些头信息以获取真实的客端IP。以下为示例代码:

import javax.servlet.http.HttpServletRequest;

public class NetworkUtils {
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.isEmpty()) {
            ip = request.getHeader("X-Forwarded-For");
            if (ip != null && ip.contains(",")) {
                ip = ip.split(",")[0];  // 获取第一个 IP
            }
        }
        return (ip != null && !ip.isEmpty()) ? ip : request.getRemoteAddr();
    }
}

2.2.1、代码说明

获取 X-Real-IP: 首先检查 X-Real-IP 头,如果存在则返回该值。
获取 X-Forwarded-For: 如果 X-Real-IP 不存在,则检查 X-Forwarded-For。如果它包含多个 IP 地址(逗号分隔),则取第一个,通常这是原始客户端的 IP。
回退到 getRemoteAddr(): 如果以上两个头都不存在,则使用 request.getRemoteAddr() 获取直接连接的 IP 地址。

3、插曲

由于我的nginx设置了https请求方式(之前文章提到过),当前端以http方式请求接口时,请求被重定向到 HTTPS ,此时浏览器会发起一个新的请求。确保 HTTPS 服务器(如 Nginx)也正确配置了 CORS。确保 Nginx 的配置中没有阻止或修改 CORS 头。可以在 Nginx 的 HTTPS 配置中添加类似以下内容:

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization';

接着前端尝试访问接口,发现报了405错误(method not allowed),这是因为 301/302 重定向默认会将 POST 请求转换为 GET 请求。这是 HTTP 协议的标准行为。解决方法是使用307/308重定向。307是 临时重定向,保持原有的 HTTP 方法, 308是永久重定向,保持原有的 HTTP 方法。
完整nginx配置如下:


#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 80;
        server_name localhost;
        #return 301 https://$host$request_uri;
        #rewrite ^ https://$host$request_uri permanent;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization';
        #rewrite ^(.*)$ https://$host$1 permanent;

        return 308 https://$host$request_uri;
    }

    server {
        listen       443 ssl;
        #listen       80;
        server_name  localhost;

        ssl_certificate /usr/local/nginx/ssl/demo.crt; #证书路径
        ssl_certificate_key /usr/local/nginx/ssl/demo.key; #私钥路径

        ssl_protocols TLSv1.2 TLSv1.3;  # 启用的TLS版本
        ssl_ciphers HIGH:!aNULL:!MD5;  # 加密套件
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /test/ {
            proxy_pass http://192.168.25.34:3010/;
            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;
        }
}

另外,下图展示了浏览器确实发出了两个请求:
在这里插入图片描述

4、总结

安全性: 当处理来自不受信任源的 X-Forwarded-For 和 X-Real-IP 时,需要谨慎,因为这些头可以被伪造。确保应用只在可信任的代理后面运行。
多级代理: 如果有多个代理服务器,确保每个代理都正确设置这些头信息,以便最终服务器能够获得真实的客户端 IP。
通过以上配置和代码示例,可以有效地在 Nginx 中保留并传递真实的客户端 IP 地址。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值