網(wǎng)站首頁 編程語言 正文
nginx配置x-forwarded-for頭部
本地用tomcat起了一個j2ee的應用,然后又起了一個nginx做反向代理。
nginx.conf:
#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的監(jiān)聽端口為50001
使用了proxy_set_header來配置nginx轉發(fā)的頭部操作。
其中如下配置就是針對xff的:
其中$proxy_add_x_forwarded_for變量的值是當前包的x-forwarded-for變量和remote-addr變量,使用逗號隔開。
所以上面的命令就是把當前的包的x-forwarded-for的值設置為x-forwarded-for和remote-addr的連接。
這樣這個包轉發(fā)給下游時,下游就有了這臺nginx服務器的ip地址。
當client第一次請求nginx服務器時,nginx拿到的x-forwarded-for為null,remote-addr就是client的實際地址,所以第一次的轉發(fā)的xff值就只有client的ip地址,轉發(fā)的nginx的地址是在remote-addr里。
下一臺nginx服務器會把第一臺nginx服務器的地址填入xff。
所以當一臺服務器收到一個包時,上一臺服務器的地址并不在xff里面,必須通過remote-addr拿到。
Controller:
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
顯示:
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命令:
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'
顯示:
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。也是符合預期的。
總結
原文鏈接:https://blog.csdn.net/u010900754/article/details/81160268
相關推薦
- 2022-06-21 C++分析講解類的靜態(tài)成員函數(shù)如何使用_C 語言
- 2022-06-24 超詳細的Python安裝第三方庫常用方法匯總_python
- 2022-05-26 mongoDB數(shù)據(jù)庫索引快速入門指南_MongoDB
- 2022-06-23 Python基于鏈接表實現(xiàn)無向圖最短路徑搜索_python
- 2023-04-19 Android.bp語法和使用方法講解_Android
- 2022-01-31 pytorch:tensor與numpy轉換 & .cpu.numpy()和.numpy()
- 2023-07-30 element中對el-input 自定義驗證規(guī)則
- 2022-05-06 linux/mac上如何查看公網(wǎng)ip
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支