亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

目錄
  • nginx配置x-forwarded-for頭部
  • 這里配置了nginx的監(jiān)聽端口為50001
  • 總結(jié)

nginx配置x-forwarded-for頭部

本地用tomcat起了一個j2ee的應(yīng)用,然后又起了一個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轉(zhuǎn)發(fā)的頭部操作。

其中如下配置就是針對xff的:

nginx如何配置x-forwarded-for頭部

其中$proxy_add_x_forwarded_for變量的值是當前包的x-forwarded-for變量和remote-addr變量,使用逗號隔開。

所以上面的命令就是把當前的包的x-forwarded-for的值設(shè)置為x-forwarded-for和remote-addr的連接。

這樣這個包轉(zhuǎn)發(fā)給下游時,下游就有了這臺nginx服務(wù)器的ip地址。

當client第一次請求nginx服務(wù)器時,nginx拿到的x-forwarded-for為null,remote-addr就是client的實際地址,所以第一次的轉(zhuǎn)發(fā)的xff值就只有client的ip地址,轉(zhuǎn)發(fā)的nginx的地址是在remote-addr里。

下一臺nginx服務(wù)器會把第一臺nginx服務(wù)器的地址填入xff。

所以當一臺服務(wù)器收到一個包時,上一臺服務(wù)器的地址并不在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。也是符合預(yù)期的。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持。

分享到:
標簽:forwarded nginx 頭部 服務(wù)器 配置
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定