網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
nginx配置x-forwarded-for頭部
本地用tomcat起了一個(gè)j2ee的應(yīng)用,然后又起了一個(gè)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)聽(tīng)端口為50001
使用了proxy_set_header來(lái)配置nginx轉(zhuǎn)發(fā)的頭部操作。
其中如下配置就是針對(duì)xff的:
其中$proxy_add_x_forwarded_for變量的值是當(dāng)前包的x-forwarded-for變量和remote-addr變量,使用逗號(hào)隔開(kāi)。
所以上面的命令就是把當(dāng)前的包的x-forwarded-for的值設(shè)置為x-forwarded-for和remote-addr的連接。
這樣這個(gè)包轉(zhuǎn)發(fā)給下游時(shí),下游就有了這臺(tái)nginx服務(wù)器的ip地址。
當(dāng)client第一次請(qǐng)求nginx服務(wù)器時(shí),nginx拿到的x-forwarded-for為null,remote-addr就是client的實(shí)際地址,所以第一次的轉(zhuǎn)發(fā)的xff值就只有client的ip地址,轉(zhuǎn)發(fā)的nginx的地址是在remote-addr里。
下一臺(tái)nginx服務(wù)器會(huì)把第一臺(tái)nginx服務(wù)器的地址填入xff。
所以當(dāng)一臺(tái)服務(wù)器收到一個(gè)包時(shí),上一臺(tái)服務(wù)器的地址并不在xff里面,必須通過(guò)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。
然后我先使用了手機(jī)訪(fǎng)問(wèn)了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是手機(jī)的ip,127.0.0.1是nginx的ip。且通過(guò)x-real-ip可以獲取到真實(shí)ip。
在使用一個(gè)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
這里客戶(hù)端就是本機(jī),所以會(huì)在xff后面添加一個(gè)127.0.0.1。也是符合預(yù)期的。
總結(jié)
原文鏈接:https://blog.csdn.net/u010900754/article/details/81160268
相關(guān)推薦
- 2022-04-08 在python中創(chuàng)建表格的兩種方法實(shí)例_python
- 2022-05-20 Modscan和Modsim 兩種Modbus調(diào)試工具使用說(shuō)明
- 2022-04-27 Python中的元組(Tuple)操作實(shí)例詳解_python
- 2022-11-13 git如何還原到某次commit并強(qiáng)制推送遠(yuǎn)程_相關(guān)技巧
- 2022-04-20 Mac中pyenv的安裝與使用教程_python
- 2022-12-04 Android?MQTT與WebSocket協(xié)議詳細(xì)講解_Android
- 2023-05-24 Python使用Pandas處理測(cè)試數(shù)據(jù)的方法_python
- 2022-02-25 commons-fileupload文件上傳的時(shí)候?qū)懭雸D片無(wú)數(shù)據(jù)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支