問題描述
我在同一臺服務器上運行了 2 個 Laravel 應用程序.服務器是 Apache 2.4,我設置了虛擬主機來為不同域上的每個應用程序提供服務.
I have 2 Laravel applications running on the same server. The server is Apache 2.4 and I have vhosts set up to serve each application on a different domain.
第一個應用程序是一個 API,它的 .env 文件設置如下:
The first application is an API and it's .env file is set up like this:
APP_ENV=production
APP_KEY=YYYYYYYYYYYYYYYYYY
APP_DEBUG=false
APP_LOG_LEVEL=debug
APP_URL=https://notify.mysite.com
APP_DOMAIN=notify.mysite.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=notify
DB_USERNAME=YYYYYYYYYYYYYYYYYY
DB_PASSWORD=YYYYYYYYYYYYYYYYYY
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
第二個應用程序是一個 UI,其中使用了第一個應用程序的 API.它的 .env 文件是這樣設置的:
The second application is a UI that among other things, utilizes the API from the first application. Its .env file is set up like this:
APP_ENV=local
APP_KEY=XXXXXXXXXXXXXX
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=https://asapps.mysite.com
APP_DOMAIN=asapps.mysite.com
APP_VERSION=1
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=asapps
DB_NOTIFY_DATABASE=notify
DB_FLIGHT_DATABASE=flights
DB_USERNAME=XXXXXXXXXXXXXX
DB_PASSWORD=XXXXXXXXXXXXXX
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
我可以從 Swagger 編輯器、Postman 和其他服務器向我的 API 發送消息,一切都按預期進行.
I can send messages to my API from my Swagger editor, from Postman, and from other servers and everything works as expected.
我的第二個應用程序本身也按預期工作.
My second application by itself also works as expected.
但是,如果我的第二個應用程序向 API 發送請求,則 API 應用程序會拋出此錯誤:
However, if my second application sends a request to the API, the API application throws this error:
帶有消息SQLSTATE[42S02]"的異常PDOException":未找到基表或視圖:1146 表asapps.preprocessor_config"不存在"在 C: otifyvendorlaravelframeworksrcIlluminateDatabaseConnection.php:332
exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'asapps.preprocessor_config' doesn't exist' in C: otifyvendorlaravelframeworksrcIlluminateDatabaseConnection.php:332
什么?
API 的數據庫設置為DB_DATABASE=notify,當我從其他服務器發送消息時,它肯定會正確使用該連接.那么為什么要嘗試使用第二個應用程序的數據庫當我從該應用程序調用 API 時連接???它幾乎就像在緩存數據庫連接并試圖繼續使用相同的連接......我該如何阻止?
The database for the API is set to DB_DATABASE=notify and it definitely does properly use that connection when I send messages from other servers. So why the heck is it trying to use the second application's database connection when I call the API from that app??? Its almost like it's caching the DB connection and trying to keep using that same one.... How do I stop that?
表'asapps.preprocessor_config'不存在'
Table 'asapps.preprocessor_config' doesn't exist'
推薦答案
經過更多的挖掘(閱讀瘋狂的谷歌搜索),我找到了 問題和解決方案在這里
After more digging (read frantic googling), I found the problem and solution here
最重要的是,當站點 A 接受請求時,php 會在 http 請求的整個長度內加載它的 .env 變量.在該請求期間,當站點 A 調用站點 B 時,由于它們在運行相同 php 的同一臺服務器上,php 仍在使用站點 A 的 .env 并且根本不單獨加載站點 B 的 .env 文件.
The bottom line, when site A accepts a request, php loads it's .env variables for the entire length of the http request. During that request, when site A calls site B, since they are on the same server running the same php, php is still using the .env from site A and does not separately load site B's .env file at all.
作者更好的解釋:
創建了包含變量的 .env 文件,這樣人們就不會將他們的憑據推送到 github 存儲庫和其他可能共享源的地方.
The .env file with the variables was created so that people would not push their credentials to github repositories and other places where they may share the source.
現在,作為環境變量,它們在 http 請求的整個持續時間內(在本例中為腳本執行)成為系統范圍內的變量.關鍵是你有一個長時間運行的腳本.
Now, being environment variables they become system wide for the entire duration of the http request (in this case script execution). The point is that you got a long running script.
要找到最終解決方案,您可以采用以下三種方法之一.
To find a definitive solution you could go one of the three ways.
....
'namespace' ENV 變量.
'namespace' the ENV variables.
這篇關于同一臺服務器上的兩個 Laravel 應用程序相互沖突的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!