AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 688795
Accepted
Drifter104
Drifter104
Asked: 2015-05-06 08:30:35 +0800 CST2015-05-06 08:30:35 +0800 CST 2015-05-06 08:30:35 +0800 CST

Nginx 301 重定向 https 到 http

  • 772

我对尾随 / 有一个奇怪的问题

我正在使用 nginx,它运行正常,但有一个小例外。
我有以下站点配置

    server {
listen         80;
return         301 https://$server_name$request_uri/;
server_name    sub1.sub2.domain.com;
}
    server {
 listen              443 ssl; # The ssl directive tells NGINX to decrypt
                              # the traffic
 server_name         sub1.sub2.domain.com;
 ssl_certificate     /etc/nginx/ssl/sub1.sub2.domain.com/server.crt; # This is the certificate file
 ssl_certificate_key /etc/nginx/ssl/sub1.sub2.domain.com/server.key; # This is the private key file
 location / {
            proxy_pass http://1.1.1.1:8880;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
    }
}

我有一个场外身份验证服务正在运行并保护一个名为安全的子文件夹,因此如果请求 /secure 或 /secure/ ,它们将被发送到场外身份验证服务。一旦他们通过身份验证,他们就会被重定向回他们最初请求的任何 url。如果在身份验证后他们碰巧请求 /secure/ 一切正常。如果他们键入 /secure(没有尾随 /),nginx 在身份验证后执行 301 重定向并将 https 替换为 http,因此他们到达http://sub1.sub2.domain.com/secure,然后通过另一个重定向返回 https

从我在这里读到的http://nginx.org/en/docs/http/ngx_http_core_module.html#location这是正确的行为,但是将 /secure/ 和 /secure 定义为单独的位置文件的解决方案没有似乎有效,并且在该示例中也没有提及有关 https 到 http 更改的任何内容。任何帮助将不胜感激。

nginx
  • 3 3 个回答
  • 3608 Views

3 个回答

  • Voted
  1. Michael Hampton
    2015-05-06T09:51:25+08:002015-05-06T09:51:25+08:00

    您在重定向中明确添加斜杠:

    return         301 https://$server_name$request_uri/;
    

    这显然不是你想要的。

    所以不要添加尾部斜杠:

    return         301 https://$server_name$request_uri;
    
    • 2
  2. Best Answer
    Xavier Lucas
    2015-05-06T09:17:43+08:002015-05-06T09:17:43+08:00

    您的应用程序对此负责,这是因为您需要使用额外的标头转发当前方案,例如X-Forwarded-Proto自从您禁用proxy_redirect后,这意味着重定向从您的上游服务器保持不变并直接发送给访问者,而不需要 nginx 使其相对于位置上下文或方案在当前服务器块中使用。

    所以在那里修复它或使用 nginx 的proxy_redirect相关方式。

    • 1
  3. Drifter104
    2015-05-07T02:06:23+08:002015-05-07T02:06:23+08:00

    所以事实证明文档是正确的(谁知道),我在尝试解决尾随 / 的第一个问题时引入了更多问题

    通过进行以下更改,我能够解决此问题。

    return         301 https://$server_name$request_uri;
    

    (删除尾随 / 用于 http 重定向)

    location = /secure {
                return  301 https://$server_name$request_uri/;
    }  
    

    这将对在进入身份验证服务之前未将 / 置于安全末尾的任何人执行重定向。

    proxy_redirect http:// $scheme://;  
    

    这会将应用程序的每个 http 重定向重写为 https

    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve