当前位置:首页 > 技术文章 > 正文内容

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

arlanguage5个月前 (12-20)技术文章34



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

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

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

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

这种方式比较简单。

  1. 我们对 http 和 https 都具有控权。
  2. 用户是直接访问 Nginx 服务器。

所以可以直接通过在 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
}

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

2. Nginx 上层有代理

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

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

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

# 错误配置
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 名称不支持 下划线
## 正确
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
server {
underscores_in_headers on;
}

开启之后, 即可使用。

Server_Version => http_server_version

3.2 Header 变量的常规操作

  1. 判断 header 是否存在
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-3c575166
  3. Nginx配置:读取自定义header + 撰写AND条件 + 修改响应体 + 域名重定向: https://segmentfault.com/a/1190000020852253
  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-3c575166

[2]

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


扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/369.html

标签: nginx 语法 if
分享给朋友:

“Nginx: 最常见的 2 种 http to https 跳转场景” 的相关文章

推荐一个Nginx配置文件的网站 nginx配置文件的几大模块

NGINX 不仅仅是一个 Web 服务器。你可能已经知道了。我们喜欢 NGINX,因为:内存使用率低高并发异步事件驱动架构负载均衡反向代理FastCGI 支持缓存 (PHP)静态文件的惊人快速处理使用 SNI 的 TLS/SSL特征:HTTPS、HTTP/2、IPv6、certbot、HSTS、安全...

Nginx 高可用方案

原文链接:https://www.cnblogs.com/SimpleWu/p/11004902.html准备工作192.168.16.128192.168.16.129两台虚拟机。安装好Nginx安装Nginx更新yum源文件:rpm -ivh http://nginx.org/packages/...

Nginx安装与调优部署文档(WinServer)

1. 安装环境准备1.1 部署规划软件安装路径 E:\nginx软件日志路径 E:\nginx\logs软件主配置文件路径 E:\nginx\conf软件子配置文件路径 E:\nginx\conf\conf.d软件站点配置路径 E:\nginx\html端口规划 802. Nginx安装部署2.1...

Nginx安装、启动、停止、重载、查看

Nginx介绍Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在开源BSD-like 协议下发行。特点是占有内存少,并发能力强。1、安装 Nginx程序yum install nginx -y 或 dnf install nginx -y2、n...

Nginx如何配置正向代理:一步步教你轻松上手

Nginx作为一个高性能的HTTP和反向代理服务器,广泛应用于各类网站和服务中。然而,很多人可能不知道,Nginx同样可以配置为正向代理。今天我们就来详细讲解一下如何配置Nginx作为正向代理,让你的网络访问更加灵活便捷。什么是正向代理?正向代理是指客户端通过代理服务器访问目标服务器的过程。简单来说...

08《Nginx 入门教程》Nginx 的 Http 模块介绍(中)

在前面介绍完 post-read、server-rewrite、find-config、rewrite 和 post-rewrite 阶段后,我们将继续学习 preaccess 和 access 两个阶段,中间会涉及部分模块,一同进行说明。1. preaccess 阶段在 preaccess 阶段在...