csun-metalab/lumen-proxypass

Lumen 的 Composer 包,用于在代理后解析正确的绝对 URL

1.1.0 2017-11-08 19:22 UTC

This package is auto-updated.

Last update: 2024-09-26 04:16:08 UTC


README

Lumen 的 Composer 包,用于在代理后解析正确的绝对 URL

此包是为 Lumen 5.0 版本及以上构建的。它修改了 url()asset() 以及其他辅助方法的函数。

要从 Composer 安装,请使用以下命令

composer require csun-metalab/lumen-proxypass

安装

首先,将以下行添加到您的 .env 文件中以利用代理属性

PROXY_ACTIVE=true
PROXY_PATH_HEADER=HTTP_X_FORWARDED_PATH

您也可以将以下可选行添加到您的 .env 文件中以利用强制 URL 和协议的能力,而无需通过负载均衡器或代理

# http or https: PUBLIC_SCHEMA_OVERRIDE=https
PUBLIC_SCHEMA_OVERRIDE=

# Example: PUBLIC_URL_OVERRIDE=http://some-other-domain.example.com/some-other-directory
PUBLIC_URL_OVERRIDE=

接下来,在 bootstrap/app.php 中注册服务提供者和配置文件,如下所示

$app->configure('proxypass');
$app->register(CSUNMetaLab\LumenProxyPass\Providers\ProxyPassServiceProvider::class);

配置文件

如果您在项目根目录中没有配置 config 目录,请创建它。

为了利用此包的定制配置值,请在 Lumen 的 config 目录中创建一个名为 proxypass.php 的文件,并将以下代码复制粘贴到该文件中

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Proxy Active?
    |--------------------------------------------------------------------------
    |
    | Determines whether the rewriting of the URLs is active. Default is true.
    |
    */
    'proxy_active' => env("PROXY_ACTIVE", true),

    /*
    |--------------------------------------------------------------------------
    | Public Absolute URL Override
    |--------------------------------------------------------------------------
    |
    | Overrides the root of the path generated by various methods to be the value
    | specified here when resolving the "public" directory. Methods include
    | asset(), url(), etc.
    |
    | I.E. A value of "https:///someotherpath/public" would make the
    | methods start with "https:///someotherpath/public" instead
    | of the typical "http://[domain]/[application path]/public" prefix.
    |
    */
    'public_url_override' => env("PUBLIC_URL_OVERRIDE", ""),

    /*
    |--------------------------------------------------------------------------
    | Public URL Schema Override
    |--------------------------------------------------------------------------
    |
    | Overrides the schema (http|https) that will be prefixed to all URLs.
    |
    */
    'public_schema_override' => env("PUBLIC_SCHEMA_OVERRIDE", ""),

    /*
    |--------------------------------------------------------------------------
    | Proxied Path Header
    |--------------------------------------------------------------------------
    |
    | The PHP-transformed name of the header that is passing along the rewritten
    | base URL from the proxy. Defaults to "HTTP_X_FORWARDED_PATH".
    |
    */
    'proxy_path_header' => env("PROXY_PATH_HEADER", "HTTP_X_FORWARDED_PATH"),

    /*
    |--------------------------------------------------------------------------
    | Trusted Proxies
    |--------------------------------------------------------------------------
    |
    | Comma-delimited list of the hostnames/IP addresses of proxy servers that
    | are allowed to manipulate the scheme and base URL. If no trusted proxies
    | have been set then proxying is allowed implicitly since this functions
    | as a whitelist.
    |
    */
    'trusted_proxies' => env("TRUSTED_PROXIES", ""),

];

?>

环境变量

您添加到 .env 文件的两个环境变量如下

PROXY_ACTIVE

将此设置为 true 以启用代理功能或 false 以禁用它。

PROXY_PATH_HEADER

这是来自您的代理的请求头中 PHP 解释的值。默认为 HTTP_X_FORWARDED_PATHX-Forwarded-Path 的计算值)

受信任的代理

此包还具有允许仅某些代理服务器修改必要的值以设置正确的绝对 URL 的能力。

默认情况下,所有代理服务器都允许修改值;但是,如果您的 .env 文件中设置了以下值,则可以创建一个代理白名单

TRUSTED_PROXIES

这是一个逗号分隔的列表,包含允许执行代理功能的域名/IP 地址。

TRUSTED_PROXIES=192.168.1.10,www.example.com,192.168.3.12

上述示例将允许以下三个代理服务器提供代理功能

  • 192.168.1.10(来自 PHP 中的 REMOTE_ADDR 值)
  • www.example.com(来自 Web 服务器的 X-Forwarded-Server 头部)
  • 192.168.3.12(来自 PHP 中的 REMOTE_ADDR 值)

使用示例

假设您有一个托管在 http://lumen.example.com 的 API,但您不想向世界展示这个位置。相反,您想显示 http://www.example.com/lumen 的 URL,因此您将 Lumen API 放在代理后面。

然而,您注意到,虽然首页加载正确,但您使用 url()asset() 或其他辅助方法编写的所有 URL 都没有使用该 URL,而是继续将 http://lumen.example.com 作为它们的基路径。

您可以将配置您的代理,在 Apache 中添加请求头,并带有 ProxyPassProxyPassReverse 指令(确保已启用 mod_headersmod_proxy

ProxyPass        /lumen http://lumen.example.com
ProxyPassReverse /lumen http://lumen.example.com

<Location /lumen>
RequestHeader set X-Forwarded-Path "http://www.example.com/lumen"
</Location>

现在,您使用 url()asset() 和其他辅助方法编写的所有 URL 都将正确编写!