nginx http proxy 反向代理

前言

  • nginx 1.14.2

proxy配置

    location / {
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header    Host               $host:$server_port;
        proxy_set_header    Remote_Addr        $remote_addr;
        proxy_set_header    X-Real-IP          $remote_addr;
        proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto  $scheme;
        proxy_set_header    X-Nginx-Proxy      true;
        
        # index页面设置
        index index;
    }
  • proxy_set_header Host $host:$server_port;:告知后端,客户端请求的真实host和port。
  • proxy_set_header X-Real-IP $remote_addr;:真实IP,即客户端的IP。需要用ngx_http_realip_module 一起使用。
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;:代理走过的ip路径。X-Forwarded-For的值可能为:218.107.55.254, 192.168.0.3, 192.168.0.99, ...,对应Client IP, Proxy1 IP, Proxy2 IP, ...

全部追加到url中

    location [location表达式] {
        proxy_pass [proxy_pass表达式];
        ....
    }

proxy_pass 表达式中端口号后面不包含/字符时,location匹配的部分全部追加到proxy_pass表达式后面。

    location / {
        proxy_pass http://127.0.0.1:8080;
        ....
    }
request urllocation 表达式匹配的部分proxy_pass 表达式proxy 后的 url是否符合预期
http://127.0.0.1/logo.png//logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/logo.png符合预期
http://127.0.0.1/images/logo.png//images/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/images/logo.png符合预期
http://127.0.0.1/user/add//user/addhttp://127.0.0.1:8080http://127.0.0.1:8080/user/add符合预期
http://127.0.0.1/oa/logo.png/oa//oa/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/oa/logo.png符合预期
http://127.0.0.1/oa/images/logo.png/oa//oa/images/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/oa/images/logo.png符合预期
http://127.0.0.1/oa/user/add/oa//oa/user/addhttp://127.0.0.1:8080http://127.0.0.1:8080/oa/user/add符合预期
http://127.0.0.1/oa/logo.png/oa/oa/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/oa/logo.png符合预期
http://127.0.0.1/oa/images/logo.png/oa/oa/images/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/oa/images/logo.png符合预期
http://127.0.0.1/oa/user/add/oa/oa/user/addhttp://127.0.0.1:8080http://127.0.0.1:8080/oa/user/add符合预期
http://127.0.0.1/oauser/add/oa/oauser/addhttp://127.0.0.1:8080http://127.0.0.1:8080/oauser/add符合预期
注:按照使用代理方式将url中的端口号去掉进行评判,比如将http://127.0.0.1/logo.png代理成ttp://127.0.0.1:8080/logo.png。

proxy_pass 表达式中端口号后面不包含/字符时:

  • location 表达式是否以/结尾均可。

部分追加到url中

proxy_pass 表达式中端口号后面包含/字符时,对location匹配的部分进行截取操作,将截取所得的部分追加到proxy_pass表达式后面。

    location / {
        proxy_pass http://127.0.0.1:8080/oa/;
        ....
    }
request urllocation表达式截取所得的部分proxy_pass 表达式proxy 后的 url是否符合预期
http://127.0.0.1/logo.png/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/logo.png符合预期
http://127.0.0.1/images/logo.png/images/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/images/logo.png符合预期
http://127.0.0.1/user/add/user/addhttp://127.0.0.1:8080/http://127.0.0.1:8080/user/add符合预期
http://127.0.0.1/logo.png/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/logo.png不符合预期
http://127.0.0.1/images/logo.png/images/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/images/logo.png不符合预期
http://127.0.0.1/user/add/user/addhttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/user/add不符合预期
http://127.0.0.1/logo.png/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oalogo.png不符合预期
http://127.0.0.1/images/logo.png/images/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/imageslogo.png不符合预期
http://127.0.0.1/user/add/user/addhttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/useradd不符合预期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/oa/logo.png符合预期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/oa/images/logo.png符合预期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/http://127.0.0.1:8080/oa/user/add符合预期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/logo.png不符合预期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/images/logo.png不符合预期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/http://127.0.0.1:8080/user/add不符合预期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/logo.png符合预期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/images/logo.png符合预期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/user/add符合预期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oalogo.png不符合预期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oaimages/logo.png不符合预期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oauser/add不符合预期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa//logo.png符合预期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa//images/logo.png符合预期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa//user/add符合预期
http://127.0.0.1/oauser/add/oauser/addhttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/user/add不符合预期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/logo.png符合预期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/images/logo.png符合预期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/user/add符合预期
http://127.0.0.1/oauser/add/oauser/addhttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oauser/add符合预期
注:按照使用代理方式将url中的端口号去掉进行评判,比如将http://127.0.0.1/logo.png代理成ttp://127.0.0.1:8080/logo.png。

