redirect - 如何从 nginx 发出相对 URL 重定向?

标签 redirect nginx

如何在 nginx 中配置重定向,以相对重定向到特定路径来响应某个 url?

nginx documentation表明这是 nginx 的默认模式,但实际上,如果重定向到的位置以/开头,则 nginx 会在位置字段中使用绝对 url 进行响应。

对于包含位置的本地服务器配置:

location = /a {
  return 301 some/path;
}
location = /b {
  return 301 /qwerty;
}

对/a 的请求的响应位置是:

Location: some/path

对于/b 来说是:

Location: http://127.0.0.1/qwerty

但是,我们希望/b 回复:

Location: /qwerty

我们想要使用相对重定向的原因是我们想要从不同的域和代理访问 nginx,例如在开发中或通过 ssl 终止负载平衡器明确表示,并且宁愿通过减轻 nginx 需要理解该上下文的方式来使事情变得简单。

仅供引用,这些示例是针对 nginx 版本 1.4.6 和 1.9.6 进行测试的,使用curl,例如:

curl --head http://127.0.0.1/b

最佳答案

有一个 nginx 指令 absolute_redirect从 nginx 版本 1.11.8 开始可用,默认情况下启用。如果禁用,nginx 发出的重定向将是相对的。这个问题有点老了,但也许对大家从谷歌上找到它的人有用。

 absolute_redirect off;

关于redirect - 如何从 nginx 发出相对 URL 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33523821/

相关文章:

Apache .htaccess 热链接重定向

redirect - NGINX:仅根据特定 URL 的 cookie 重定向用户

apache - mod_rewrite : remove trailing slash (only one! )

Node.js 请求模块 + NGINX

python - MemoryError 使用 Nginx、uWSGI 和 web.py

Grails Spring Security 使用 https 重定向到登录页面

java - Strut2有多个包会导致问题

nginx - 当使用 header Cache-control : no-cache? 请求时,nginx 是否重置 URL 的缓存

laravel - 在 Kubernetes 中部署 Laravel

ubuntu - kubernetes nginx 服务未从浏览器打开