Laravel路由解析为Web服务器的本地IP,而不是应用程序的URL。

huangapple go评论61阅读模式
英文:

Laravel Route resolves to local IP from webserver not to application URL

问题

以下是您要翻译的部分:

"I am having the problem that all my links generated with the route() function of laravel resolve to the local IP of the webserver instead of the domain written in the APP_URL section of the .env file."

  • "我遇到的问题是,使用Laravel的route()函数生成的所有链接都解析为Web服务器的本地IP,而不是.env文件中的APP_URL部分中写的域名。"

"Actually rendered:

<a href="http://sub.domain.com/1">some product id</a>"

  • "实际渲染的是:

<a href="http://sub.domain.com/1">一些产品ID</a>"

"What it should be like:

<a href="http://10.101.171.23/1">some product id</a>"

  • "应该是这样的:

<a href="http://10.101.171.23/1">一些产品ID</a>"

"Code in the view:

<a href="{{ route('products.show', $product->id) }}">some product id</a>"

  • "视图中的代码:

<a href="{{ route('products.show', $product->id) }}">一些产品ID</a>"

英文:

I am having the problem that all my links generated with the route() function of laravel resolve to the local IP of the webserver instead of the domain written in the APP_URL section of the .env file.

  • The APP_URL is set correctly
  • nginx configured properly

Actually rendered:

&lt;a href=&quot;http://sub.domain.com/1&quot;&gt;some product id&lt;/a&gt;

What it should be like:

&lt;a href=&quot;http://10.101.171.23/1&quot;&gt;some product id&lt;/a&gt;

Code in the view:

&lt;a href=&quot;{{ route(&#39;products.show&#39;, $product-&gt;id) }}&quot;&gt;some product id&lt;/a&gt;

答案1

得分: 1

你现在有一个代理,它将流量重定向到你的nginx,就像一个配置不良的负载均衡器可能会做的那样。

在HTTP请求中,域名是从全局变量 $_SERVER 中获取的,而不是从 .env 配置文件中获取的。

要在代码方面解决这个问题,你可以将以下内容添加到 AppServiceProvider.php 中的 boot() 方法中:

public function boot()
{
    URL::forceRootUrl(Config::get('app.url'));
}
英文:

You mostly have a proxy in place that redirects the trafic to your nginx, like a badly configured load balancer might do.

In HTTP request the domain is taken from the global variable $_SERVER and not from the .env config.

To fix the issue code wise, you can add this to your boot() method in AppServiceProvider.php

public function boot()
{
    URL::forceRootUrl(Config::get(&#39;app.url&#39;));
}

huangapple
  • 本文由 发表于 2023年4月11日 07:14:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定