当前位置: 首页 > article >正文

Nginx下proxy_redirect的三种配置方式

Nginx中的proxy_redirect指令,用于修改代理服务器接收到的后端服务器响应中的重定向URL。在代理环境中,若后端返回的重定向URL不符合客户端需求,就用它调整。

语法

proxy_redirect default;

proxy_redirect redirect replacement;

proxy_redirect off;

default:Nginx按proxy_pass设置自动调整重定向 URL。 redirect replacement:redirect是原始重定向URL模式,replacement是替换后的URL。 off:禁用此功能,不修改重定向URL。

示例
<p>设后端服务器为http://example.shizhanxia.com,Nginx代理监听http://shizhanxia.com,后端返回内部重定向URL,需调整为外部可访问地址。</p>
<h3>示例1:使用default</h3>
<pre>server {
listen 80;
server_name shizhanxia.com;
location / {
proxy_pass http://example.shizhanxia.com;
proxy_redirect default;
}
}
</pre>
<p>当客户端访问http://shizhanxia.com时,Nginx会将请求代理至http://example.shizhanxia.com。经与后端交互,若后端返回重定向,比如重定向到http://example.shizhanxia.com/some/path,由于配置了proxy_redirect default,Nginx会将此重定向地址调整为http://shizhanxia.com/some/path再返回给客户端。</p>
<h3>示例2:自定义替换</h3>
<pre>server {
listen 80;
server_name shizhanxia.com;
location / {
proxy_pass http://example.shizhanxia.com;
proxy_redirect http://example.shizhanxia.com/ http://shizhanxia.com/;
}
}
</pre>
<p>如此配置后,若后端服务器返回以http://example.shizhanxia.com/开头的重定向URL,Nginx会依规则替换。如重定向URL为http://example.shizhanxia.com/login,经Nginx处理会变为http://shizhanxia.com/login,再返回给客户端,助其精准访问目标地址。</p>
<h3>示例3:禁用proxy_redirect</h3>
<pre>server {
listen 80;
server_name shizhanxia.com;
location / {
proxy_pass http://example.shizhanxia.com;
proxy_redirect off;
}
}
</pre>
<p>此配置下,后端重定向URL不被修改,直接返回客户端。</p>

http://www.kler.cn/a/553639.html

相关文章:

  • scroll、offset、client三大家族和getBoundingClientRect方法
  • android 定制mtp连接外设的设备名称
  • 基于JavaSpringboot+Vue实现前后端分离房屋租赁系统
  • lua的local 变量和self 变量的理解理解后解决自己写的bug.
  • 【论文阅读】DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning
  • git-提交时间和作者时间的区别
  • 当使用vcpkg安装的qt5时,在VS调用出现libcrypto-*-x64.dll不是有效路径时
  • Docker如何切换文件目录安装——详细攻略!
  • IOS UITextField 无法隐藏键盘问题
  • 苹果新品今日发布,AI手机市场竞争加剧,近屿智能专注AI人才培养
  • 《深度学习》——ResNet网络
  • 无人机避障——感知篇(采用Livox-Mid360激光雷达获取点云数据显示)
  • 使用 Python 和 OpenCV 进行图像边缘检测:从基础到实战
  • 基于MATLAB的均匀面阵MUSIC算法DOA估计仿真
  • 基于SpringBoot+vue粮油商城小程序系统
  • 汇能感知的光谱相机/模块产品有哪些?
  • 【机器学习】K折交叉验证(K-Fold Cross-Validation)
  • 网工项目实践2.4 北京公司安全加固、服务需求分析及方案制定
  • linux内核数据结构之哈希表
  • 【Cesium学习(十二)】Cesium常见问题整理总结