Skip to content

[Bug] 反向代理 TLS 终结场景下,Laravel 生成的表单/链接为 http://,浏览器报"不安全"警告 #117

Description

@alick-zhang

问题描述

按照标准生产架构部署(宿主机 nginx 终结 HTTPS,反代到 geoflow-web 的 18080 端口)后,页面能正常打开,但所有表单提交时浏览器都会弹出 "The information you're about to submit is not secure" 警告

登录页实际渲染结果:

<form method="POST" action="http://geo.example.com/geo_admin/login">

浏览器地址栏是 https://,表单 action 却是 http://

复现条件

  • 宿主机 nginx:listen 443 ssl + proxy_pass http://127.0.0.1:18080,已正确传递 X-Forwarded-Proto $scheme
  • docker-compose 默认配置(geoflow-web 监听 127.0.0.1:18080

根因分析

问题 1:docker/nginx/local.conf 无条件覆盖 X-Forwarded-Proto

location @geoflow_app {
    proxy_set_header X-Forwarded-Proto $scheme;   # 容器内 $scheme 永远是 http
    ...
}

TLS 在上游代理终结时,容器内 $scheme 恒为 http,把上游传来的 https 覆盖掉了。/reverb/ 的 WebSocket 反代块也有同样问题。

问题 2:bootstrap/app.php 未配置 trustProxies

即使头传对了,Laravel 11 默认不信任任何代理,X-Forwarded-* 头会被忽略。因此本项目在「TLS 前置代理」这一最常见的生产形态下,生成的 URL 必然降级为 http。

受影响的不止表单 action,还包括:secure cookie 判定、CSRF、redirect/分页链接、Reverb 的 wss 连接。

修复建议

docker/nginx/local.conf:透传上游头,直连时回退本机值

map $http_x_forwarded_proto $geoflow_forwarded_proto {
    ''      $scheme;
    default $http_x_forwarded_proto;
}

map $http_x_forwarded_port $geoflow_forwarded_port {
    ''      $geoflow_host_port;
    default $http_x_forwarded_port;
}

map $http_host $geoflow_host_port {
    ~:([0-9]+)$ $1;
    default $server_port;
}

# location 内改用:
proxy_set_header X-Forwarded-Proto $geoflow_forwarded_proto;

bootstrap/app.php

$middleware->trustProxies(at: '*');   // 或限定为 docker 网段 172.16.0.0/12

已在本地实测:修复后登录页表单 action 正确生成为 https://,浏览器警告消失,站点/后台/静态资源均正常。

环境信息

  • 部署方式:docker-compose(nginx:1.27-alpine + php artisan serve
  • Laravel 11

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions