nginx proxy_redirect 作用

准备环境 springboot /redirect controller

curl http://localhost:10080/redirect -vv
*   Trying ::1:10080...
* Connected to localhost (::1) port 10080 (#0)
> GET /redirect HTTP/1.1
> Host: localhost:10080
> User-Agent: curl/7.76.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Location: http://localhost:10080/ip
< Content-Length: 0
< Date: Wed, 26 May 2021 05:55:45 GMT
< 
* Connection #0 to host localhost left intact

1. 默认配置,即proxy_redirect default;

location / {
                proxy_pass http://localhost:10080/;
        }

效果

curl -vv http://localhost/redirect
*   Trying ::1:80...
* connect to ::1 port 80 failed: 拒绝连接
*   Trying 127.0.0.1:80...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /redirect HTTP/1.1
> Host: localhost
> User-Agent: curl/7.76.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 
< Server: nginx/1.19.3
< Date: Wed, 26 May 2021 05:57:02 GMT
< Content-Length: 0
< Location: http://localhost/ip
< Connection: keep-alive
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< 
* Connection #0 to host localhost left intact

2. proxy_redirect default;

location / {
                proxy_pass http://localhost:10080/;
                proxy_redirect default;
        }

注意,proxy_redirect default必须在proxy_pass下方配置

效果

curl -vv http://localhost/redirect
*   Trying ::1:80...
* connect to ::1 port 80 failed: 拒绝连接
*   Trying 127.0.0.1:80...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /redirect HTTP/1.1
> Host: localhost
> User-Agent: curl/7.76.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 
< Server: nginx/1.19.3
< Date: Wed, 26 May 2021 06:02:20 GMT
< Content-Length: 0
< Location: http://localhost/ip
< Connection: keep-alive
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< 
* Connection #0 to host localhost left intact

与方案1等同

3. proxy_redirect off;

location / {
                proxy_pass http://localhost:10080/;
                proxy_redirect off;
        }

效果

curl http://localhost:10080/redirect -vv
*   Trying ::1:10080...
* Connected to localhost (::1) port 10080 (#0)
> GET /redirect HTTP/1.1
> Host: localhost:10080
> User-Agent: curl/7.76.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Location: http://localhost:10080/ip
< Content-Length: 0
< Date: Wed, 26 May 2021 06:03:34 GMT
< 
* Connection #0 to host localhost left intact

可以看到,302响应的是内部地址。

 

总结

常规部署情况下,不需要特别配置proxy_redirect或者配置proxy_redirect default即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值