英文:
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:
<a href="http://sub.domain.com/1">some product id</a>
What it should be like:
<a href="http://10.101.171.23/1">some product id</a>
Code in the view:
<a href="{{ route('products.show', $product->id) }}">some product id</a>
答案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('app.url'));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论