当前位置: 代码网 > 服务器>服务器>Nginx > nginx proxy_redirect的作用及说明

nginx proxy_redirect的作用及说明

2024年06月17日 Nginx 我要评论
准备环境springboot /redirect controllercurl http://localhost:10080/redirect -vv* trying ::1:10080...*

准备环境

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)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com