proxy_pass 表达式中端口号后面包含/字符时:

  • proxy_pass 表达式/结尾时,location 表达式也应以/结尾
  • proxy_pass 表达式不以/结尾时,location 表达式也不应以/结尾

多次代理配置

location / {
    proxy_pass http://127.0.0.1:8081;
    proxy_set_header   Host               $host:$server_port;
    proxy_set_header   Remote_Addr        $remote_addr;
    proxy_set_header   X-Real-IP          $remote_addr;
    proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;

    set $xscheme $scheme;
    if ( $http_x_nginx_proxy ) {
        set $xscheme $http_x_forwarded_proto;
    }
    proxy_set_header   X-Forwarded-Proto  $xscheme;
    proxy_set_header   X-Nginx-Proxy      true;
    index index;
}

以某个路径开头

    location ^~ /api {
        ...
    }

或者

    location ~* ^/api {
         ...
    }

weblogic:https请求转成http请求

    location / {
        ...
        proxy_set_header WL-Proxy-SSL true; 
        ...
    }
  • proxy_set_header WL-Proxy-SSL true; https请求转成http请求时,weblogic设置

tomcat:https请求转成http请求

    location / {
        ...
        proxy_set_header X-Forwarded-Proto $scheme;
        ...
    }
  • proxy_set_header X-Forwarded-Proto $scheme; https请求转成http请求时,tomcat设置

nginx多次代理:(tomcat中)scheme或https丢失问题

  • 在第1层代理上设置X-Forwarded-Proto
    location / {
        ...
        proxy_set_header    X-Forwarded-Proto  $scheme;
        proxy_set_header    X-Nginx-Proxy      true;
        ...
    }
  • 在第2层代理上再次设置X-Forwarded-Proto
    location / {
        ...
        set $xscheme $scheme;
        if ( $http_x_nginx_proxy ) {
            set $xscheme $http_x_forwarded_proto;
        }
        proxy_set_header   X-Forwarded-Proto  $xscheme;
        ...
    }
  • 在第3层及更多层,参考第2层的设置

nginx多次代理:客户端真实IP丢失 问题

  • 在第1层代理上设置X-Real-IP
    location / {
        ...
        proxy_set_header   X-Real-IP    $remote_addr;
        ...
    }
  • 在第2层代理上再次设置X-Real-IP
    location / {
        ...
        proxy_set_header X-Forwarded-Proto $http_x_real_ip;
        ...
    }
  • 在第3层及更多层,参考第2层的设置

nginx配置ws

ws协议必须要有的

	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection 'upgrade';

nginx 全局添加

在nginx.conf中添加

单Server添加

server {
    listen        80;
    server_name   xxx;
    root          /xxx/xxx;
    
    # ws://
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection 'upgrade';    
    
    ...

}

局部添加

    location / {
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header    Host               $host:$server_port;
        proxy_set_header    Remote_Addr        $remote_addr;
        proxy_set_header    X-Real-IP          $remote_addr;
        proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto  $scheme;
        proxy_set_header    X-Nginx-Proxy      true;
        
        # ws://
	    proxy_set_header Upgrade $http_upgrade;
	    proxy_set_header Connection 'upgrade';    
        
        # index页面设置
        index index;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值