Nginx: 最常见的 2 中 http to https 跳转场景

Nginx: 最常见的 2 中 http to https 跳转场景Nginx 最常见的 2 种 http to https 跳转场景建议点击 查看原文查看最新内容 原文链接 https typonotes com posts 2023 08 28 nginx http https redirect

欢迎大家来到IT世界,在知识的湖畔探索吧!



Nginx: 最常见的 2 中 http to https 跳转场景
欢迎大家来到IT世界,在知识的湖畔探索吧!

Nginx: 最常见的 2 种 http to https 跳转场景

建议点击 查看原文 查看最新内容。

原文链接:
https://typonotes.com/posts/2023/08/28/nginx-http-https-redirect-scenarios/

1. Nginx 上层无代理, 用户直接访问

这种方式比较简单。

  1. 我们对 http 和 https 都具有控权。
  2. 用户是直接访问 Nginx 服务器。
Nginx: 最常见的 2 中 http to https 跳转场景

所以可以直接通过在 http server 上配置到 301 跳转 到 https 服务器即可。

# http server
server {
listen 80;
server_name _;
return 301 https://$host$request_uri;
}

# https server
server {
listen 443 ssl http2;
server_name www.example.com;

# ... other
}

欢迎大家来到IT世界,在知识的湖畔探索吧!

通常, 我个人习惯将两个配置写在同一个文件中。更具体的配置逻辑都放在 https server 中。

2. Nginx 上层有代理

这种情况, 稍微麻烦一点。

  1. 最重要的, 用户并不直接访问我们的 Nginx Server, 而是通过上层代理 Proxy 代理。
  2. 实际提供 HTTPS 服务的其实是上层 Proxy, 且
    我们并没有管理权限
  3. 因此, Proxy 在访问 Nginx Server 的时候, 始终使用
    HTTP 协议。
Nginx: 最常见的 2 中 http to https 跳转场景

这种情况下, 我们直接使用 Nginx 提供的 内置变量 scheme 就行不通了。

欢迎大家来到IT世界,在知识的湖畔探索吧!# 错误配置
server {
listen 80;
server_name _;

if ($scheme = "http"){
return 301 https://$host$request_uri;
}
}

使用上述配置, 无论用户通过任何协议请求, Nginx Server 拿到的都是 http, 即 条件恒等。结果就是永远在跳转, 直到重定向次数过多而报错。

解决方案就是 使用 Proxy 提供的 Header 进行判断。不同的 Proxy 提供的 Header 名称可能不一样,需要具体分析。

# 可用配置
server {
listen 80;
server_name _;

# ... other

if ($http_x_forward_scheme = "http"){
return 301 https://$host$request_uri;
}
}

注意: 这里的 http_x_forward_scheme 对应的就是 请求头 中的 X-Forward-Scheme。具体规则参考 3. Nginx 获取 Http Header 规则

3. Nginx 获取 Http Header 规则

Nginx 默认提供了获取 HTTP Header 的方法, 参考文档 Nginx 各种头技巧[1]

这里做一个总结,

3.1 HTTP Header 转 Nginx 变量

默认情况下 变量名遵守以下规则:

  1. 将 Header 名称 所有大写变小些, 所有
    -
    _

  2. 以 http_ 开头
  3. Header 名称不支持
    下划线
欢迎大家来到IT世界,在知识的湖畔探索吧! 正确
Server-Version => http_server_version
X-Forward-Scheme => http_x_forward_scheme
X-Customize-Header => http_x_customize_header

错误
Server_Verver (x)

如果要支持 Header 名称下划线, 需要 额外开启 语法 underscores_in_headers[2]

Syntax: underscores_in_headers on | off;
Default: underscores_in_headers off;
Context: http, server
欢迎大家来到IT世界,在知识的湖畔探索吧!server {
underscores_in_headers on;
}

开启之后, 即可使用。

Server_Version => http_server_version

3.2 Header 变量的常规操作

  1. 判断 header 是否存在
欢迎大家来到IT世界,在知识的湖畔探索吧!server {
if ( $x_customize_header ){
# statement
}
}
  1. 判断 header 值是否预期, 参考 nginx if 语法。
server {
if ( $x_customize_header = "vscode-client/v1.2" ){
# statement
}
}

参考文档

  1. Heroku Routing Header: https://devcenter.heroku.com/articles/http-routing
  2. Nginx 各种头技巧: https://liqiang.io/post/nginx-redirect-with-request-header-3c
  3. Nginx配置:读取自定义header + 撰写AND条件 + 修改响应体 + 域名重定向: https://segmentfault.com/a/52253
  4. Nginx If-Condition: https://blog.xinac.cn/archives/nginx%E9%85%8D%E7%BD%AE%E4%B8%ADifelse%E7%9A%84%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%B3%95.html
  5. Nginx if-is-evil: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/
  6. Nginx Creating-Nginx-Rewrite-Rules: https://www.nginx.com/blog/creating-nginx-rewrite-rules/
  7. Nginx 中的 If 判断: https://www.ucloud.cn/yun/40533.html

互相吹捧, 共同进步

加我好友, 备注 技术群 加群一起学习 Golang, Devops, Docker/K8s

参考资料

[1]

Nginx 各种头技巧:
https://liqiang.io/post/nginx-redirect-with-request-header-3c


[2]

语法 underscores_in_headers:
http://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers


免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/119181.html

(0)
上一篇 2025年 4月 11日 下午6:23
下一篇 2025年 4月 11日 下午7:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信