做校服的网站,自己做的网站怎么放上网,去长沙旅游攻略及费用,桂林做网站公司如何在 Nginx 中配置以保留真实 IP 地址 1、概述2、nginx配置示例2.1、配置说明2.2、客户端获取真实IP2.2.1、代码说明 3、插曲4、总结 大家好#xff0c;我是欧阳方超#xff0c;可以我的公众号“欧阳方超”#xff0c;后续内容将在公众号首发。
1、概述
当使用nginx作为… 如何在 Nginx 中配置以保留真实 IP 地址 1、概述2、nginx配置示例2.1、配置说明2.2、客户端获取真实IP2.2.1、代码说明 3、插曲4、总结 大家好我是欧阳方超可以我的公众号“欧阳方超”后续内容将在公众号首发。
1、概述
当使用nginx作为反向代理服务器时客户端的请求会经过nginx转发到后端服务器。在这过程中客户端的IP可能会被覆盖导致后端在获取请求的IP时只能获取到nginx所在机器的IP。为了保留客户端的真实IPnginx提供了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 地址。 我是欧阳方超把事情做好了自然就有兴趣了如果你喜欢我的文章欢迎点赞、转发、评论加关注。我们下次见。