nginx 上反向代理别名后面的 Nette
P粉953231781
P粉953231781 2023-09-04 00:28:05
0
1
418
<p>我有一个在 Apache2/Debian 11 服务器上运行的 Nette 应用程序,运行良好。但是,我们需要使用别名将其隐藏在 nginx 代理后面。</p> <p>假设我们有一个运行在 http://127.0.0.1:89/ 上的完美 Apache2 环境,并且我们希望通过设置为反向代理的 nginx 来访问它,地址为 https://example.com/应用程序/</p> <p>nginx设置如下:</p> <pre class="brush:php;toolbar:false;">location /app/ { proxy_pass http://127.0.0.1:89; proxy_set_header Host $host; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Proto &quot;https&quot;; proxy_set_header X-Forwarded-Port &quot;443&quot;; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }</pre> <p>虽然 Apache2 配置在代理尝试之前保持不变:</p> <pre class="brush:php;toolbar:false;">&lt;VirtualHost *:89&gt; ServerAdmin webmaster@localhost DocumentRoot /var/www/app/www/ &lt;Directory /var/www/app/www/&gt; Options Indexes FollowSymLinks AllowOverride All Require all granted &lt;/Directory&gt; &lt;/VirtualHost&gt;</pre> <p>问题是,Nette 应用程序仍然认为它在没有“/app”URI 部分的路径上运行,因此通过重定向和链接调用生成的所有链接(包括 $basePath 模板变量)都是无效的。< /p> <p>我还将代理信息附加到 Nette 配置中,并且由于 nginx 实例在同一服务器上运行,因此它看起来如下:</p> <pre class="brush:php;toolbar:false;">http: proxy: 127.0.0.1</pre> <p>我尝试将 nginx 配置设置为转发转发的 URI 中的 /admin 路径:</p> <pre class="brush:php;toolbar:false;">proxy_pass http://127.0.0.1:89/admin;</pre> <p>并且还尝试摆弄 Nette 路由器以过滤掉“admin/”部分(不寻找 AdminPresenter,这显然是缺失的):</p> <pre class="brush:php;toolbar:false;">$router-&gt;addRoute('[admin/]&lt;presenter&gt;/&lt;action&gt;[/&lt;id&gt;]', 'Homepage:default');</pre> <p>Nette 应用程序在尝试访问该页面时产生此错误:</p> <pre class="brush:php;toolbar:false;">TypeError: unpack() expects parameter 2 to be string, bool given in /var/www/app/vendor/nette/http/src/Http/Helpers.php:49 @ http://example.com/app/</pre> <p>有人可以给我指出正确的方向吗?</p>
P粉953231781
P粉953231781

répondre à tous(1)
P粉035600555

好吧,回答我自己的问题,以防有人遇到同样的问题。我觉得我的解决方案有点黑客,所以请随意发布非黑客答案。

首先,我修改了 nginx 配置:

location /app/ {
    rewrite /app/(.*) /app/$1 break;
    proxy_pass http://127.0.0.1:89/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Proto "https";
    proxy_set_header X-Forwarded-Port "443";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

然后我修改了 Nette 配置以覆盖所有代理(可能实际上并不需要):

http:
    proxy: 0.0.0.0/0

我还在路由器代码中添加了“app”路由(不是作为可选前缀,而是作为常规路由):

$router = new RouteList;
$router->addRoute('app/<presenter>/<action>[/<id>]', 'Homepage:default');
$router->addRoute('<presenter>/<action>[/<id>]', 'Homepage:default');
return $router;

还修改了BasePresenter代码启动方法:

$this->template->basePath = '/app'.$this->template->basePath;

最后,我修改了.htaccess文件,将所有静态资源的URL重写为不包含“app”部分的路径:

# Default Nette htaccess contents
RewriteEngine On
RewriteRule /\.|^\. - [F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$ index.php [L]

# Added this line under the default Nette htaccess file
RewriteRule ^app(/.*|$) $1 [NC,L]

Apache 虚拟主机保持不变。

这样,nginx 将完整的请求 URL(包括“app”)传递给 Apache,Apache 使用“app”前缀调用 Nette 路由器。路由现在工作正常,因为“应用程序”是 URL 的一部分,甚至可以被 Nette 感知(因为它实际上完全存在于请求标头中)。这会导致 $basePath 和 links/redirect 工作。

但是,静态资源不是通过 Nette 路由器提供的,因此 app/ 前缀会导致 Apache 找不到该文件并报告 404。这就是添加重写规则的原因从静态资源的 URL 中删除 app/ 前缀。

它很hacky,但它适用于代理和非代理访问。

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!