当前位置: 首页 > news >正文

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

如何在 Nginx 中配置以保留真实 IP 地址

  • 1、概述
  • 2、nginx配置示例
    • 2.1、配置说明
    • 2.2、客户端获取真实IP
      • 2.2.1、代码说明
  • 3、插曲
  • 4、总结

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

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;  # 设置真实客户端 IPproxy_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 地址。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。

http://www.lryc.cn/news/477300.html

相关文章:

  • docker对nginx.conf进行修改后页面无变化或页面报错
  • SpringCloudGateway — 网关路由
  • docker pull 拉取镜像失败,使用Docker离线包
  • 轻松理解操作系统 - 轻松了解 inode 是如何管理文件的
  • go中Println和Printf的区别
  • C++现代教程七之模块
  • AVLTree
  • Java面向对象 C语言字符串常量
  • SpringBoot+Thymeleaf电商系统
  • 了解数据库并发产生的问题
  • openstack之guardian介绍与实例创建过程
  • 新一代跟踪器StrongSORT: Make DeepSORT Great Again论文解析—让 DeepSORT 再次伟大
  • SAP ABAP开发学习——RFC
  • Elasticsearch里的索引index是什么概念?(ChatGPT回答)
  • 安全性测试
  • ComfyUI和Photoshop相结合,PS内实现:文生图,图生图,高清放大,局部重绘,面部修复,设计师福音
  • 使用 map 和 reduce 提取对象数组中的 id 并组成新数组
  • Zero-Shot Relational Learning for Multimodal Knowledge Graphs
  • AUTOSAR COM 模块的主要功能导读以及示例
  • VMware下Centos7虚拟机数据盘/data目录(非lvm)不停机热扩容实操
  • 易盾增强版滑块识别/易盾识别/滑块识别/增强版滑块识别/易盾滑块本地识别
  • DMRl-Former用于工业过程预测建模和关键样本分析的数据模式相关可解释Transformer网络
  • 向量模型Jina Embedding: 从v1到v3论文笔记
  • Spring学习笔记(一)
  • Java编程基础
  • C++【string类,模拟实现string类】
  • Jupyter lab 打开时默认使用 Notebook 而不是浏览器
  • 【linux】ubunda repo是什么
  • 【MySQL】深层理解索引及特性(重点)--下(12)
  • 无人机声学侦测算法详解!