nginx proxy_redirect的作用及说明

 更新时间:2024年06月05日 08:59:26   作者:hkNaruto  
这篇文章主要介绍了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即可。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • nginx学习总结五(nginx反向代理)

    nginx学习总结五(nginx反向代理)

    Nginx代理与负载均衡配置与优化技巧,方便需要的朋友
    2012-11-11
  • nginx try_files指令判断文件是否存在实例

    nginx try_files指令判断文件是否存在实例

    这篇文章主要介绍了nginx try_files指令判断文件是否存在实例,本文用一个实际需求讲解try_files指令的使用,并给出配置示例,需要的朋友可以参考下
    2015-02-02
  • nginx代理返回代码499问题分析与处理

    nginx代理返回代码499问题分析与处理

    我们通过nginx作为互联网代理服务器,通过它实现我行内部系统向互联网系统的接口访问及调用,但是在使用过程中,不时的会出现大量返回代码为499的问题,所以本文我们对为什么nginx会返回499错误代码展开分析和研究
    2023-06-06
  • Ubuntu下nginx编译安装参数配置

    Ubuntu下nginx编译安装参数配置

    这篇文章主要介绍了Ubuntu下nginx编译安装参数配置,本文总结了一些依赖库的安装以及编码配置参数,需要的朋友可以参考下
    2015-06-06
  • Windows下使用 Nginx 搭建 HTTP文件服务器 实现文件下载功能

    Windows下使用 Nginx 搭建 HTTP文件服务器 实现文件下载功能

    Nginx 是一款轻量级的 HTTP 服务器,采用事件驱动的异步非阻塞处理方式框架,这让其具有极好的 IO 性能,时常用于服务端的反向代理和负载均衡,这篇文章主要介绍了Windows下使用 Nginx 搭建 HTTP文件服务器实现文件下载功能,需要的朋友可以参考下
    2023-03-03
  • Nginx部署JavaWeb项目全过程

    Nginx部署JavaWeb项目全过程

    这篇文章主要介绍了Nginx部署JavaWeb项目全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例

    Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例

    这篇文章主要介绍了Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例,是这两款高人气框架使用Nginx的关键配置点,需要的朋友可以参考下
    2016-01-01
  • nginx地址重定向的方法

    nginx地址重定向的方法

    这篇文章主要介绍了nginx地址重定向的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • 基于Nginx实现访问控制、连接限制

    基于Nginx实现访问控制、连接限制

    Nginx自带的模块支持对并发请求数进行限制, 还有对请求来源进行限制。可以用来防止DDOS攻击。这篇文章主要介绍了基于Nginx实现访问控制、连接限制的相关资料,需要的朋友可以参考下
    2019-11-11
  • nginx解决跨域问题的实例方法

    nginx解决跨域问题的实例方法

    在本篇文章里小编给各位分享了关于nginx怎么解决跨域问题的方法和实例代码,需要的朋友们参考下。
    2019-07-07

最新评论