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 跨域配置的具体实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-09-09
  • Nginx 禁止访问某个目录或文件的设置方法

    Nginx 禁止访问某个目录或文件的设置方法

    如果基于WEB根目录下,要禁止用户访问/config目录,或者要禁止用户访问/config.ini(ZF常用INI,不过建议还是放到WEB目录以外的地方),可以通过location进行配置,返回403或者404等
    2010-09-09
  • Nginx配置http和https的实现步骤

    Nginx配置http和https的实现步骤

    Nginx是使用最多的代理服务器之一,本文主要介绍了Nginx配置http和https的实现步骤,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • 详解Nginx服务器中HTTP Headers相关的模块配置使用

    详解Nginx服务器中HTTP Headers相关的模块配置使用

    这篇文章主要介绍了详解Nginx服务器中HTTP Headers相关的模块配置使用,包括ngx_http_headers_module与它的增强版ngx_headers_more的配置使用讲解,需要的朋友可以参考下
    2016-01-01
  • Nginx反向代理中出现502错误的解决步骤

    Nginx反向代理中出现502错误的解决步骤

    反向代理是一种服务器代理的方式,它代理了客户端的请求并将请求转发给后端服务器,然后将后端服务器的响应返回给客户端,但经常会遇到502错误,所以本文给大家介绍了Nginx反向代理中出现502错误的解决步骤,需要的朋友可以参考下
    2025-03-03
  • nginx打印请求头日志方法(亲测可用)

    nginx打印请求头日志方法(亲测可用)

    之前想用nginx打印收到的请求的请求头,但是只找到打印请求体的,没有打印请求头的,本文就来介绍一下nginx打印请求头日志方法,感兴趣的可以了解一下
    2023-11-11
  • nginx 平滑重启的实现方法

    nginx 平滑重启的实现方法

    这篇文章主要介绍了nginx 平滑重启的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Nginx服务器中414错误和504错误的配置解决方法

    Nginx服务器中414错误和504错误的配置解决方法

    这篇文章主要介绍了Nginx服务器中414错误和504错误的配置解决方法,分别对应Request-URI Too Large和Gateway Time-out这样的错误提示,需要的朋友可以参考下
    2015-12-12
  • Nginx设置https和http同时使用同一个端口访问

    Nginx设置https和http同时使用同一个端口访问

    本文主要介绍了Nginx通过8070端口同时支持HTTP和HTTPS的配置方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-05-05
  • Windows下Nginx + PHP5 的安装与配置方法

    Windows下Nginx + PHP5 的安装与配置方法

    Nginx 是一个轻量级的高性能 Http WebServer,以事件驱动方式编写,因此相比 Apache 而言,Nginx 更加稳定、性能更好,而且配置简单,资源占用较低。以下是我在 Windows 7 安装中 Nginx 和 PHP5.3 的步骤。
    2010-07-07

最新评论